diff options
Diffstat (limited to 'fatcat-cli/src/specifier.rs')
-rw-r--r-- | fatcat-cli/src/specifier.rs | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/fatcat-cli/src/specifier.rs b/fatcat-cli/src/specifier.rs index 0d8d209..65d095c 100644 --- a/fatcat-cli/src/specifier.rs +++ b/fatcat-cli/src/specifier.rs @@ -3,6 +3,7 @@ use anyhow::{anyhow, Context, Result}; use lazy_static::lazy_static; use regex::Regex; use std::str::FromStr; +use std::fmt; #[derive(Debug, PartialEq, Clone)] pub enum ReleaseLookupKey { @@ -13,16 +14,43 @@ pub enum ReleaseLookupKey { // TODO: the others } +impl fmt::Display for ReleaseLookupKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::DOI => write!(f, "doi"), + Self::PMCID => write!(f, "pmcid"), + Self::PMID => write!(f, "pmid"), + Self::Arxiv => write!(f, "arxiv"), + } + } +} + #[derive(Debug, PartialEq, Clone)] pub enum ContainerLookupKey { ISSNL, } +impl fmt::Display for ContainerLookupKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::ISSNL => write!(f, "issnl"), + } + } +} + #[derive(Debug, PartialEq, Clone)] pub enum CreatorLookupKey { Orcid, } +impl fmt::Display for CreatorLookupKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Orcid => write!(f, "orcid"), + } + } +} + #[derive(Debug, PartialEq, Clone)] pub enum FileLookupKey { SHA1, @@ -30,6 +58,16 @@ pub enum FileLookupKey { MD5, } +impl fmt::Display for FileLookupKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::SHA1=> write!(f, "sha1"), + Self::SHA256=> write!(f, "sha256"), + Self::MD5=> write!(f, "md5"), + } + } +} + #[derive(Debug, PartialEq, Clone)] pub enum Specifier { Release(String), @@ -475,6 +513,28 @@ impl Specifier { } } +impl fmt::Display for Specifier { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + 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::ContainerLookup(prefix, val) => write!(f, "{}:{}", prefix, val), + Self::Creator(fcid) => write!(f, "release_{}", fcid), + Self::CreatorLookup(prefix, val) => write!(f, "{}:{}", prefix, val), + Self::File(fcid) => write!(f, "release_{}", 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::Changelog(index) => write!(f, "changelog_{}", index), + } + } +} + impl FromStr for Specifier { type Err = anyhow::Error; @@ -501,7 +561,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|username|changelog):(\S+)$" + r"^(doi|pmcid|pmid|arxiv|issnl|orcid|sha1|sha256|md5|user):(\S+)$" ) .unwrap(); } @@ -537,7 +597,7 @@ impl FromStr for Specifier { key.to_string(), )), ("md5", key) => Ok(Specifier::FileLookup(FileLookupKey::MD5, key.to_string())), - ("username", key) => Ok(Specifier::EditorUsername(key.to_string())), + ("user", key) => Ok(Specifier::EditorUsername(key.to_string())), _ => Err(anyhow!("unexpected entity lookup type: {}", &caps[1])), }; } @@ -567,7 +627,7 @@ mod tests { Specifier::Creator("iimvc523xbhqlav6j3sbthuehu".to_string()) ); assert_eq!( - Specifier::from_str("username:big-bot").unwrap(), + Specifier::from_str("user:big-bot").unwrap(), Specifier::EditorUsername("big-bot".to_string()) ); assert_eq!( |