diff --git a/crates/ruff/src/args.rs b/crates/ruff/src/args.rs index bbf6b5516c..a7b1077051 100644 --- a/crates/ruff/src/args.rs +++ b/crates/ruff/src/args.rs @@ -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::() { + Ok(table) => match Options::from_toml_table(table) { Ok(option) => { if option.extend.is_none() { return Ok(SingleConfigArgument::SettingsOverride(Arc::new(option))); diff --git a/crates/ruff_server/src/session/settings.rs b/crates/ruff_server/src/session/settings.rs index 70151a4114..9f8b5c9b23 100644 --- a/crates/ruff_server/src/session/settings.rs +++ b/crates/ruff_server/src/session/settings.rs @@ -114,7 +114,7 @@ impl TryFrom for ResolvedConfiguration { )), ClientConfiguration::Object(map) => { let _guard = ValueSourceGuard::new(ValueSource::Editor, false); - let options = toml::Table::try_from(map)?.try_into::()?; + let options = Options::from_toml_table(toml::Table::try_from(map)?)?; if options.extend.is_some() { Err(ResolvedConfigurationError::ExtendNotSupported) } else { diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index 10e96101e5..f28a960dad 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -516,6 +516,13 @@ pub struct Options { pub analyze: Option, } +impl Options { + /// Deserialize inline configuration in one crate, avoiding repeated code generation. + pub fn from_toml_table(table: toml::Table) -> Result { + table.try_into() + } +} + /// Configures how Ruff checks your code. /// /// Options specified in the `lint` section take precedence over the deprecated top-level settings. diff --git a/crates/ruff_workspace/src/pyproject.rs b/crates/ruff_workspace/src/pyproject.rs index e8b64d0f78..7e6c7c6766 100644 --- a/crates/ruff_workspace/src/pyproject.rs +++ b/crates/ruff_workspace/src/pyproject.rs @@ -33,9 +33,7 @@ pub struct Pyproject { project: Option, } -fn parse_toml, T: DeserializeOwned>(path: P, table_path: &[&str]) -> Result { - let path = path.as_ref(); - +fn parse_toml(path: &Path, table_path: &[&str]) -> Result { let _guard = ValueSourceGuard::new( ValueSource::File(Arc::new(SystemPathBuf::from_path_buf_lossy( path.to_path_buf(), @@ -66,18 +64,18 @@ fn parse_toml, T: DeserializeOwned>(path: P, table_path: &[&str]) } /// Parse a `ruff.toml` file. -fn parse_ruff_toml>(path: P) -> Result { +fn parse_ruff_toml(path: &Path) -> Result { parse_toml(path, &[]) } /// Parse a `pyproject.toml` file. -fn parse_pyproject_toml>(path: P) -> Result { +fn parse_pyproject_toml(path: &Path) -> Result { parse_toml(path, &["tool", "ruff"]) } /// Return `true` if a `pyproject.toml` contains a `[tool.ruff]` section. fn ruff_enabled>(path: P) -> Result { - 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")?