diff options
Diffstat (limited to 'rust/fatcat-cli/src/entities.rs')
-rw-r--r-- | rust/fatcat-cli/src/entities.rs | 211 |
1 files changed, 151 insertions, 60 deletions
diff --git a/rust/fatcat-cli/src/entities.rs b/rust/fatcat-cli/src/entities.rs index c606d17..eee3946 100644 --- a/rust/fatcat-cli/src/entities.rs +++ b/rust/fatcat-cli/src/entities.rs @@ -1,11 +1,9 @@ - -use std::str::FromStr; -use anyhow::{Result, anyhow}; +use crate::Specifier; +use anyhow::{anyhow, Result}; +use fatcat_openapi::models; use lazy_static::lazy_static; use regex::Regex; -use fatcat_openapi::models; -use crate::Specifier; - +use std::str::FromStr; #[derive(Debug, PartialEq, Clone)] pub struct Mutation { @@ -22,7 +20,6 @@ impl FromStr for Mutation { static ref MUTATE_ENTITY_RE: Regex = Regex::new(r"^([a-z_]+)=(.*)$").unwrap(); } if let Some(captures) = MUTATE_ENTITY_RE.captures(s) { - // XXX: Some() vs None for value return Ok(Mutation { field: captures[1].to_string(), value: match &captures[2] { @@ -48,8 +45,7 @@ impl FromStr for Mutation { * - get by specifier */ -pub trait ApiEntityModel: ApiModelSer+ApiModelIdent+ApiModelMutate { -} +pub trait ApiEntityModel: ApiModelSer + ApiModelIdent + ApiModelMutate {} impl ApiEntityModel for models::ReleaseEntity {} impl ApiEntityModel for models::ContainerEntity {} @@ -58,9 +54,9 @@ impl ApiEntityModel for models::WorkEntity {} impl ApiEntityModel for models::FileEntity {} impl ApiEntityModel for models::FilesetEntity {} impl ApiEntityModel for models::WebcaptureEntity {} -impl ApiEntityModel for models::Editor{} -impl ApiEntityModel for models::Editgroup{} -impl ApiEntityModel for models::ChangelogEntry{} +impl ApiEntityModel for models::Editor {} +impl ApiEntityModel for models::Editgroup {} +impl ApiEntityModel for models::ChangelogEntry {} pub trait ApiModelSer { fn to_json_string(&self) -> Result<String>; @@ -68,7 +64,6 @@ pub trait ApiModelSer { } impl<T: serde::Serialize> ApiModelSer for T { - fn to_json_string(&self) -> Result<String> { Ok(serde_json::to_string(self)?) } @@ -85,20 +80,38 @@ pub trait ApiModelIdent { macro_rules! generic_entity_specifier { ($specifier_type:ident) => { fn specifier(&self) -> Specifier { - if let Some(fcid) = &self.ident { Specifier::$specifier_type(fcid.to_string()) } else { panic!("expected full entity") } + if let Some(fcid) = &self.ident { + Specifier::$specifier_type(fcid.to_string()) + } else { + panic!("expected full entity") + } } - } + }; } -impl ApiModelIdent for models::ReleaseEntity { generic_entity_specifier!(Release); } -impl ApiModelIdent for models::ContainerEntity { generic_entity_specifier!(Container); } -impl ApiModelIdent for models::CreatorEntity { generic_entity_specifier!(Creator); } -impl ApiModelIdent for models::WorkEntity { generic_entity_specifier!(Work); } -impl ApiModelIdent for models::FileEntity { generic_entity_specifier!(File); } -impl ApiModelIdent for models::FilesetEntity { generic_entity_specifier!(FileSet); } -impl ApiModelIdent for models::WebcaptureEntity { generic_entity_specifier!(WebCapture); } +impl ApiModelIdent for models::ReleaseEntity { + generic_entity_specifier!(Release); +} +impl ApiModelIdent for models::ContainerEntity { + generic_entity_specifier!(Container); +} +impl ApiModelIdent for models::CreatorEntity { + generic_entity_specifier!(Creator); +} +impl ApiModelIdent for models::WorkEntity { + generic_entity_specifier!(Work); +} +impl ApiModelIdent for models::FileEntity { + generic_entity_specifier!(File); +} +impl ApiModelIdent for models::FilesetEntity { + generic_entity_specifier!(FileSet); +} +impl ApiModelIdent for models::WebcaptureEntity { + generic_entity_specifier!(WebCapture); +} -impl ApiModelIdent for models::ChangelogEntry{ +impl ApiModelIdent for models::ChangelogEntry { fn specifier(&self) -> Specifier { Specifier::Changelog(self.index) } @@ -106,13 +119,21 @@ impl ApiModelIdent for models::ChangelogEntry{ impl ApiModelIdent for models::Editgroup { fn specifier(&self) -> Specifier { - if let Some(fcid) = &self.editgroup_id { Specifier::Editgroup(fcid.to_string()) } else { panic!("expected full entity") } + if let Some(fcid) = &self.editgroup_id { + Specifier::Editgroup(fcid.to_string()) + } else { + panic!("expected full entity") + } } } impl ApiModelIdent for models::Editor { fn specifier(&self) -> Specifier { - if let Some(fcid) = &self.editor_id { Specifier::Editor(fcid.to_string()) } else { panic!("expected full entity") } + if let Some(fcid) = &self.editor_id { + Specifier::Editor(fcid.to_string()) + } else { + panic!("expected full entity") + } } } @@ -124,19 +145,45 @@ impl ApiModelMutate for models::ReleaseEntity { fn mutate(&mut self, mutations: Vec<Mutation>) -> Result<()> { for m in mutations { match (m.field.as_str(), m.value) { - ("title", val) => { self.title = val; }, - ("subtitle", val) => { self.subtitle = val; }, - ("container_id", val) => { self.container_id = val; }, - ("work_id", val) => { self.work_id = val; }, - ("release_type", val) => { self.release_type = val; }, - ("release_stage", val) => { self.release_stage = val; }, - ("withdrawn_status", val) => { self.withdrawn_status = val; }, - ("license_slug", val) => { self.license_slug= val; }, - ("volume", val) => { self.volume = val; }, - ("issue", val) => { self.issue = val; }, - ("number", val) => { self.number = val; }, - ("publisher", val) => { self.publisher = val; }, - ("language", val) => { self.language = val; }, + ("title", val) => { + self.title = val; + } + ("subtitle", val) => { + self.subtitle = val; + } + ("container_id", val) => { + self.container_id = val; + } + ("work_id", val) => { + self.work_id = val; + } + ("release_type", val) => { + self.release_type = val; + } + ("release_stage", val) => { + self.release_stage = val; + } + ("withdrawn_status", val) => { + self.withdrawn_status = val; + } + ("license_slug", val) => { + self.license_slug = val; + } + ("volume", val) => { + self.volume = val; + } + ("issue", val) => { + self.issue = val; + } + ("number", val) => { + self.number = val; + } + ("publisher", val) => { + self.publisher = val; + } + ("language", val) => { + self.language = val; + } (field, _) => unimplemented!("setting field {} on a release", field), } } @@ -148,10 +195,18 @@ impl ApiModelMutate for models::ContainerEntity { fn mutate(&mut self, mutations: Vec<Mutation>) -> Result<()> { for m in mutations { match (m.field.as_str(), m.value) { - ("name", val) => { self.name = val; }, - ("container_type", val) => { self.container_type = val; }, - ("publisher", val) => { self.publisher = val; }, - ("issnl", val) => { self.issnl = val; }, + ("name", val) => { + self.name = val; + } + ("container_type", val) => { + self.container_type = val; + } + ("publisher", val) => { + self.publisher = val; + } + ("issnl", val) => { + self.issnl = val; + } (field, _) => unimplemented!("setting field {} on a container", field), } } @@ -163,9 +218,15 @@ impl ApiModelMutate for models::CreatorEntity { fn mutate(&mut self, mutations: Vec<Mutation>) -> Result<()> { for m in mutations { match (m.field.as_str(), m.value) { - ("display_name", val) => { self.display_name = val; }, - ("given_name", val) => { self.given_name = val; }, - ("surname", val) => { self.surname = val; }, + ("display_name", val) => { + self.display_name = val; + } + ("given_name", val) => { + self.given_name = val; + } + ("surname", val) => { + self.surname = val; + } (field, _) => unimplemented!("setting field {} on a creator", field), } } @@ -183,12 +244,24 @@ impl ApiModelMutate for models::FileEntity { fn mutate(&mut self, mutations: Vec<Mutation>) -> Result<()> { for m in mutations { match (m.field.as_str(), m.value) { - ("size", Some(val)) => { self.size = Some(i64::from_str(&val)?); }, - ("size", None) => { self.size = None; }, - ("md5", val) => { self.md5 = val; }, - ("sha1", val) => { self.sha1 = val; }, - ("sha256", val) => { self.sha256 = val; }, - ("mimetype", val) => { self.mimetype = val; }, + ("size", Some(val)) => { + self.size = Some(i64::from_str(&val)?); + } + ("size", None) => { + self.size = None; + } + ("md5", val) => { + self.md5 = val; + } + ("sha1", val) => { + self.sha1 = val; + } + ("sha256", val) => { + self.sha256 = val; + } + ("mimetype", val) => { + self.mimetype = val; + } (field, _) => unimplemented!("setting field {} on a file", field), } } @@ -212,7 +285,9 @@ impl ApiModelMutate for models::Editor { fn mutate(&mut self, mutations: Vec<Mutation>) -> Result<()> { for m in mutations { match (m.field.as_str(), m.value) { - ("username", Some(val)) => { self.username = val; }, + ("username", Some(val)) => { + self.username = val; + } (field, _) => unimplemented!("setting field {} on an editor", field), } } @@ -224,7 +299,9 @@ impl ApiModelMutate for models::Editgroup { fn mutate(&mut self, mutations: Vec<Mutation>) -> Result<()> { for m in mutations { match (m.field.as_str(), m.value) { - ("description", val) => { self.description = val; }, + ("description", val) => { + self.description = val; + } (field, _) => unimplemented!("setting field {} on an editgroup", field), } } @@ -245,12 +322,26 @@ mod tests { #[test] fn test_mutation_from_str() -> () { assert!(Mutation::from_str("release_asdf").is_err()); - assert_eq!(Mutation::from_str("title=blah").unwrap(), - Mutation { field: "title".to_string(), value: Some("blah".to_string()) }); - assert_eq!(Mutation::from_str("title=").unwrap(), - Mutation { field: "title".to_string(), value: None }); - assert_eq!(Mutation::from_str("title=string with spaces and stuff").unwrap(), - Mutation { field: "title".to_string(), value: Some("string with spaces and stuff".to_string()) }); + assert_eq!( + Mutation::from_str("title=blah").unwrap(), + Mutation { + field: "title".to_string(), + value: Some("blah".to_string()) + } + ); + assert_eq!( + Mutation::from_str("title=").unwrap(), + Mutation { + field: "title".to_string(), + value: None + } + ); + assert_eq!( + Mutation::from_str("title=string with spaces and stuff").unwrap(), + Mutation { + field: "title".to_string(), + value: Some("string with spaces and stuff".to_string()) + } + ); } - } |