mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-19 12:16:43 +00:00
Clearer error message when line-length goes beyond threshold (#21072)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
fdb8ea487c
commit
fa12fd0184
2 changed files with 29 additions and 2 deletions
|
|
@ -14,7 +14,7 @@ use ruff_text_size::TextSize;
|
|||
/// The length of a line of text that is considered too long.
|
||||
///
|
||||
/// The allowed range of values is 1..=320
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Serialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
pub struct LineLength(
|
||||
#[cfg_attr(feature = "schemars", schemars(range(min = 1, max = 320)))] NonZeroU16,
|
||||
|
|
@ -46,6 +46,21 @@ impl fmt::Display for LineLength {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for LineLength {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let value = u16::deserialize(deserializer)?;
|
||||
Self::try_from(value).map_err(|_| {
|
||||
serde::de::Error::custom(format!(
|
||||
"line-length must be between 1 and {} (got {value})",
|
||||
Self::MAX,
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl CacheKey for LineLength {
|
||||
fn cache_key(&self, state: &mut CacheKeyHasher) {
|
||||
state.write_u16(self.0.get());
|
||||
|
|
|
|||
|
|
@ -266,7 +266,6 @@ mod tests {
|
|||
use crate::pyproject::{Pyproject, Tools, find_settings_toml, parse_pyproject_toml};
|
||||
|
||||
#[test]
|
||||
|
||||
fn deserialize() -> Result<()> {
|
||||
let pyproject: Pyproject = toml::from_str(r"")?;
|
||||
assert_eq!(pyproject.tool, None);
|
||||
|
|
@ -456,6 +455,19 @@ other-attribute = 1
|
|||
.is_err()
|
||||
);
|
||||
|
||||
let invalid_line_length = toml::from_str::<Pyproject>(
|
||||
r"
|
||||
[tool.ruff]
|
||||
line-length = 500
|
||||
",
|
||||
)
|
||||
.expect_err("Deserialization should have failed for a too large line-length");
|
||||
|
||||
assert_eq!(
|
||||
invalid_line_length.message(),
|
||||
"line-length must be between 1 and 320 (got 500)"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue