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:
Samuel Cormier-Iijima 2023-12-07 18:59:22 -05:00 committed by GitHub
parent 6bbabceead
commit 2414298289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 482 additions and 96 deletions

View file

@ -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"),
}