Skip to content

Commit

Permalink
Replace toml with toml_edit (#1680)
Browse files Browse the repository at this point in the history
The `toml` crate doesn't support TOML 1.0, but `toml_edit` does. While
there is a plan to [migrate `toml` to be on
`toml_edit`](toml-rs/toml#340), it's not ready
yet and it's very easy to switch back to `toml` when it's ready.
  • Loading branch information
messense authored Jan 6, 2023
1 parent fe67a0d commit 43575da
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
32 changes: 27 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ strum = { version = "0.24.1", features = ["strum_macros"] }
strum_macros = { version = "0.24.3" }
textwrap = { version = "0.16.0" }
titlecase = { version = "2.2.1" }
toml = { version = "0.5.9" }
toml_edit = { version = "0.17.1", features = ["easy"] }
walkdir = { version = "2.3.2" }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion flake8_to_ruff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde = { version = "1.0.147", features = ["derive"] }
serde_json = { version = "1.0.87" }
strum = { version = "0.24.1", features = ["strum_macros"] }
strum_macros = { version = "0.24.3" }
toml = { version = "0.5.9" }
toml_edit = { version = "0.17.1", features = ["easy"] }

[dev-dependencies]

Expand Down
2 changes: 1 addition & 1 deletion flake8_to_ruff/src/black.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Pyproject {

pub fn parse_black_options<P: AsRef<Path>>(path: P) -> Result<Option<Black>> {
let contents = std::fs::read_to_string(path)?;
Ok(toml::from_str::<Pyproject>(&contents)?
Ok(toml_edit::easy::from_str::<Pyproject>(&contents)?
.tool
.and_then(|tool| tool.black))
}
2 changes: 1 addition & 1 deletion flake8_to_ruff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() -> Result<()> {

// Create Ruff's pyproject.toml section.
let pyproject = converter::convert(&config, black.as_ref(), cli.plugin)?;
println!("{}", toml::to_string_pretty(&pyproject)?);
println!("{}", toml_edit::easy::to_string_pretty(&pyproject)?);

Ok(())
}
24 changes: 12 additions & 12 deletions src/settings/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ impl Pyproject {
/// Parse a `ruff.toml` file.
fn parse_ruff_toml<P: AsRef<Path>>(path: P) -> Result<Options> {
let contents = fs::read_file(path)?;
toml::from_str(&contents).map_err(Into::into)
toml_edit::easy::from_str(&contents).map_err(Into::into)
}

/// Parse a `pyproject.toml` file.
fn parse_pyproject_toml<P: AsRef<Path>>(path: P) -> Result<Pyproject> {
let contents = fs::read_file(path)?;
toml::from_str(&contents).map_err(Into::into)
toml_edit::easy::from_str(&contents).map_err(Into::into)
}

/// Return `true` if a `pyproject.toml` contains a `[tool.ruff]` section.
Expand Down Expand Up @@ -143,17 +143,17 @@ mod tests {

#[test]
fn deserialize() -> Result<()> {
let pyproject: Pyproject = toml::from_str(r#""#)?;
let pyproject: Pyproject = toml_edit::easy::from_str(r#""#)?;
assert_eq!(pyproject.tool, None);

let pyproject: Pyproject = toml::from_str(
let pyproject: Pyproject = toml_edit::easy::from_str(
r#"
[tool.black]
"#,
)?;
assert_eq!(pyproject.tool, Some(Tools { ruff: None }));

let pyproject: Pyproject = toml::from_str(
let pyproject: Pyproject = toml_edit::easy::from_str(
r#"
[tool.black]
[tool.ruff]
Expand Down Expand Up @@ -209,7 +209,7 @@ mod tests {
})
);

let pyproject: Pyproject = toml::from_str(
let pyproject: Pyproject = toml_edit::easy::from_str(
r#"
[tool.black]
[tool.ruff]
Expand Down Expand Up @@ -266,7 +266,7 @@ line-length = 79
})
);

let pyproject: Pyproject = toml::from_str(
let pyproject: Pyproject = toml_edit::easy::from_str(
r#"
[tool.black]
[tool.ruff]
Expand Down Expand Up @@ -323,7 +323,7 @@ exclude = ["foo.py"]
})
);

let pyproject: Pyproject = toml::from_str(
let pyproject: Pyproject = toml_edit::easy::from_str(
r#"
[tool.black]
[tool.ruff]
Expand Down Expand Up @@ -380,7 +380,7 @@ select = ["E501"]
})
);

let pyproject: Pyproject = toml::from_str(
let pyproject: Pyproject = toml_edit::easy::from_str(
r#"
[tool.black]
[tool.ruff]
Expand Down Expand Up @@ -438,7 +438,7 @@ ignore = ["E501"]
})
);

assert!(toml::from_str::<Pyproject>(
assert!(toml_edit::easy::from_str::<Pyproject>(
r#"
[tool.black]
[tool.ruff]
Expand All @@ -447,7 +447,7 @@ line_length = 79
)
.is_err());

assert!(toml::from_str::<Pyproject>(
assert!(toml_edit::easy::from_str::<Pyproject>(
r#"
[tool.black]
[tool.ruff]
Expand All @@ -456,7 +456,7 @@ select = ["E123"]
)
.is_err());

assert!(toml::from_str::<Pyproject>(
assert!(toml_edit::easy::from_str::<Pyproject>(
r#"
[tool.black]
[tool.ruff]
Expand Down

0 comments on commit 43575da

Please sign in to comment.