2024-01-30 05:56:51 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2024-01-09 15:55:49 +00:00
|
|
|
|
|
|
|
use crate::SharedString;
|
|
|
|
|
2024-01-30 14:54:23 +00:00
|
|
|
/// A URI stored in a [`SharedString`].
|
2024-01-30 05:56:51 +00:00
|
|
|
#[derive(PartialEq, Eq, Hash, Clone)]
|
2024-01-30 14:54:23 +00:00
|
|
|
pub enum SharedUri {
|
2024-01-30 05:56:51 +00:00
|
|
|
/// A path to a local file.
|
|
|
|
File(SharedString),
|
|
|
|
/// A URL to a remote resource.
|
|
|
|
Network(SharedString),
|
|
|
|
}
|
2024-01-09 15:55:49 +00:00
|
|
|
|
2024-01-30 14:54:23 +00:00
|
|
|
impl SharedUri {
|
|
|
|
/// Creates a [`SharedUri`] pointing to a local file.
|
2024-01-30 05:56:51 +00:00
|
|
|
pub fn file<S: Into<SharedString>>(s: S) -> Self {
|
|
|
|
Self::File(s.into())
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:54:23 +00:00
|
|
|
/// Creates a [`SharedUri`] pointing to a remote resource.
|
2024-01-30 05:56:51 +00:00
|
|
|
pub fn network<S: Into<SharedString>>(s: S) -> Self {
|
|
|
|
Self::Network(s.into())
|
2024-01-09 15:55:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:54:23 +00:00
|
|
|
impl Default for SharedUri {
|
2024-01-30 05:56:51 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::Network(SharedString::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:54:23 +00:00
|
|
|
impl Deref for SharedUri {
|
2024-01-30 05:56:51 +00:00
|
|
|
type Target = SharedString;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
match self {
|
|
|
|
Self::File(s) => s,
|
|
|
|
Self::Network(s) => s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:54:23 +00:00
|
|
|
impl DerefMut for SharedUri {
|
2024-01-30 05:56:51 +00:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
match self {
|
|
|
|
Self::File(s) => s,
|
|
|
|
Self::Network(s) => s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:54:23 +00:00
|
|
|
impl std::fmt::Debug for SharedUri {
|
2024-01-09 15:55:49 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-01-30 05:56:51 +00:00
|
|
|
match self {
|
|
|
|
Self::File(s) => write!(f, "File({:?})", s),
|
|
|
|
Self::Network(s) => write!(f, "Network({:?})", s),
|
|
|
|
}
|
2024-01-09 15:55:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:54:23 +00:00
|
|
|
impl std::fmt::Display for SharedUri {
|
2024-01-30 05:56:51 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.as_ref())
|
2024-01-09 15:55:49 +00:00
|
|
|
}
|
|
|
|
}
|