Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix byte-unit #184 #185

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()))
);
}
}
22 changes: 16 additions & 6 deletions src/dns_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ impl RuntimeConfig {
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())
.unwrap_or_else(|| Byte::from_u64_with_unit(128, Unit::KB).unwrap())
.as_u64()
}
#[inline]
pub fn log_num(&self) -> u64 {
Expand Down Expand Up @@ -425,8 +425,8 @@ impl RuntimeConfig {
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())
.unwrap_or_else(|| Byte::from_u64_with_unit(128, Unit::KB).unwrap())
.as_u64()
}

#[inline]
Expand Down Expand Up @@ -1336,20 +1336,30 @@ mod parse {
);
}

#[test]
fn test_default_audit_size_1() {
use byte_unit::Unit;
let cfg = RuntimeConfig::builder().build();
assert_eq!(
cfg.audit_size(),
Byte::from_i64_with_unit(128, Unit::KB).unwrap().as_u64()
);
}

#[test]
fn test_parse_config_audit_size_1() {
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