Skip to content

Commit 8618eaf

Browse files
jszwedkoneuronull
authored andcommitted
fix(config): Fix TOML parsing of compression levels (#18173)
TOML parses as as an i64. Signed-off-by: Jesse Szwedko <[email protected]>
1 parent cf9a49a commit 8618eaf

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/sinks/util/buffer/compression.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,21 @@ impl<'de> de::Deserialize<'de> for CompressionLevel {
431431
where
432432
E: de::Error,
433433
{
434-
Ok(CompressionLevel::Val(v as u32))
434+
u32::try_from(v).map(CompressionLevel::Val).map_err(|err| {
435+
de::Error::custom(format!(
436+
"unsigned integer could not be converted to u32: {}",
437+
err
438+
))
439+
})
440+
}
441+
442+
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
443+
where
444+
E: de::Error,
445+
{
446+
u32::try_from(v).map(CompressionLevel::Val).map_err(|err| {
447+
de::Error::custom(format!("integer could not be converted to u32: {}", err))
448+
})
435449
}
436450
}
437451

@@ -490,7 +504,7 @@ mod test {
490504
use super::{Compression, CompressionLevel};
491505

492506
#[test]
493-
fn deserialization() {
507+
fn deserialization_json() {
494508
let fixtures_valid = [
495509
(r#""none""#, Compression::None),
496510
(r#""gzip""#, Compression::Gzip(CompressionLevel::default())),
@@ -545,7 +559,7 @@ mod test {
545559
),
546560
(
547561
r#"{"algorithm": "gzip", "level": -1}"#,
548-
r#"invalid type: integer `-1`, expected unsigned number or string at line 1 column 33"#,
562+
r#"integer could not be converted to u32: out of range integral type conversion attempted at line 1 column 33"#,
549563
),
550564
(
551565
r#"{"algorithm": "gzip", "level": "good"}"#,
@@ -575,6 +589,22 @@ mod test {
575589
}
576590
}
577591

592+
#[test]
593+
fn deserialization_toml() {
594+
let fixtures_valid = [
595+
// TOML differs from YAML and JSON by always parsing integers as signed
596+
(
597+
r#"algorithm = "gzip"
598+
level = 8"#,
599+
Compression::Gzip(CompressionLevel::Val(8)),
600+
),
601+
];
602+
for (sources, result) in fixtures_valid.iter() {
603+
let deserialized: Result<Compression, _> = toml::from_str(sources);
604+
assert_eq!(deserialized.expect("valid source"), *result);
605+
}
606+
}
607+
578608
#[test]
579609
fn from_and_to_value() {
580610
let fixtures_valid = [

0 commit comments

Comments
 (0)