Skip to content

Commit

Permalink
move serde to dev-dependency, work around with Json* types
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre committed Feb 26, 2025
1 parent 7329128 commit 62b43a7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
6 changes: 2 additions & 4 deletions crates/ruff_python_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,21 @@ bstr = { workspace = true }
compact_str = { workspace = true }
memchr = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true, optional = true }
static_assertions = { workspace = true }
unicode-ident = { workspace = true }
unicode_names2 = { workspace = true }
unicode-normalization = { workspace = true }

[dev-dependencies]
ruff_annotate_snippets = { workspace = true }
ruff_python_ast = { workspace = true, features = ["serde"] }
ruff_source_file = { workspace = true }

anyhow = { workspace = true }
insta = { workspace = true, features = ["glob"] }
serde = { workspace = true }
serde_json = { workspace = true }
walkdir = { workspace = true }

[features]
serde = ["dep:serde", "ruff_python_ast/serde"]

[lints]
workspace = true
5 changes: 0 additions & 5 deletions crates/ruff_python_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,6 @@ impl FusedIterator for TokenIterWithContext<'_> {}
///
/// The mode argument specifies in what way code must be parsed.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize),
serde(rename_all = "kebab-case")
)]
pub enum Mode {
/// The code consists of a sequence of statements.
Module,
Expand Down
12 changes: 0 additions & 12 deletions crates/ruff_python_parser/src/parser/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,10 @@ use crate::{AsMode, Mode};
/// let options = ParseOptions::from(PySourceType::Python);
/// ```
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize),
serde(rename_all = "kebab-case")
)]
pub struct ParseOptions {
/// Specify the mode in which the code will be parsed.
#[cfg_attr(feature = "serde", serde(default = "default_mode"))]
pub(crate) mode: Mode,
/// Target version for detecting version-related syntax errors.
#[cfg_attr(feature = "serde", serde(default))]
pub(crate) target_version: PythonVersion,
}

Expand Down Expand Up @@ -60,8 +53,3 @@ impl From<PySourceType> for ParseOptions {
}
}
}

#[cfg(feature = "serde")]
fn default_mode() -> Mode {
Mode::Module
}
36 changes: 35 additions & 1 deletion crates/ruff_python_parser/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,39 @@ fn test_invalid_syntax(input_path: &Path) {
});
}

/// Copy of [`ParseOptions`] for deriving [`Deserialize`] with serde as a dev-dependency.
#[derive(serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
struct JsonParseOptions {
#[serde(default)]
mode: JsonMode,
#[serde(default)]
target_version: PythonVersion,
}

/// Copy of [`Mode`] for deserialization.
#[derive(Default, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
enum JsonMode {
#[default]
Module,
Expression,
ParenthesizedExpression,
Ipython,
}

impl From<JsonParseOptions> for ParseOptions {
fn from(value: JsonParseOptions) -> Self {
let mode = match value.mode {
JsonMode::Module => Mode::Module,
JsonMode::Expression => Mode::Expression,
JsonMode::ParenthesizedExpression => Mode::ParenthesizedExpression,
JsonMode::Ipython => Mode::Ipython,
};
Self::from(mode).with_target_version(value.target_version)
}
}

/// Extract [`ParseOptions`] from an initial pragma line, if present.
///
/// For example,
Expand All @@ -171,7 +204,8 @@ fn test_invalid_syntax(input_path: &Path) {
fn extract_options(source: &str) -> Option<ParseOptions> {
let header = source.lines().next()?;
let (_label, options) = header.split_once("# parse_options: ")?;
serde_json::from_str(options.trim()).ok()
let options: Option<JsonParseOptions> = serde_json::from_str(options.trim()).ok();
options.map(ParseOptions::from)
}

// Test that is intentionally ignored by default.
Expand Down

0 comments on commit 62b43a7

Please sign in to comment.