Skip to content

Commit

Permalink
Extend to OptionSet
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 16, 2024
1 parent a568ee5 commit dde82c5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
13 changes: 12 additions & 1 deletion crates/ruff/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ use ruff_workspace::options_base::OptionsMetadata;
#[allow(clippy::print_stdout)]
pub(crate) fn config(key: Option<&str>, format: HelpFormat) -> Result<()> {
match key {
None => print!("{}", Options::metadata()),
None => {
let metadata = Options::metadata();
match format {
HelpFormat::Text => {
println!("{metadata}");
}

HelpFormat::Json => {
println!("{}", &serde_json::to_string_pretty(&metadata)?);
}
}
}
Some(key) => match Options::metadata().find(key) {
None => {
return Err(anyhow!("Unknown option: {key}"));
Expand Down
48 changes: 43 additions & 5 deletions crates/ruff_workspace/src/options_base.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::Serialize;
use serde::{Serialize, Serializer};
use std::collections::BTreeMap;

use std::fmt::{Debug, Display, Formatter};

Expand Down Expand Up @@ -42,11 +43,12 @@ where

/// Metadata of an option that can either be a [`OptionField`] or [`OptionSet`].
#[derive(Clone, PartialEq, Eq, Debug, Serialize)]
#[serde(untagged)]
pub enum OptionEntry {
/// A single option.
Field(OptionField),

/// A set of options
/// A set of options.
Set(OptionSet),
}

Expand All @@ -63,11 +65,9 @@ impl Display for OptionEntry {
///
/// It extracts the options by calling the [`OptionsMetadata::record`] of a type implementing
/// [`OptionsMetadata`].
#[derive(Copy, Clone, Eq, PartialEq, Serialize)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct OptionSet {
#[serde(skip_serializing)]
record: fn(&mut dyn Visit),
#[serde(skip_serializing)]
doc: fn() -> Option<&'static str>,
}

Expand Down Expand Up @@ -329,6 +329,44 @@ impl Display for OptionSet {
}
}

struct SerializeVisitor<'a> {
entries: &'a mut BTreeMap<String, OptionField>,
}

impl<'a> Visit for SerializeVisitor<'a> {
fn record_set(&mut self, name: &str, set: OptionSet) {
// Collect the entries of the set.
let mut entries = BTreeMap::new();
let mut visitor = SerializeVisitor {
entries: &mut entries,
};
set.record(&mut visitor);

// Insert the set into the entries.
for (key, value) in entries {
self.entries.insert(format!("{name}.{key}"), value);
}
}

fn record_field(&mut self, name: &str, field: OptionField) {
self.entries.insert(name.to_string(), field);
}
}

impl Serialize for OptionSet {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut entries = BTreeMap::new();
let mut visitor = SerializeVisitor {
entries: &mut entries,
};
self.record(&mut visitor);
entries.serialize(serializer)
}
}

impl Debug for OptionSet {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
Expand Down

0 comments on commit dde82c5

Please sign in to comment.