From 28f902b3e01a4619e891deba14a4b0ad21ca576f Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 13 Oct 2021 18:11:19 -0700 Subject: implement ext id changes and editor username lookup --- fatcat-cli/src/specifier.rs | 121 +++++++++++++++++++++++++++++++++----------- 1 file changed, 91 insertions(+), 30 deletions(-) diff --git a/fatcat-cli/src/specifier.rs b/fatcat-cli/src/specifier.rs index c69abf8..22c7f1c 100644 --- a/fatcat-cli/src/specifier.rs +++ b/fatcat-cli/src/specifier.rs @@ -11,6 +11,7 @@ pub enum ReleaseLookupKey { PMCID, PMID, Arxiv, + Hdl, // TODO: the others } @@ -21,6 +22,7 @@ impl fmt::Display for ReleaseLookupKey { Self::PMCID => write!(f, "pmcid"), Self::PMID => write!(f, "pmid"), Self::Arxiv => write!(f, "arxiv"), + Self::Hdl => write!(f, "hdl"), } } } @@ -28,12 +30,18 @@ impl fmt::Display for ReleaseLookupKey { #[derive(Debug, PartialEq, Clone)] pub enum ContainerLookupKey { ISSNL, + ISSNE, + ISSNP, + ISSN, } impl fmt::Display for ContainerLookupKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::ISSNL => write!(f, "issnl"), + Self::ISSNE => write!(f, "issne"), + Self::ISSNP => write!(f, "issnp"), + Self::ISSN => write!(f, "issn"), } } } @@ -111,9 +119,7 @@ impl Specifier { ContainerLookup(_, _) => Ok(self.get_from_api(api_client, None, None)?.specifier()), CreatorLookup(_, _) => Ok(self.get_from_api(api_client, None, None)?.specifier()), FileLookup(_, _) => Ok(self.get_from_api(api_client, None, None)?.specifier()), - EditorUsername(_username) => Err(anyhow!( - "editor lookup by username isn't implemented in fatcat-server API yet, sorry" - )), + EditorUsername(_) => Ok(self.get_from_api(api_client, None, None)?.specifier()), } } @@ -142,7 +148,7 @@ impl Specifier { }, ReleaseLookup(ext_id, key) => { use ReleaseLookupKey::*; - let (doi, pmcid, pmid, arxiv) = ( + let (doi, pmcid, pmid, arxiv, hdl) = ( if let DOI = ext_id { Some(key.to_string()) } else { @@ -163,11 +169,16 @@ impl Specifier { } else { None }, + if let Hdl = ext_id { + Some(key.to_string()) + } else { + None + }, ); - // doi, wikidata, isbn13, pmid, pmcid, core, arxiv, jstor, ark, mag + // doi, wikidata, isbn13, pmid, pmcid, core, arxiv, jstor, ark, mag, hdl let result = api_client.rt.block_on(api_client.api.lookup_release( doi, None, None, pmid, pmcid, None, arxiv, None, None, None, None, None, None, - expand, hide, + hdl, expand, hide, ))?; match result { fatcat_openapi::LookupReleaseResponse::FoundEntity(model) => { @@ -214,13 +225,34 @@ impl Specifier { .with_context(|| format!("API GET failed: {:?}", self)), }, ContainerLookup(ext_id, key) => { - let result = api_client.rt.block_on(match ext_id { - ContainerLookupKey::ISSNL => { - api_client - .api - .lookup_container(Some(key.to_string()), None, expand, hide) - } - })?; + use ContainerLookupKey::*; + let (issnl, issnp, issne, issn) = ( + if let ISSNL = ext_id { + Some(key.to_string()) + } else { + None + }, + if let ISSNE = ext_id { + Some(key.to_string()) + } else { + None + }, + if let ISSNP = ext_id { + Some(key.to_string()) + } else { + None + }, + if let ISSN = ext_id { + Some(key.to_string()) + } else { + None + }, + ); + let result = api_client.rt.block_on( + api_client + .api + .lookup_container(issnl, issne, issnp, issn, None, expand, hide), + )?; match result { fatcat_openapi::LookupContainerResponse::FoundEntity(model) => { Ok(Box::new(model)) @@ -395,11 +427,20 @@ impl Specifier { resp => Err(anyhow!("{:?}", resp)) .with_context(|| format!("API GET failed: {:?}", self)), }, - EditorUsername(_username) => { - unimplemented!( - "editor lookup by username isn't implemented in fatcat-server API yet, sorry" - ) - } + EditorUsername(username) => match api_client + .rt + .block_on(api_client.api.lookup_editor(Some(username.to_string())))? + { + fatcat_openapi::LookupEditorResponse::Found(model) => Ok(Box::new(model)), + fatcat_openapi::LookupEditorResponse::BadRequest(err) => { + Err(anyhow!("Bad Request ({}): {}", err.error, err.message)) + } + fatcat_openapi::LookupEditorResponse::NotFound(err) => { + Err(anyhow!("Not Found: {}", err.message)) + } + resp => Err(anyhow!("{:?}", resp)) + .with_context(|| format!("API GET failed: {:?}", self)), + }, }; match ret { Ok(_) => ret, @@ -518,18 +559,18 @@ impl fmt::Display for Specifier { match self { Self::Release(fcid) => write!(f, "release_{}", fcid), Self::ReleaseLookup(prefix, val) => write!(f, "{}:{}", prefix, val), - Self::Work(fcid) => write!(f, "release_{}", fcid), - Self::Container(fcid) => write!(f, "release_{}", fcid), + Self::Work(fcid) => write!(f, "work_{}", fcid), + Self::Container(fcid) => write!(f, "container_{}", fcid), Self::ContainerLookup(prefix, val) => write!(f, "{}:{}", prefix, val), - Self::Creator(fcid) => write!(f, "release_{}", fcid), + Self::Creator(fcid) => write!(f, "creator_{}", fcid), Self::CreatorLookup(prefix, val) => write!(f, "{}:{}", prefix, val), - Self::File(fcid) => write!(f, "release_{}", fcid), + Self::File(fcid) => write!(f, "file_{}", fcid), Self::FileLookup(prefix, val) => write!(f, "{}:{}", prefix, val), - Self::FileSet(fcid) => write!(f, "release_{}", fcid), - Self::WebCapture(fcid) => write!(f, "release_{}", fcid), - Self::Editgroup(fcid) => write!(f, "release_{}", fcid), - Self::Editor(fcid) => write!(f, "release_{}", fcid), - Self::EditorUsername(username) => write!(f, "user:{}", username), + Self::FileSet(fcid) => write!(f, "fileset_{}", fcid), + Self::WebCapture(fcid) => write!(f, "webcapture_{}", fcid), + Self::Editgroup(fcid) => write!(f, "editgroup_{}", fcid), + Self::Editor(fcid) => write!(f, "editor_{}", fcid), + Self::EditorUsername(username) => write!(f, "username:{}", username), Self::Changelog(index) => write!(f, "changelog_{}", index), } } @@ -561,7 +602,7 @@ impl FromStr for Specifier { // then try lookup prefixes lazy_static! { static ref SPEC_LOOKUP_RE: Regex = - Regex::new(r"^(doi|pmcid|pmid|arxiv|issnl|orcid|sha1|sha256|md5|user):(\S+)$") + Regex::new(r"^(doi|pmcid|pmid|arxiv|hdl|issnl|issne|issnp|issn|orcid|sha1|sha256|md5|username|u):(\S+)$") .unwrap(); } if let Some(caps) = SPEC_LOOKUP_RE.captures(s) { @@ -582,10 +623,26 @@ impl FromStr for Specifier { ReleaseLookupKey::Arxiv, key.to_string(), )), + ("hdl", key) => Ok(Specifier::ReleaseLookup( + ReleaseLookupKey::Hdl, + key.to_string(), + )), ("issnl", key) => Ok(Specifier::ContainerLookup( ContainerLookupKey::ISSNL, key.to_string(), )), + ("issne", key) => Ok(Specifier::ContainerLookup( + ContainerLookupKey::ISSNE, + key.to_string(), + )), + ("issnp", key) => Ok(Specifier::ContainerLookup( + ContainerLookupKey::ISSNP, + key.to_string(), + )), + ("issn", key) => Ok(Specifier::ContainerLookup( + ContainerLookupKey::ISSN, + key.to_string(), + )), ("orcid", key) => Ok(Specifier::CreatorLookup( CreatorLookupKey::Orcid, key.to_string(), @@ -596,7 +653,7 @@ impl FromStr for Specifier { key.to_string(), )), ("md5", key) => Ok(Specifier::FileLookup(FileLookupKey::MD5, key.to_string())), - ("user", key) => Ok(Specifier::EditorUsername(key.to_string())), + ("username", key) | ("u", key) => Ok(Specifier::EditorUsername(key.to_string())), _ => Err(anyhow!("unexpected entity lookup type: {}", &caps[1])), }; } @@ -649,7 +706,11 @@ mod tests { Specifier::Creator("iimvc523xbhqlav6j3sbthuehu".to_string()) ); assert_eq!( - Specifier::from_str("user:big-bot").unwrap(), + Specifier::from_str("username:big-bot").unwrap(), + Specifier::EditorUsername("big-bot".to_string()) + ); + assert_eq!( + Specifier::from_str("u:big-bot").unwrap(), Specifier::EditorUsername("big-bot".to_string()) ); assert_eq!( -- cgit v1.2.3