Reduce configuration deserialization code size (#27924)

This commit is contained in:
Micha Reiser authored and GitHub committed 2026-08-20 13:41:48 +02:00
1 parent 564fb737a8
commit e0eb7b0763
4 files changed
+14 -9

No files matched your search

+1 -1
View File
@@ -1015,7 +1015,7 @@ impl TypedValueParser for ConfigArgumentParser {
let _guard = ValueSourceGuard::new(ValueSource::Cli, false);
let config_parse_error = match toml::Table::from_str(value) {
Ok(table) => match table.try_into::<Options>() {
Ok(table) => match Options::from_toml_table(table) {
Ok(option) => {
if option.extend.is_none() {
return Ok(SingleConfigArgument::SettingsOverride(Arc::new(option)));
+1 -1
View File
@@ -114,7 +114,7 @@ impl TryFrom<ClientConfiguration> for ResolvedConfiguration {
)),
ClientConfiguration::Object(map) => {
let _guard = ValueSourceGuard::new(ValueSource::Editor, false);
let options = toml::Table::try_from(map)?.try_into::<Options>()?;
let options = Options::from_toml_table(toml::Table::try_from(map)?)?;
if options.extend.is_some() {
Err(ResolvedConfigurationError::ExtendNotSupported)
} else {
+7
View File
@@ -516,6 +516,13 @@ pub struct Options {
pub analyze: Option<AnalyzeOptions>,
}
impl Options {
/// Deserialize inline configuration in one crate, avoiding repeated code generation.
pub fn from_toml_table(table: toml::Table) -> Result<Self, toml::de::Error> {
table.try_into()
}
}
/// Configures how Ruff checks your code.
///
/// Options specified in the `lint` section take precedence over the deprecated top-level settings.
+5 -7
View File
@@ -33,9 +33,7 @@ pub struct Pyproject {
project: Option<Project>,
}
fn parse_toml<P: AsRef<Path>, T: DeserializeOwned>(path: P, table_path: &[&str]) -> Result<T> {
let path = path.as_ref();
fn parse_toml<T: DeserializeOwned>(path: &Path, table_path: &[&str]) -> Result<T> {
let _guard = ValueSourceGuard::new(
ValueSource::File(Arc::new(SystemPathBuf::from_path_buf_lossy(
path.to_path_buf(),
@@ -66,18 +64,18 @@ fn parse_toml<P: AsRef<Path>, T: DeserializeOwned>(path: P, table_path: &[&str])
}
/// Parse a `ruff.toml` file.
fn parse_ruff_toml<P: AsRef<Path>>(path: P) -> Result<Options> {
fn parse_ruff_toml(path: &Path) -> Result<Options> {
parse_toml(path, &[])
}
/// Parse a `pyproject.toml` file.
fn parse_pyproject_toml<P: AsRef<Path>>(path: P) -> Result<Pyproject> {
fn parse_pyproject_toml(path: &Path) -> Result<Pyproject> {
parse_toml(path, &["tool", "ruff"])
}
/// Return `true` if a `pyproject.toml` contains a `[tool.ruff]` section.
fn ruff_enabled<P: AsRef<Path>>(path: P) -> Result<bool> {
let pyproject = parse_pyproject_toml(path)?;
let pyproject = parse_pyproject_toml(path.as_ref())?;
Ok(pyproject.tool.and_then(|tool| tool.ruff).is_some())
}
@@ -547,7 +545,7 @@ per-file-ignores = { "__init__.py" = ["F401"] }
let pyproject =
find_settings_toml(tempdir.path())?.context("Failed to find pyproject.toml")?;
let pyproject = parse_pyproject_toml(pyproject)?;
let pyproject = parse_pyproject_toml(&pyproject)?;
let config = pyproject
.tool
.context("Expected to find [tool] field")?