Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/ruff_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ impl FromStr for IndentStyle {
"tab" | "Tabs" => Ok(Self::Tab),
"space" | "Spaces" => Ok(Self::Space(IndentStyle::DEFAULT_SPACES)),
// TODO: replace this error with a diagnostic
_ => Err("Value not supported for IndentStyle"),
v => {
let v = v.strip_prefix("Spaces, size: ").unwrap_or(v);

u8::from_str(v)
.map(Self::Space)
.map_err(|_| "Value not supported for IndentStyle")
}
}
}
}
Expand Down
39 changes: 36 additions & 3 deletions crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ruff_formatter::printer::{LineEnding, PrinterOptions};
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
use ruff_python_ast::PySourceType;
use std::path::Path;
use std::str::FromStr;

#[derive(Clone, Debug)]
#[cfg_attr(
Expand All @@ -16,6 +17,7 @@ pub struct PyFormatOptions {
/// Specifies the indent style:
/// * Either a tab
/// * or a specific amount of spaces
#[cfg_attr(feature = "serde", serde(default = "default_indent_style"))]
indent_style: IndentStyle,

/// The preferred line width at which the formatter should wrap lines.
Expand All @@ -33,12 +35,16 @@ fn default_line_width() -> LineWidth {
LineWidth::try_from(88).unwrap()
}

fn default_indent_style() -> IndentStyle {
IndentStyle::Space(4)
}

impl Default for PyFormatOptions {
fn default() -> Self {
Self {
source_type: PySourceType::default(),
indent_style: IndentStyle::Space(4),
line_width: LineWidth::try_from(88).unwrap(),
indent_style: default_indent_style(),
line_width: default_line_width(),
quote_style: QuoteStyle::default(),
magic_trailing_comma: MagicTrailingComma::default(),
}
Expand Down Expand Up @@ -66,7 +72,8 @@ impl PyFormatOptions {
self.quote_style
}

pub fn with_quote_style(&mut self, style: QuoteStyle) -> &mut Self {
#[must_use]
pub fn with_quote_style(mut self, style: QuoteStyle) -> Self {
self.quote_style = style;
self
}
Expand Down Expand Up @@ -150,6 +157,19 @@ impl TryFrom<char> for QuoteStyle {
}
}

impl FromStr for QuoteStyle {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"\"" | "double" | "Double" => Ok(Self::Double),
"'" | "single" | "Single" => Ok(Self::Single),
// TODO: replace this error with a diagnostic
_ => Err("Value not supported for QuoteStyle"),
}
}
}

#[derive(Copy, Clone, Debug, Default)]
#[cfg_attr(
feature = "serde",
Expand All @@ -167,3 +187,16 @@ impl MagicTrailingComma {
matches!(self, Self::Respect)
}
}

impl FromStr for MagicTrailingComma {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"respect" | "Respect" => Ok(Self::Respect),
"ignore" | "Ignore" => Ok(Self::Ignore),
// TODO: replace this error with a diagnostic
_ => Err("Value not supported for MagicTrailingComma"),
}
}
}