mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-20 02:20:25 +00:00
Add "preserve" quote-style to mimic Black's skip-string-normalization (#8822)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
6bbabceead
commit
2414298289
10 changed files with 482 additions and 96 deletions
|
@ -207,35 +207,7 @@ pub enum QuoteStyle {
|
|||
Single,
|
||||
#[default]
|
||||
Double,
|
||||
}
|
||||
|
||||
impl QuoteStyle {
|
||||
pub const fn as_char(self) -> char {
|
||||
match self {
|
||||
QuoteStyle::Single => '\'',
|
||||
QuoteStyle::Double => '"',
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn invert(self) -> QuoteStyle {
|
||||
match self {
|
||||
QuoteStyle::Single => QuoteStyle::Double,
|
||||
QuoteStyle::Double => QuoteStyle::Single,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<char> for QuoteStyle {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: char) -> std::result::Result<Self, Self::Error> {
|
||||
match value {
|
||||
'\'' => Ok(QuoteStyle::Single),
|
||||
'"' => Ok(QuoteStyle::Double),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
Preserve,
|
||||
}
|
||||
|
||||
impl FromStr for QuoteStyle {
|
||||
|
@ -245,6 +217,7 @@ impl FromStr for QuoteStyle {
|
|||
match s {
|
||||
"\"" | "double" | "Double" => Ok(Self::Double),
|
||||
"'" | "single" | "Single" => Ok(Self::Single),
|
||||
"preserve" | "Preserve" => Ok(Self::Preserve),
|
||||
// TODO: replace this error with a diagnostic
|
||||
_ => Err("Value not supported for QuoteStyle"),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue