From 4046f018c347d7db1c08328537e9b52f5d839eb0 Mon Sep 17 00:00:00 2001 From: Teascade Date: Mon, 24 Aug 2020 20:05:34 +0300 Subject: [PATCH] Do some cleaning up --- src/api.rs | 12 +++++++++++- src/cmd.rs | 20 ++++++++++++-------- src/main.rs | 34 ++++++++++++++-------------------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/api.rs b/src/api.rs index 899c8c9..7afcf54 100644 --- a/src/api.rs +++ b/src/api.rs @@ -20,7 +20,7 @@ pub struct ErrorModel { pub message: Option, } -#[derive(Deserialize, Debug)] +#[derive(Deserialize, Debug, Clone)] pub struct TagModel { pub id: Option, pub imei: Option, @@ -44,6 +44,16 @@ impl TagModel { None } } + + pub fn get_id(&self) -> Result { + match &self.id { + Some(id) => Ok(id.to_string()), + None => Err(GenericError::MessagedError( + "Could not find device id. Error probably on Yetzon server side.".to_owned(), + None, + )), + } + } } impl Display for TagModel { diff --git a/src/cmd.rs b/src/cmd.rs index ef65da5..2186cd2 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -27,11 +27,11 @@ pub enum Subcommand { #[argh( subcommand, name = "between", - description = "fetch locations for a given section in time.\n nickname, or index from nick list.\nsince and until in format dd.mm.yyy, OR hh:mm:ss, OR dd.mm.yyy-hh:mm:ss" + description = "fetch locations for a given section in time.\n nickname, or index from nick list.\nsince and until in format dd.mm.yyy, OR hh:mm:ss, OR dd.mm.yyy-hh:mm:ss" )] pub struct BetweenOpt { - #[argh(positional, description = "tag to be fetched")] - pub tag: String, + #[argh(positional, description = "device to be fetched")] + pub device: String, // Device = tag, but in human speech #[argh( option, short = 's', @@ -51,7 +51,11 @@ pub struct BetweenOpt { pub struct InitOpt {} #[derive(FromArgs)] -#[argh(subcommand, name = "nick", description = "provides nicknaming to tags")] +#[argh( + subcommand, + name = "nick", + description = "provides nicknaming to devices" +)] pub struct NickOpt { #[argh(subcommand)] pub subcommand: NickSub, @@ -68,7 +72,7 @@ pub enum NickSub { #[argh( subcommand, name = "list", - description = "List all tags and their current nicknames" + description = "List all devices and their current nicknames" )] pub struct NickListOpt {} @@ -76,11 +80,11 @@ pub struct NickListOpt {} #[argh( subcommand, name = "set", - description = "Set nickname for given tag index. see index with nick list" + description = "Set nickname for given device index. see index with nick list" )] pub struct NickSetOpt { - #[argh(positional, description = "tag index, see index with nick list")] + #[argh(positional, description = "device index, see index with nick list")] pub index: i32, - #[argh(positional, description = "the new nickname for the tag")] + #[argh(positional, description = "the new nickname for the device")] pub nickname: String, } diff --git a/src/main.rs b/src/main.rs index 230368f..145ee6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> { None => None, }; - let locs = run(&config, opt.tag, since, until)?; + let locs = run(&config, opt.device, since, until)?; dbg!( &locs.iter().map(|loc| loc.0).collect::>(), locs.len(), @@ -75,13 +75,13 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> { Ok(()) } else { Err(GenericError::MessagedError( - format!("Tag with index {} does not have an id", opt.index), + format!("Device with index {} does not have an id", opt.index), None, )) } } else { Err(GenericError::MessagedError( - format!("Could not find tag with index {}", opt.index), + format!("Could not find device with index {}", opt.index), None, )) } @@ -100,15 +100,18 @@ fn run( let mut api = API::new(config.clone()); let tags = api.get_tags()?; - let tag = find_tag(tag_str, &tags, config)?; + let tag_id = find_tag(tag_str, &tags, config)?.get_id()?; - let state_list = api.get_states(&tag)?; + print!("Preparing..\r"); + std::io::stdout().lock().flush().ok(); + + let state_list = api.get_states(&tag_id)?; let states = API::get_between(&state_list, from, to, true, &config); let mut locations = Vec::new(); for (_, state) in states.iter() { - api.queue_location(&tag, state.id.as_ref().unwrap()); + api.queue_location(&tag_id, state.id.as_ref().unwrap()); } let receiver = api.begin_location_fetch(); let mut counter = 0; @@ -170,33 +173,24 @@ fn find_tag( tag_str: String, tags: &Vec, config: &Config, -) -> Result { +) -> Result { let mut tag = None; if let Ok(num) = tag_str.parse::() { if let Some(found) = tags.get((num - 1).max(0) as usize) { - tag = Some(&*found); + tag = Some(found.clone()); } } for curr in tags.iter() { if let Some(nick) = curr.get_nick(config) { if nick == tag_str { - tag = Some(curr); + tag = Some(curr.clone()); } } } match tag { - Some(t) => match &t.id { - Some(id) => Ok(id.to_string()), - None => Err(GenericError::MessagedError( - format!( - "Could not find tag {} id. Error probably on Yetzon server side.", - tag_str - ), - None, - )), - }, + Some(tag) => Ok(tag), None => Err(GenericError::MessagedError( - format!("Could not find tag {}", tag_str), + format!("Could not find device {}", tag_str), None, )), }