diff --git a/lib/src/dbs/channel.rs b/lib/src/dbs/channel.rs index 38c00673..fcd579c3 100644 --- a/lib/src/dbs/channel.rs +++ b/lib/src/dbs/channel.rs @@ -70,24 +70,26 @@ impl Model { chn: &Sender<(Option, Value)>, ) -> Result<(), Error> { if ctx.is_ok() { - if let Some(c) = self.count { - for _ in 0..c { - Thing { - tb: self.table.to_string(), - id: Id::rand(), + match self { + Model::Count(tb, c) => { + for _ in 0..c { + Thing { + tb: tb.to_string(), + id: Id::rand(), + } + .process(ctx, opt, stm, txn, chn) + .await?; } - .process(ctx, opt, stm, txn, chn) - .await?; } - } - if let Some(r) = self.range { - for x in r.0..=r.1 { - Thing { - tb: self.table.to_string(), - id: Id::from(x), + Model::Range(tb, b, e) => { + for x in b..=e { + Thing { + tb: tb.to_string(), + id: Id::from(x), + } + .process(ctx, opt, stm, txn, chn) + .await?; } - .process(ctx, opt, stm, txn, chn) - .await?; } } } diff --git a/lib/src/dbs/iterate.rs b/lib/src/dbs/iterate.rs index 58a2c9eb..a30f77e3 100644 --- a/lib/src/dbs/iterate.rs +++ b/lib/src/dbs/iterate.rs @@ -69,24 +69,26 @@ impl Model { ite: &mut Iterator, ) -> Result<(), Error> { if ctx.is_ok() { - if let Some(c) = self.count { - for _ in 0..c { - Thing { - tb: self.table.to_string(), - id: Id::rand(), + match self { + Model::Count(tb, c) => { + for _ in 0..c { + Thing { + tb: tb.to_string(), + id: Id::rand(), + } + .iterate(ctx, opt, txn, ite) + .await?; } - .iterate(ctx, opt, txn, ite) - .await?; } - } - if let Some(r) = self.range { - for x in r.0..=r.1 { - Thing { - tb: self.table.to_string(), - id: Id::from(x), + Model::Range(tb, b, e) => { + for x in b..=e { + Thing { + tb: tb.to_string(), + id: Id::from(x), + } + .iterate(ctx, opt, txn, ite) + .await?; } - .iterate(ctx, opt, txn, ite) - .await?; } } } diff --git a/lib/src/sql/model.rs b/lib/src/sql/model.rs index fd2680dd..c16b1731 100644 --- a/lib/src/sql/model.rs +++ b/lib/src/sql/model.rs @@ -8,24 +8,24 @@ use nom::character::complete::char; use serde::{Deserialize, Serialize}; use std::fmt; -#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] -pub struct Model { - pub table: String, - pub count: Option, - pub range: Option<(u64, u64)>, +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] +pub enum Model { + Count(String, u64), + Range(String, u64, u64), } impl fmt::Display for Model { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if let Some(ref c) = self.count { - let t = escape(&self.table, &val_char, "`"); - write!(f, "|{}:{}|", t, c)?; + match self { + Model::Count(tb, c) => { + let t = escape(tb, &val_char, "`"); + write!(f, "|{}:{}|", t, c) + } + Model::Range(tb, b, e) => { + let t = escape(tb, &val_char, "`"); + write!(f, "|{}:{}..{}|", t, b, e) + } } - if let Some((ref b, ref e)) = self.range { - let t = escape(&self.table, &val_char, "`"); - write!(f, "|{}:{}..{}|", t, b, e)?; - } - Ok(()) } } @@ -39,14 +39,7 @@ fn model_count(i: &str) -> IResult<&str, Model> { let (i, _) = char(':')(i)?; let (i, c) = take_u64(i)?; let (i, _) = char('|')(i)?; - Ok(( - i, - Model { - table: t, - count: Some(c), - range: None, - }, - )) + Ok((i, Model::Count(t, c))) } fn model_range(i: &str) -> IResult<&str, Model> { @@ -58,14 +51,7 @@ fn model_range(i: &str) -> IResult<&str, Model> { let (i, _) = char('.')(i)?; let (i, e) = take_u64(i)?; let (i, _) = char('|')(i)?; - Ok(( - i, - Model { - table: t, - count: None, - range: Some((b, e)), - }, - )) + Ok((i, Model::Range(t, b, e))) } #[cfg(test)] @@ -80,14 +66,7 @@ mod tests { assert!(res.is_ok()); let out = res.unwrap().1; assert_eq!("|test:1000|", format!("{}", out)); - assert_eq!( - out, - Model { - table: String::from("test"), - count: Some(1000), - range: None, - } - ); + assert_eq!(out, Model::Count(String::from("test"), 1000)); } #[test] @@ -97,13 +76,6 @@ mod tests { assert!(res.is_ok()); let out = res.unwrap().1; assert_eq!("|test:1..1000|", format!("{}", out)); - assert_eq!( - out, - Model { - table: String::from("test"), - count: None, - range: Some((1, 1000)), - } - ); + assert_eq!(out, Model::Range(String::from("test"), 1, 1000)); } }