Skip to content

Commit

Permalink
🐛 fix byte-unit #184
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyish committed Nov 29, 2023
1 parent fb2ae8c commit f19b323
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
19 changes: 11 additions & 8 deletions src/config/parser/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ impl NomParser for Byte {
fn parse(input: &str) -> IResult<&str, Self> {
let num = recognize(pair(digit1, opt(pair(char('.'), digit1))));
let unit = alpha1;
map_res(
recognize(tuple((num, space0, unit))),
<Byte as std::str::FromStr>::from_str,
)(input)
map_res(recognize(tuple((num, space0, unit))), |s| {
Byte::parse_str(s, true)
})(input)
}
}

Expand All @@ -20,19 +19,23 @@ mod tests {
use byte_unit::Unit;
assert_eq!(
Byte::parse("12kb"),
Ok(("", Byte::from_i64_with_unit(12, Unit::Kbit).unwrap()))
Ok(("", Byte::from_u64_with_unit(12, Unit::KB).unwrap()))
);
assert_eq!(
Byte::parse("123mb"),
Ok(("", Byte::from_i64_with_unit(123, Unit::Mbit).unwrap()))
Ok(("", Byte::from_u64_with_unit(123, Unit::MB).unwrap()))
);
assert_eq!(
Byte::parse("80mb"),
Ok(("", Byte::from_i64_with_unit(80, Unit::Mbit).unwrap()))
Ok(("", Byte::from_u64_with_unit(80, Unit::MB).unwrap()))
);
assert_eq!(
Byte::parse("30 gb"),
Ok(("", Byte::from_i64_with_unit(30, Unit::Gbit).unwrap()))
Ok(("", Byte::from_i64_with_unit(30, Unit::GB).unwrap()))
);
assert_eq!(
Byte::parse("30GB"),
Ok(("", Byte::from_i64_with_unit(30, Unit::GB).unwrap()))
);
}
}
16 changes: 4 additions & 12 deletions src/dns_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ impl RuntimeConfig {

#[inline]
pub fn log_size(&self) -> u64 {
use byte_unit::{Byte, Unit};
self.log
.size
.map(|n| n.as_u64())
.unwrap_or(Byte::from_i64_with_unit(128, Unit::KB).unwrap().as_u64())
self.log.size.map(|n| n.as_u64()).unwrap_or(128)
}
#[inline]
pub fn log_num(&self) -> u64 {
Expand Down Expand Up @@ -422,11 +418,7 @@ impl RuntimeConfig {

#[inline]
pub fn audit_size(&self) -> u64 {
use byte_unit::{Byte, Unit};
self.audit
.size
.map(|n| n.as_u64())
.unwrap_or(Byte::from_i64_with_unit(128, Unit::KB).unwrap().as_u64())
self.audit.size.map(|n| n.as_u64()).unwrap_or(128)
}

#[inline]
Expand Down Expand Up @@ -1341,15 +1333,15 @@ mod parse {
use byte_unit::Unit;
let mut cfg = RuntimeConfig::builder();
cfg.config("audit-size 80mb");
assert_eq!(cfg.audit.size, Byte::from_i64_with_unit(80, Unit::Mbit));
assert_eq!(cfg.audit.size, Byte::from_i64_with_unit(80, Unit::MB));
}

#[test]
fn test_parse_config_audit_size_2() {
use byte_unit::Unit;
let mut cfg = RuntimeConfig::builder();
cfg.config("audit-size 30 gb");
assert_eq!(cfg.audit.size, Byte::from_i64_with_unit(30, Unit::Gbit));
assert_eq!(cfg.audit.size, Byte::from_i64_with_unit(30, Unit::GB));
}

#[test]
Expand Down

0 comments on commit f19b323

Please sign in to comment.