From 658401529378bf7bdc6747ae1745d76732fd51f9 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Sat, 13 Feb 2021 20:38:55 -0800 Subject: allow editgroup_id to be supplied with 'editgroup_' prefix --- fatcat-cli/src/lib.rs | 2 +- fatcat-cli/src/main.rs | 28 ++++++++++++++-------------- fatcat-cli/src/specifier.rs | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/fatcat-cli/src/lib.rs b/fatcat-cli/src/lib.rs index 1657d8d..beb9c97 100644 --- a/fatcat-cli/src/lib.rs +++ b/fatcat-cli/src/lib.rs @@ -23,7 +23,7 @@ pub use entities::{ Mutation, }; pub use search::{crude_search, SearchResults}; -pub use specifier::Specifier; +pub use specifier::{Specifier, EditgroupSpecifier}; #[derive(Debug, PartialEq, Clone, Copy)] pub enum EntityType { diff --git a/fatcat-cli/src/main.rs b/fatcat-cli/src/main.rs index fd04044..b8b3865 100644 --- a/fatcat-cli/src/main.rs +++ b/fatcat-cli/src/main.rs @@ -83,17 +83,17 @@ enum EditgroupsCommand { /// Accept (merge) a single editgroup Accept { #[structopt(env = "FATCAT_EDITGROUP", hide_env_values = true)] - editgroup_id: String, + editgroup_id: EditgroupSpecifier, }, /// Submit a single editgroup for review Submit { #[structopt(env = "FATCAT_EDITGROUP", hide_env_values = true)] - editgroup_id: String, + editgroup_id: EditgroupSpecifier, }, /// Un-submit a single editgroup (for more editing) Unsubmit { #[structopt(env = "FATCAT_EDITGROUP", hide_env_values = true)] - editgroup_id: String, + editgroup_id: EditgroupSpecifier, }, } @@ -177,7 +177,7 @@ enum Command { env = "FATCAT_EDITGROUP", hide_env_values = true )] - editgroup_id: String, + editgroup_id: EditgroupSpecifier, }, /// Update an existing editgroup, either from file or updating specified fields @@ -194,7 +194,7 @@ enum Command { env = "FATCAT_EDITGROUP", hide_env_values = true )] - editgroup_id: String, + editgroup_id: EditgroupSpecifier, mutations: Vec, }, @@ -209,7 +209,7 @@ enum Command { env = "FATCAT_EDITGROUP", hide_env_values = true )] - editgroup_id: String, + editgroup_id: EditgroupSpecifier, }, /// Use a text editor to update entity; fetches and uploads automatically @@ -222,7 +222,7 @@ enum Command { env = "FATCAT_EDITGROUP", hide_env_values = true )] - editgroup_id: String, + editgroup_id: EditgroupSpecifier, #[structopt(long)] json: bool, @@ -396,7 +396,7 @@ fn run(opt: Opt) -> Result<()> { editgroup_id, } => { let json_str = read_entity_file(input_path)?; - let ee = api_client.create_entity_from_json(entity_type, &json_str, editgroup_id)?; + let ee = api_client.create_entity_from_json(entity_type, &json_str, editgroup_id.as_string())?; writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&ee)?)?)? } Command::Update { @@ -420,7 +420,7 @@ fn run(opt: Opt) -> Result<()> { } }; let ee = - api_client.update_entity_from_json(exact_specifier, &json_str, editgroup_id)?; + api_client.update_entity_from_json(exact_specifier, &json_str, editgroup_id.as_string())?; writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&ee)?)?)? } Command::Edit { @@ -433,7 +433,7 @@ fn run(opt: Opt) -> Result<()> { let ee = edit_entity_locally( &mut api_client, specifier, - editgroup_id, + editgroup_id.as_string(), json, editing_command, )?; @@ -645,7 +645,7 @@ fn run(opt: Opt) -> Result<()> { editgroup_id, } => { let result = api_client - .delete_entity(specifier.clone(), editgroup_id) + .delete_entity(specifier.clone(), editgroup_id.as_string()) .with_context(|| format!("delete entity: {:?}", specifier))?; println!("{}", serde_json::to_string(&result)?); } @@ -721,19 +721,19 @@ fn run(opt: Opt) -> Result<()> { Command::Editgroups { cmd: EditgroupsCommand::Accept { editgroup_id }, } => { - let msg = api_client.accept_editgroup(editgroup_id.clone())?; + let msg = api_client.accept_editgroup(editgroup_id.as_string())?; writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)? } Command::Editgroups { cmd: EditgroupsCommand::Submit { editgroup_id }, } => { - let msg = api_client.update_editgroup_submit(editgroup_id, true)?; + let msg = api_client.update_editgroup_submit(editgroup_id.as_string(), true)?; writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)? } Command::Editgroups { cmd: EditgroupsCommand::Unsubmit { editgroup_id }, } => { - let msg = api_client.update_editgroup_submit(editgroup_id, false)?; + let msg = api_client.update_editgroup_submit(editgroup_id.as_string(), false)?; writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)? } Command::Status { json } => { diff --git a/fatcat-cli/src/specifier.rs b/fatcat-cli/src/specifier.rs index 65d095c..cac4282 100644 --- a/fatcat-cli/src/specifier.rs +++ b/fatcat-cli/src/specifier.rs @@ -615,6 +615,32 @@ impl FromStr for Specifier { } } +pub struct EditgroupSpecifier(String); + +impl EditgroupSpecifier { + + pub fn as_string(self) -> String { + self.0 + } +} + +impl FromStr for EditgroupSpecifier { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + lazy_static! { + static ref SPEC_ENTITY_RE: Regex = Regex::new(r"^(editgroup_)?([2-7a-z]{26})$").unwrap(); + } + if let Some(caps) = SPEC_ENTITY_RE.captures(s) { + return Ok(EditgroupSpecifier(caps[2].to_string())) + } + Err(anyhow!( + "expecting an editgroup identifier, got: {}", + s + )) + } +} + #[cfg(test)] mod tests { use super::*; @@ -641,4 +667,17 @@ mod tests { ); assert!(Specifier::from_str("changelog_12E4").is_err()); } + + #[test] + fn test_editgroup_from_str() -> () { + assert!(EditgroupSpecifier::from_str("release_asdf").is_err()); + assert_eq!( + EditgroupSpecifier::from_str("editgroup_iimvc523xbhqlav6j3sbthuehu").unwrap().0, + "iimvc523xbhqlav6j3sbthuehu".to_string() + ); + assert_eq!( + EditgroupSpecifier::from_str("iimvc523xbhqlav6j3sbthuehu").unwrap().0, + "iimvc523xbhqlav6j3sbthuehu".to_string() + ); + } } -- cgit v1.2.3