oni_comb_uri_rs/models/
path.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::fmt;
use std::fmt::Formatter;

#[derive(Debug, Clone, PartialEq, Hash)]
pub enum Path {
  RootlessPath {
    type_name: &'static str,
    parts: Vec<String>,
  },
  AbemptyPath {
    type_name: &'static str,
    parts: Vec<String>,
  },
  AbsolutePath {
    type_name: &'static str,
    parts: Vec<String>,
  },
  NoSchemePath {
    type_name: &'static str,
    parts: Vec<String>,
  },
  EmptyPath {
    type_name: &'static str,
  },
}

impl Default for Path {
  fn default() -> Self {
    Path::of_empty()
  }
}

impl fmt::Display for Path {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    // let root = match self {
    //   Path::RootlessPath { .. } | Path::NoSchemePath { .. } | Path::EmptyPath { .. } => "",
    //   _ => "/",
    // };
    write!(f, "{}", self.parts().join(""))
  }
}

impl Path {
  pub fn of_rootless_from_strs(parts: &[&str]) -> Self {
    Path::RootlessPath {
      type_name: "rootless_path",
      parts: parts.into_iter().map(|e| e.to_string()).collect::<Vec<_>>(),
    }
  }

  pub fn of_rootless_from_strings(parts: &[String]) -> Self {
    Path::RootlessPath {
      type_name: "rootless_path",
      parts: Vec::from(parts),
    }
  }

  pub fn of_abempty_from_strs(parts: &[&str]) -> Self {
    Path::AbemptyPath {
      type_name: "abempty_path",
      parts: parts.into_iter().map(|e| e.to_string()).collect::<Vec<_>>(),
    }
  }

  pub fn of_abempty_from_strings(parts: &[String]) -> Self {
    Path::AbemptyPath {
      type_name: "abempty_path",
      parts: Vec::from(parts),
    }
  }

  pub fn of_absolute_from_strs(parts: &[&str]) -> Self {
    Path::AbsolutePath {
      type_name: "absolute_path",
      parts: parts.into_iter().map(|e| e.to_string()).collect::<Vec<_>>(),
    }
  }

  pub fn of_absolute_from_strings(parts: &[String]) -> Self {
    Path::AbsolutePath {
      type_name: "absolute_path",
      parts: Vec::from(parts),
    }
  }

  pub fn of_no_scheme_from_strs(parts: &[&str]) -> Self {
    Path::NoSchemePath {
      type_name: "no_scheme_path",
      parts: parts.into_iter().map(|e| e.to_string()).collect::<Vec<_>>(),
    }
  }

  pub fn of_no_scheme_from_strings(parts: &[String]) -> Self {
    Path::NoSchemePath {
      type_name: "no_scheme_path",
      parts: Vec::from(parts),
    }
  }

  pub fn of_empty() -> Self {
    Path::EmptyPath {
      type_name: "empty_path",
    }
  }

  pub fn type_name(&self) -> &'static str {
    match self {
      &Path::RootlessPath { type_name, .. } => type_name,
      &Path::AbemptyPath { type_name, .. } => type_name,
      &Path::AbsolutePath { type_name, .. } => type_name,
      &Path::NoSchemePath { type_name, .. } => type_name,
      &Path::EmptyPath { type_name } => type_name,
    }
  }

  pub fn parts(&self) -> &Vec<String> {
    static EMPTY_PARTS: Vec<String> = vec![];
    match self {
      Path::RootlessPath { parts, .. } => parts,
      Path::AbemptyPath { parts, .. } => parts,
      Path::AbsolutePath { parts, .. } => parts,
      Path::NoSchemePath { parts, .. } => parts,
      Path::EmptyPath { .. } => &EMPTY_PARTS,
    }
  }

  pub fn is_empty(&self) -> bool {
    self.parts().is_empty()
  }

  pub fn non_empty(&self) -> bool {
    !self.is_empty()
  }

  pub fn with_parts(&mut self, parts: Vec<String>) {
    self.add_parts(parts)
  }

  pub fn to_rootless(&self) -> Path {
    Path::of_rootless_from_strings(&self.parts().clone())
  }

  pub fn to_absolute(&self) -> Path {
    Path::of_absolute_from_strings(&self.parts().clone())
  }

  pub fn add_part(&mut self, part: String) {
    let parts_opt = match self {
      Path::RootlessPath { parts, .. } => Some(parts),
      Path::AbemptyPath { parts, .. } => Some(parts),
      Path::AbsolutePath { parts, .. } => Some(parts),
      Path::NoSchemePath { parts, .. } => Some(parts),
      Path::EmptyPath { .. } => None,
    };
    match parts_opt {
      Some(parts) => {
        parts.push(part);
      }
      None => (),
    }
  }

  pub fn add_parts(&mut self, parts: Vec<String>) {
    for x in parts {
      self.add_part(x)
    }
  }
}