[`configuration`] Fix unclear error messages for line-length values exceeding `u16::MAX` (#21329)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Dan Parizher 2025-11-10 13:29:35 -05:00 committed by GitHub
parent f63a9f2334
commit deeda56906
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 7 deletions

View File

@ -51,8 +51,12 @@ impl<'de> serde::Deserialize<'de> for LineLength {
where
D: serde::Deserializer<'de>,
{
let value = u16::deserialize(deserializer)?;
Self::try_from(value).map_err(|_| {
let value = i64::deserialize(deserializer)?;
u16::try_from(value)
.ok()
.and_then(|u16_value| Self::try_from(u16_value).ok())
.ok_or_else(|| {
serde::de::Error::custom(format!(
"line-length must be between 1 and {} (got {value})",
Self::MAX,

View File

@ -468,6 +468,62 @@ line-length = 500
"line-length must be between 1 and 320 (got 500)"
);
// Test value at u16::MAX boundary (65535) - should show range error
let invalid_line_length_65535 = toml::from_str::<Pyproject>(
r"
[tool.ruff]
line-length = 65535
",
)
.expect_err("Deserialization should have failed for line-length at u16::MAX");
assert_eq!(
invalid_line_length_65535.message(),
"line-length must be between 1 and 320 (got 65535)"
);
// Test value exceeding u16::MAX (65536) - should show clear error
let invalid_line_length_65536 = toml::from_str::<Pyproject>(
r"
[tool.ruff]
line-length = 65536
",
)
.expect_err("Deserialization should have failed for line-length exceeding u16::MAX");
assert_eq!(
invalid_line_length_65536.message(),
"line-length must be between 1 and 320 (got 65536)"
);
// Test value far exceeding u16::MAX (99_999) - should show clear error
let invalid_line_length_99999 = toml::from_str::<Pyproject>(
r"
[tool.ruff]
line-length = 99_999
",
)
.expect_err("Deserialization should have failed for line-length far exceeding u16::MAX");
assert_eq!(
invalid_line_length_99999.message(),
"line-length must be between 1 and 320 (got 99999)"
);
// Test negative value - should show clear error
let invalid_line_length_negative = toml::from_str::<Pyproject>(
r"
[tool.ruff]
line-length = -5
",
)
.expect_err("Deserialization should have failed for negative line-length");
assert_eq!(
invalid_line_length_negative.message(),
"line-length must be between 1 and 320 (got -5)"
);
Ok(())
}