Skip to content

Commit

Permalink
avoid parsing TOML twice
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonrothe committed Jan 22, 2023
1 parent 7f03ab5 commit 4127eb4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 39 deletions.
29 changes: 15 additions & 14 deletions flake8_to_ruff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
use configparser::ini::Ini;
use ruff::flake8_to_ruff;
use ruff::flake8_to_ruff::{self, ExternalConfig};

#[derive(Parser)]
#[command(
Expand Down Expand Up @@ -48,19 +48,20 @@ fn main() -> Result<()> {
let config = ini.load(cli.file).map_err(|msg| anyhow::anyhow!(msg))?;

// Read the pyproject.toml file.
let (black, isort) = match cli.pyproject {
Some(path) => {
let black = flake8_to_ruff::parse_black_options(&path)?;
let isort = flake8_to_ruff::parse_isort_options(&path)?;
(black, isort)
}
None => (None, None),
};

let external_config = flake8_to_ruff::ExternalConfig {
black: black.as_ref(),
isort: isort.as_ref(),
};
let pyproject = cli.pyproject.map(flake8_to_ruff::parse).transpose()?;
let external_config = pyproject
.as_ref()
.map(|pyproject| {
pyproject
.tool
.as_ref()
.map(|tool| ExternalConfig {
black: tool.black.as_ref(),
isort: tool.isort.as_ref(),
})
.unwrap_or_default()
})
.unwrap_or_default();

// Create Ruff's pyproject.toml section.
let pyproject = flake8_to_ruff::convert(&config, &external_config, cli.plugin)?;
Expand Down
11 changes: 0 additions & 11 deletions src/flake8_to_ruff/black.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//! Extract Black configuration settings from a pyproject.toml.

use std::path::Path;

use anyhow::Result;
use serde::{Deserialize, Serialize};

use super::pyproject::Pyproject;
use crate::settings::types::PythonVersion;

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
Expand All @@ -15,10 +11,3 @@ pub struct Black {
#[serde(alias = "target-version", alias = "target_version")]
pub target_version: Option<Vec<PythonVersion>>,
}

pub fn parse_black_options<P: AsRef<Path>>(path: P) -> Result<Option<Black>> {
let contents = std::fs::read_to_string(path)?;
Ok(toml_edit::easy::from_str::<Pyproject>(&contents)?
.tool
.and_then(|tool| tool.black))
}
12 changes: 0 additions & 12 deletions src/flake8_to_ruff/isort.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
//! Extract isort configuration settings from a pyproject.toml.

use std::path::Path;

use anyhow::Result;
use serde::{Deserialize, Serialize};

use super::pyproject::Pyproject;

/// The [isort configuration](https://pycqa.github.io/isort/docs/configuration/config_files.html).
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Isort {
#[serde(alias = "src-paths", alias = "src_paths")]
pub src_paths: Option<Vec<String>>,
}

pub fn parse_isort_options<P: AsRef<Path>>(path: P) -> Result<Option<Isort>> {
let contents = std::fs::read_to_string(path)?;
Ok(toml_edit::easy::from_str::<Pyproject>(&contents)?
.tool
.and_then(|tool| tool.isort))
}
3 changes: 1 addition & 2 deletions src/flake8_to_ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ mod parser;
mod plugin;
mod pyproject;

pub use black::parse_black_options;
pub use converter::convert;
pub use external_config::ExternalConfig;
pub use isort::parse_isort_options;
pub use plugin::Plugin;
pub use pyproject::parse;
9 changes: 9 additions & 0 deletions src/flake8_to_ruff/pyproject.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::path::Path;

use anyhow::Result;
use serde::{Deserialize, Serialize};

use super::black::Black;
Expand All @@ -13,3 +16,9 @@ pub struct Tools {
pub struct Pyproject {
pub tool: Option<Tools>,
}

pub fn parse<P: AsRef<Path>>(path: P) -> Result<Pyproject> {
let contents = std::fs::read_to_string(path)?;
let pyproject = toml_edit::easy::from_str::<Pyproject>(&contents)?;
Ok(pyproject)
}

0 comments on commit 4127eb4

Please sign in to comment.