diff --git a/src/model/instance.rs b/src/model/instance.rs index 91b20f5..571ce11 100644 --- a/src/model/instance.rs +++ b/src/model/instance.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use serde::{Deserialize, Serialize}; pub struct InstanceMeta { @@ -9,3 +11,17 @@ pub struct InstanceMeta { pub struct Instance { pub url: String, } + +impl ToString for Instance { + fn to_string(&self) -> String { + self.url.clone() + } +} + +impl FromStr for Instance { + type Err = (); + + fn from_str(s: &str) -> Result { + Ok(Self { url: s.to_string() }) + } +} diff --git a/src/model/repository.rs b/src/model/repository.rs index c0c5b36..13154fa 100644 --- a/src/model/repository.rs +++ b/src/model/repository.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use serde::{Deserialize, Serialize}; use super::{instance::Instance, user::User}; @@ -12,7 +14,31 @@ pub struct Repository { impl ToString for Repository { fn to_string(&self) -> String { - format!("{}/{}@{}", self.owner, self.name, self.instance) + format!( + "{}/{}@{}", + self.owner.to_string(), + self.name, + self.instance.to_string() + ) + } +} + +impl FromStr for Repository { + type Err = (); + + fn from_str(s: &str) -> Result { + let mut by_ampersand = s.split('@'); + let mut path_split = by_ampersand.next().unwrap().split('/'); + + let instance = Instance::from_str(by_ampersand.next().unwrap()).unwrap(); + let owner = User::from_str(path_split.next().unwrap()).unwrap(); + let name = path_split.next().unwrap().to_string(); + + Ok(Self { + instance, + owner, + name, + }) } } diff --git a/src/model/user.rs b/src/model/user.rs index aa93bc0..9d3dac7 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use serde::{Deserialize, Serialize}; use super::instance::Instance; @@ -13,3 +15,15 @@ impl ToString for User { format!("{}:{}", self.username, self.instance.url) } } + +impl FromStr for User { + type Err = (); + + fn from_str(s: &str) -> Result { + let mut colon_split = s.split(':'); + let username = colon_split.next().unwrap().to_string(); + let instance = Instance::from_str(colon_split.next().unwrap()).unwrap(); + + Ok(Self { username, instance }) + } +}