url_cleaner_engine/glue/caching/
path.rs

1//! The home of [`CachePath`].
2
3use std::str::FromStr;
4
5use serde::{Serialize, Deserialize};
6
7use crate::util::*;
8
9/// The path of a cache database.
10#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Suitability)]
11#[serde(deny_unknown_fields)]
12#[serde(remote = "Self")]
13pub enum CachePath {
14    /// Stores the database in memory, wiping it on program exit.
15    ///
16    /// Has the string representation of `:memory:`.
17    #[default]
18    Memory,
19    /// A filesystem/network/whatever path.
20    Path(String)
21}
22
23crate::util::string_or_struct_magic!(CachePath);
24
25impl CachePath {
26    /// The cache's path as a [`str`].
27    ///
28    /// If `self` is [`Self::Memory`], returns `:memory:`.
29    /// # Examples
30    /// ```
31    /// use url_cleaner_engine::glue::*;
32    ///
33    /// assert_eq!(CachePath::Memory                          .as_str(), ":memory:");
34    /// assert_eq!(CachePath::Path(       "abc.sqlite".into()).as_str(), "abc.sqlite");
35    /// assert_eq!(CachePath::Path("file://abc.sqlite".into()).as_str(), "file://abc.sqlite");
36    /// ```
37    pub fn as_str(&self) -> &str {
38        match self {
39            Self::Memory => ":memory:",
40            Self::Path(x) => x
41        }
42    }
43}
44
45impl AsRef<str> for CachePath {
46    fn as_ref(&self) -> &str {
47        self.as_str()
48    }
49}
50
51impl FromStr for CachePath {
52    type Err = std::convert::Infallible;
53
54    fn from_str(s: &str) -> Result<Self, Self::Err> {
55        Ok(s.into())
56    }
57}
58
59impl From<&str> for CachePath {
60    fn from(value: &str) -> Self {
61        value.to_string().into()
62    }
63}
64
65impl From<String> for CachePath {
66    fn from(value: String) -> Self {
67        match &*value {
68            ":memory:" => Self::Memory,
69            _ => Self::Path(value)
70        }
71    }
72}