shared_uri.rs

 1use std::ops::{Deref, DerefMut};
 2
 3use crate::SharedString;
 4
 5/// A URI stored in a [`SharedString`].
 6#[derive(PartialEq, Eq, Hash, Clone)]
 7pub enum SharedUri {
 8    /// A path to a local file.
 9    File(SharedString),
10    /// A URL to a remote resource.
11    Network(SharedString),
12}
13
14impl SharedUri {
15    /// Creates a [`SharedUri`] pointing to a local file.
16    pub fn file<S: Into<SharedString>>(s: S) -> Self {
17        Self::File(s.into())
18    }
19
20    /// Creates a [`SharedUri`] pointing to a remote resource.
21    pub fn network<S: Into<SharedString>>(s: S) -> Self {
22        Self::Network(s.into())
23    }
24}
25
26impl Default for SharedUri {
27    fn default() -> Self {
28        Self::Network(SharedString::default())
29    }
30}
31
32impl Deref for SharedUri {
33    type Target = SharedString;
34
35    fn deref(&self) -> &Self::Target {
36        match self {
37            Self::File(s) => s,
38            Self::Network(s) => s,
39        }
40    }
41}
42
43impl DerefMut for SharedUri {
44    fn deref_mut(&mut self) -> &mut Self::Target {
45        match self {
46            Self::File(s) => s,
47            Self::Network(s) => s,
48        }
49    }
50}
51
52impl std::fmt::Debug for SharedUri {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        match self {
55            Self::File(s) => write!(f, "File({:?})", s),
56            Self::Network(s) => write!(f, "Network({:?})", s),
57        }
58    }
59}
60
61impl std::fmt::Display for SharedUri {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        write!(f, "{}", self.as_ref())
64    }
65}