rrpack_basis/
auto_path.rs

1use rill_protocol::io::provider::{EntryId, Path};
2use serde::{Deserialize, Serialize};
3
4const SIZE: usize = 4;
5
6/// `Live` bacause of `Live` product approach.
7#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
8#[serde(from = "String", into = "String")]
9pub struct AutoPath {
10    pub package: EntryId,
11    pub dashboard: EntryId,
12    pub group: EntryId,
13    pub name: EntryId,
14}
15
16impl AutoPath {
17    fn unassigned(name: EntryId) -> Self {
18        let entry = EntryId::from("unassigned");
19        Self {
20            package: entry.clone(),
21            dashboard: entry.clone(),
22            group: entry,
23            name,
24        }
25    }
26}
27
28impl From<AutoPath> for Path {
29    fn from(this: AutoPath) -> Self {
30        vec![this.package, this.dashboard, this.group, this.name].into()
31    }
32}
33
34impl From<[&str; SIZE]> for AutoPath {
35    fn from(array: [&str; SIZE]) -> Self {
36        Self {
37            package: array[0].into(),
38            dashboard: array[1].into(),
39            group: array[2].into(),
40            name: array[3].into(),
41        }
42    }
43}
44
45impl From<String> for AutoPath {
46    fn from(s: String) -> Self {
47        let s: &str = s.as_ref();
48        Self::from(s)
49    }
50}
51
52impl From<&str> for AutoPath {
53    fn from(s: &str) -> Self {
54        let path = s.parse::<Path>().map(Vec::from);
55        match path {
56            Ok(path) if path.len() == SIZE => {
57                let mut items = path.into_iter();
58                Self {
59                    package: items.next().unwrap(),
60                    dashboard: items.next().unwrap(),
61                    group: items.next().unwrap(),
62                    name: items.next().unwrap(),
63                }
64            }
65            _ => Self::unassigned(EntryId::from(s)),
66        }
67    }
68}
69
70impl From<AutoPath> for String {
71    fn from(path: AutoPath) -> Self {
72        format!(
73            "{}.{}.{}.{}",
74            path.package, path.dashboard, path.group, path.name
75        )
76    }
77}