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(config): Fix TOML parsing of compression levels #18173

Merged
merged 1 commit into from
Aug 7, 2023
Merged
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
36 changes: 33 additions & 3 deletions src/sinks/util/buffer/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,21 @@ impl<'de> de::Deserialize<'de> for CompressionLevel {
where
E: de::Error,
{
Ok(CompressionLevel::Val(v as u32))
u32::try_from(v).map(CompressionLevel::Val).map_err(|err| {
de::Error::custom(format!(
"unsigned integer could not be converted to u32: {}",
err
))
})
}

fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
u32::try_from(v).map(CompressionLevel::Val).map_err(|err| {
de::Error::custom(format!("integer could not be converted to u32: {}", err))
})
}
}

Expand Down Expand Up @@ -490,7 +504,7 @@ mod test {
use super::{Compression, CompressionLevel};

#[test]
fn deserialization() {
fn deserialization_json() {
let fixtures_valid = [
(r#""none""#, Compression::None),
(r#""gzip""#, Compression::Gzip(CompressionLevel::default())),
Expand Down Expand Up @@ -545,7 +559,7 @@ mod test {
),
(
r#"{"algorithm": "gzip", "level": -1}"#,
r#"invalid type: integer `-1`, expected unsigned number or string at line 1 column 33"#,
r#"integer could not be converted to u32: out of range integral type conversion attempted at line 1 column 33"#,
),
(
r#"{"algorithm": "gzip", "level": "good"}"#,
Expand Down Expand Up @@ -575,6 +589,22 @@ mod test {
}
}

#[test]
fn deserialization_toml() {
let fixtures_valid = [
// TOML differs from YAML and JSON by always parsing integers as signed
(
r#"algorithm = "gzip"
level = 8"#,
Compression::Gzip(CompressionLevel::Val(8)),
),
];
for (sources, result) in fixtures_valid.iter() {
let deserialized: Result<Compression, _> = toml::from_str(sources);
assert_eq!(deserialized.expect("valid source"), *result);
}
}

#[test]
fn from_and_to_value() {
let fixtures_valid = [
Expand Down