L + Ratio
parent: tbd commit: 1f5f028
Showing 3 changed files with 57 insertions and 1 deletion
src/model/instance.rs
@@ -1,3 +1,5 @@ | ||
1 | use std::str::FromStr; | |
2 | ||
1 | 3 | use serde::{Deserialize, Serialize}; |
2 | 4 | |
3 | 5 | pub struct InstanceMeta { |
@@ -9,3 +11,17 @@ pub struct InstanceMeta { | ||
9 | 11 | pub struct Instance { |
10 | 12 | pub url: String, |
11 | 13 | } |
14 | ||
15 | impl ToString for Instance { | |
16 | fn to_string(&self) -> String { | |
17 | self.url.clone() | |
18 | } | |
19 | } | |
20 | ||
21 | impl FromStr for Instance { | |
22 | type Err = (); | |
23 | ||
24 | fn from_str(s: &str) -> Result<Self, Self::Err> { | |
25 | Ok(Self { url: s.to_string() }) | |
26 | } | |
27 | } |
src/model/repository.rs
@@ -1,3 +1,5 @@ | ||
1 | use std::str::FromStr; | |
2 | ||
1 | 3 | use serde::{Deserialize, Serialize}; |
2 | 4 | |
3 | 5 | use super::{instance::Instance, user::User}; |
@@ -12,7 +14,31 @@ pub struct Repository { | ||
12 | 14 | |
13 | 15 | impl ToString for Repository { |
14 | 16 | fn to_string(&self) -> String { |
15 | format!("{}/{}@{}", self.owner, self.name, self.instance) | |
17 | format!( | |
18 | "{}/{}@{}", | |
19 | self.owner.to_string(), | |
20 | self.name, | |
21 | self.instance.to_string() | |
22 | ) | |
23 | } | |
24 | } | |
25 | ||
26 | impl FromStr for Repository { | |
27 | type Err = (); | |
28 | ||
29 | fn from_str(s: &str) -> Result<Self, Self::Err> { | |
30 | let mut by_ampersand = s.split('@'); | |
31 | let mut path_split = by_ampersand.next().unwrap().split('/'); | |
32 | ||
33 | let instance = Instance::from_str(by_ampersand.next().unwrap()).unwrap(); | |
34 | let owner = User::from_str(path_split.next().unwrap()).unwrap(); | |
35 | let name = path_split.next().unwrap().to_string(); | |
36 | ||
37 | Ok(Self { | |
38 | instance, | |
39 | owner, | |
40 | name, | |
41 | }) | |
16 | 42 | } |
17 | 43 | } |
18 | 44 |
src/model/user.rs
@@ -1,3 +1,5 @@ | ||
1 | use std::str::FromStr; | |
2 | ||
1 | 3 | use serde::{Deserialize, Serialize}; |
2 | 4 | |
3 | 5 | use super::instance::Instance; |
@@ -13,3 +15,15 @@ impl ToString for User { | ||
13 | 15 | format!("{}:{}", self.username, self.instance.url) |
14 | 16 | } |
15 | 17 | } |
18 | ||
19 | impl FromStr for User { | |
20 | type Err = (); | |
21 | ||
22 | fn from_str(s: &str) -> Result<Self, Self::Err> { | |
23 | let mut colon_split = s.split(':'); | |
24 | let username = colon_split.next().unwrap().to_string(); | |
25 | let instance = Instance::from_str(colon_split.next().unwrap()).unwrap(); | |
26 | ||
27 | Ok(Self { username, instance }) | |
28 | } | |
29 | } |