diff --git a/src/main.rs b/src/main.rs index 51b7d89..96079e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ enum SousaCommands { Play, Pause, Search, + SwitchTo, } #[derive(Parser, Debug)] @@ -27,21 +28,19 @@ struct CliArgs { port: Option, /// The command to execute - #[arg(index = 1, value_enum)] + #[arg(index = 1, value_enum, requires("search_arg"))] action: Option, /// The string to search for when paired with a "Search" action - #[arg(index = 2, required_if_eq("action", "search"))] + #[arg(index = 2, requires("field"))] search_arg: Option, /// The field to search for when running `search` #[arg( - short, long, - required_if_eq("action", "search"), value_parser(["title", "artist", "album"]) )] - search_field: Option, + field: Option, // Add flag for "search filepaths too" } @@ -58,25 +57,21 @@ fn main() { SousaCommands::Play => serde_json::to_string(&UIRequest::Play).unwrap(), SousaCommands::Pause => serde_json::to_string(&UIRequest::Pause).unwrap(), SousaCommands::Search => { - let request: UIRequest = match cli.search_field.unwrap().to_lowercase().as_str() { - "title" => UIRequest::Search(PartialTag { - title: cli.search_arg, - ..PartialTag::default() - }), - "artist" => UIRequest::Search(PartialTag { - artist: cli.search_arg, - ..PartialTag::default() - }), - "album" => UIRequest::Search(PartialTag { - album: cli.search_arg, - ..PartialTag::default() - }), - _ => panic!( - "Unknown search type! Expected values are 'title', 'artist', and 'album'" + let request = match parse_to_partialtag(cli.field.unwrap(), cli.search_arg.unwrap()) { + Ok(tag) => UIRequest::Search(tag), + Err(_) => panic!( + "Unknown Search type! Expected values are 'title', 'artist', and 'album'" ), }; serde_json::to_string(&request).unwrap() } + SousaCommands::SwitchTo => { + let request = match parse_to_partialtag(cli.field.unwrap(), cli.search_arg.unwrap()) { + Ok(tag) => UIRequest::SwitchTo(tag), + Err(_) => panic!("Unknown type!"), + }; + serde_json::to_string(&request).unwrap() + } }; socket.write_message(Message::Text(message_string)).unwrap(); @@ -88,6 +83,24 @@ fn main() { socket.close(None).unwrap(); } +fn parse_to_partialtag(field: String, value: String) -> Result { + match field.to_lowercase().as_str() { + "title" => Ok(PartialTag { + title: Some(value), + ..PartialTag::default() + }), + "artist" => Ok(PartialTag { + artist: Some(value), + ..PartialTag::default() + }), + "album" => Ok(PartialTag { + album: Some(value), + ..PartialTag::default() + }), + _ => Err(()), + } +} + impl ServerResponse { fn pretty_print(self: &Self) -> () { let mut table = Table::new(vec![