Move filesize to use bigint (#2984)

* Move filesize to be bigint-sized

* Add tests and fix filesize display

* clippy
This commit is contained in:
Jonathan Turner
2021-01-30 11:35:18 +13:00
committed by GitHub
parent 7b4cbd7ce9
commit 44e088c6fe
17 changed files with 227 additions and 148 deletions

View File

@ -631,8 +631,8 @@ impl Unit {
}
}
pub fn filesize(size_in_bytes: u64) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Filesize(size_in_bytes))
pub fn filesize(size_in_bytes: impl Into<BigInt>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Filesize(size_in_bytes.into()))
}
pub fn duration(nanos: BigInt) -> UntaggedValue {

View File

@ -186,7 +186,7 @@ impl UntaggedValue {
}
/// Helper for creating filesize values
pub fn filesize(s: impl Into<u64>) -> UntaggedValue {
pub fn filesize(s: impl Into<BigInt>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Filesize(s.into()))
}
@ -713,7 +713,7 @@ impl U64Ext for u64 {
fn to_filesize_value(&self, the_tag: Tag) -> Value {
Value {
value: UntaggedValue::Primitive(Primitive::Filesize(*self)),
value: UntaggedValue::Primitive(Primitive::Filesize(BigInt::from(*self))),
tag: the_tag,
}
}

View File

@ -31,7 +31,7 @@ pub enum Primitive {
#[serde(with = "serde_bigdecimal")]
Decimal(BigDecimal),
/// A count in the number of bytes, used as a filesize
Filesize(u64),
Filesize(BigInt),
/// A string value
String(String),
/// A path to travel to reach a value in a table
@ -254,17 +254,21 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
Primitive::EndOfStream => String::new(),
Primitive::FilePath(p) => format!("{}", p.display()),
Primitive::Filesize(num_bytes) => {
let byte = byte_unit::Byte::from_bytes(*num_bytes as u128);
if let Some(value) = num_bytes.to_u128() {
let byte = byte_unit::Byte::from_bytes(value);
if byte.get_bytes() == 0u128 {
return "".to_string();
}
if byte.get_bytes() == 0u128 {
return "".to_string();
}
let byte = byte.get_appropriate_unit(false);
let byte = byte.get_appropriate_unit(false);
match byte.get_unit() {
byte_unit::ByteUnit::B => format!("{} B ", byte.get_value()),
_ => byte.format(1),
match byte.get_unit() {
byte_unit::ByteUnit::B => format!("{} B ", byte.get_value()),
_ => byte.format(1),
}
} else {
format!("{} B", num_bytes)
}
}
Primitive::Duration(duration) => format_duration(duration),