diff --git a/src/rust/cli/cmn.rs b/src/rust/cli/cmn.rs index 6d290cd2cdb..a051b267dbd 100644 --- a/src/rust/cli/cmn.rs +++ b/src/rust/cli/cmn.rs @@ -8,11 +8,32 @@ use std::io; use std::fmt; use std::path::{Path, PathBuf}; use std::str::FromStr; - +use std::string::ToString; use std::io::{Write, Read, stdout}; use std::default::Default; +const FIELD_SEP: &'static str = "."; + +#[derive(Clone, Default)] +pub struct FieldCursor(Vec); + +impl ToString for FieldCursor { + fn to_string(&self) -> String { + String::new() + } +} + +impl FieldCursor { + pub fn set(&mut self, value: &str) -> Result<(), CLIError> { + Ok(()) + } + + pub fn num_fields(&self) -> usize { + self.0.len() + } +} + pub fn parse_kv_arg<'a>(kv: &'a str, err: &mut InvalidOptionsError) -> (&'a str, Option<&'a str>) { let mut add_err = || err.issues.push(CLIError::InvalidKeyValueSyntax(kv.to_string())); diff --git a/src/rust/lib.rs b/src/rust/lib.rs index e05f416047f..3893c42eed8 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -151,4 +151,44 @@ bar\r\n\ assert_eq!(r.0.first, 2); assert_eq!(r.0.last, 42); } +} + +#[cfg(test)] +mod test_cli { + use super::cli::cmn::*; + + use std::default::Default; + + #[test] + fn cursor() { + let mut c: FieldCursor = Default::default(); + + assert_eq!(c.to_string(), ""); + assert_eq!(c.num_fields(), 0); + assert!(c.set(".").is_ok()); + assert!(c.set("..").is_err()); + assert_eq!(c.num_fields(), 0); + + assert!(c.set("foo").is_ok()); + assert_eq!(c.to_string(), "foo"); + assert_eq!(c.num_fields(), 1); + assert!(c.set("..").is_ok()); + assert_eq!(c.num_fields(), 0); + assert_eq!(c.to_string(), ""); + + assert!(c.set("foo.").is_err()); + + assert!(c.set("foo.bar").is_ok()); + assert_eq!(c.num_fields(), 2); + assert_eq!(c.to_string(), "foo.bar"); + assert!(c.set("sub.level").is_ok()); + assert_eq!(c.num_fields(), 4); + assert_eq!(c.to_string(), "foo.bar.sub.level"); + assert!(c.set("...other").is_ok()); + assert_eq!(c.to_string(), "foo.bar.other"); + assert_eq!(c.num_fields(), 3); + assert!(c.set(".one.two.three...beer").is_ok()); + assert_eq!(c.num_fields(), 2); + assert_eq!(c.to_string(), "one.beer"); + } } \ No newline at end of file