Skip to content

Commit 7aedbde

Browse files
committed
size-parser: Support also parser from u64
1 parent fc3de59 commit 7aedbde

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

size-parser/src/lib.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,10 @@ impl<'de> Deserialize<'de> for MemorySize {
257257
}
258258

259259
if deserializer.is_human_readable() {
260-
deserializer.deserialize_str(MemorySizeVisitor)
260+
// For human-readable formats like JSON, support both strings and numbers
261+
deserializer.deserialize_any(MemorySizeVisitor)
261262
} else {
263+
// For binary formats, expect u64
262264
deserializer.deserialize_u64(MemorySizeVisitor)
263265
}
264266
}
@@ -405,8 +407,10 @@ pub mod human_size {
405407
}
406408

407409
if deserializer.is_human_readable() {
408-
deserializer.deserialize_str(MemorySizeVisitor(std::marker::PhantomData))
410+
// For human-readable formats like JSON, support both strings and numbers
411+
deserializer.deserialize_any(MemorySizeVisitor(std::marker::PhantomData))
409412
} else {
413+
// For binary formats, expect u64
410414
deserializer.deserialize_u64(MemorySizeVisitor(std::marker::PhantomData))
411415
}
412416
}
@@ -515,11 +519,15 @@ mod tests {
515519
let json = serde_json::to_string(&size).unwrap();
516520
assert_eq!(json, "\"2G\"");
517521

518-
// Test deserialization
522+
// Test deserialization from string
519523
let deserialized: MemorySize = serde_json::from_str("\"1G\"").unwrap();
520524
assert_eq!(deserialized.bytes(), 1024 * 1024 * 1024);
521525

522-
// Test deserialization from various formats
526+
// Test deserialization from JSON number
527+
let from_number: MemorySize = serde_json::from_str("2147483648").unwrap();
528+
assert_eq!(from_number.bytes(), 2147483648);
529+
530+
// Test deserialization from various string formats
523531
let from_k: MemorySize = serde_json::from_str("\"512K\"").unwrap();
524532
assert_eq!(from_k.bytes(), 512 * 1024);
525533

@@ -592,4 +600,31 @@ mod tests {
592600
assert!(result.is_err());
593601
assert!(result.unwrap_err().to_string().contains("conversion error"));
594602
}
603+
604+
#[cfg(feature = "serde")]
605+
#[test]
606+
fn test_human_size_json_number_support() {
607+
use serde::{Deserialize, Serialize};
608+
609+
#[derive(Serialize, Deserialize, PartialEq, Debug)]
610+
struct Config {
611+
#[serde(with = "crate::human_size")]
612+
memory_size: u64,
613+
}
614+
615+
// Test deserialization from JSON string
616+
let from_string: Config = serde_json::from_str(r#"{"memory_size":"2G"}"#).unwrap();
617+
assert_eq!(from_string.memory_size, 2 * 1024 * 1024 * 1024);
618+
619+
// Test deserialization from JSON number
620+
let from_number: Config = serde_json::from_str(r#"{"memory_size":2147483648}"#).unwrap();
621+
assert_eq!(from_number.memory_size, 2147483648);
622+
623+
// Test that both produce the same result when the number matches the parsed string
624+
let gb_2 = 2u64 * 1024 * 1024 * 1024;
625+
let from_string_2g: Config = serde_json::from_str(r#"{"memory_size":"2G"}"#).unwrap();
626+
let from_number_2g: Config =
627+
serde_json::from_str(&format!(r#"{{"memory_size":{}}}"#, gb_2)).unwrap();
628+
assert_eq!(from_string_2g.memory_size, from_number_2g.memory_size);
629+
}
595630
}

0 commit comments

Comments
 (0)