1use std::fmt;
2use std::str::{FromStr, Split};
3
4use serde::{Deserialize, Serialize};
5
6use crate::artifact::SubSpaceName;
7use crate::error::Error;
8
9#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
10pub struct Name {
11 pub sub_space: SubSpaceName,
12 pub path: String,
13}
14
15impl Name {
16 pub fn more(string: &str) -> Result<(Self, Split<&str>), Error> {
17 let (sub_space, mut parts) = SubSpaceName::more(string)?;
18
19 Ok((
20 Name {
21 sub_space: sub_space,
22 path: parts.next().ok_or("path")?.to_string(),
23 },
24 parts,
25 ))
26 }
27
28 pub fn from(string: &str) -> Result<Self, Error> {
29 let (name, _) = Name::more(string)?;
30 Ok(name)
31 }
32
33 pub fn to(&self) -> String {
34 let mut rtn = String::new();
35 rtn.push_str(self.sub_space.to().as_str());
36 rtn.push_str(":");
37 rtn.push_str(self.path.as_str());
38 return rtn;
39 }
40
41 pub fn as_name(&self) -> Self {
42 self.clone()
43 }
44}
45
46impl fmt::Display for Name {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 write!(f, "{}", self.to())
49 }
50}
51
52impl FromStr for Name {
53 type Err = Error;
54
55 fn from_str(s: &str) -> Result<Self, Self::Err> {
56 let (name, _) = Name::more(s)?;
57 Ok(name)
58 }
59}