shared_url.rs

 1use std::ops::{Deref, DerefMut};
 2
 3use crate::SharedString;
 4
 5/// A URL stored in a `SharedString` pointing to a file or a remote resource.
 6#[derive(PartialEq, Eq, Hash, Clone)]
 7pub enum SharedUrl {
 8    /// A path to a local file.
 9    File(SharedString),
10    /// A URL to a remote resource.
11    Network(SharedString),
12}
13
14impl SharedUrl {
15    /// Create a URL pointing to a local file.
16    pub fn file<S: Into<SharedString>>(s: S) -> Self {
17        Self::File(s.into())
18    }
19
20    /// Create a URL 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 SharedUrl {
27    fn default() -> Self {
28        Self::Network(SharedString::default())
29    }
30}
31
32impl Deref for SharedUrl {
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 SharedUrl {
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 SharedUrl {
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 SharedUrl {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        write!(f, "{}", self.as_ref())
64    }
65}