mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:26 +00:00
Simplify the StringFlags
trait (#15944)
This commit is contained in:
parent
64e64d2681
commit
cb71393332
3 changed files with 13 additions and 28 deletions
|
@ -1027,18 +1027,20 @@ pub trait StringFlags: Copy {
|
|||
self.quote_len()
|
||||
}
|
||||
|
||||
fn as_any_string_flags(self) -> AnyStringFlags {
|
||||
AnyStringFlags::new(self.prefix(), self.quote_style(), self.triple_quotes())
|
||||
}
|
||||
|
||||
fn display_contents(self, contents: &str) -> DisplayFlags {
|
||||
DisplayFlags {
|
||||
prefix: self.prefix(),
|
||||
quote_str: self.quote_str(),
|
||||
flags: self.as_any_string_flags(),
|
||||
contents,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DisplayFlags<'a> {
|
||||
prefix: AnyStringPrefix,
|
||||
quote_str: &'a str,
|
||||
flags: AnyStringFlags,
|
||||
contents: &'a str,
|
||||
}
|
||||
|
||||
|
@ -1047,8 +1049,8 @@ impl std::fmt::Display for DisplayFlags<'_> {
|
|||
write!(
|
||||
f,
|
||||
"{prefix}{quote}{contents}{quote}",
|
||||
prefix = self.prefix,
|
||||
quote = self.quote_str,
|
||||
prefix = self.flags.prefix(),
|
||||
quote = self.flags.quote_str(),
|
||||
contents = self.contents
|
||||
)
|
||||
}
|
||||
|
@ -2241,11 +2243,7 @@ impl From<AnyStringFlags> for StringLiteralFlags {
|
|||
|
||||
impl From<StringLiteralFlags> for AnyStringFlags {
|
||||
fn from(value: StringLiteralFlags) -> Self {
|
||||
Self::new(
|
||||
AnyStringPrefix::Regular(value.prefix()),
|
||||
value.quote_style(),
|
||||
value.triple_quotes(),
|
||||
)
|
||||
value.as_any_string_flags()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2266,11 +2264,7 @@ impl From<AnyStringFlags> for BytesLiteralFlags {
|
|||
|
||||
impl From<BytesLiteralFlags> for AnyStringFlags {
|
||||
fn from(value: BytesLiteralFlags) -> Self {
|
||||
Self::new(
|
||||
AnyStringPrefix::Bytes(value.prefix()),
|
||||
value.quote_style(),
|
||||
value.triple_quotes(),
|
||||
)
|
||||
value.as_any_string_flags()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2291,11 +2285,7 @@ impl From<AnyStringFlags> for FStringFlags {
|
|||
|
||||
impl From<FStringFlags> for AnyStringFlags {
|
||||
fn from(value: FStringFlags) -> Self {
|
||||
Self::new(
|
||||
AnyStringPrefix::Format(value.prefix()),
|
||||
value.quote_style(),
|
||||
value.triple_quotes(),
|
||||
)
|
||||
value.as_any_string_flags()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ use rustc_hash::{FxBuildHasher, FxHashSet};
|
|||
use ruff_python_ast::name::Name;
|
||||
use ruff_python_ast::{
|
||||
self as ast, BoolOp, CmpOp, ConversionFlag, Expr, ExprContext, FStringElement, FStringElements,
|
||||
IpyEscapeKind, Number, Operator, UnaryOp,
|
||||
IpyEscapeKind, Number, Operator, StringFlags, UnaryOp,
|
||||
};
|
||||
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ use ruff_python_ast::str::{Quote, TripleQuotes};
|
|||
use ruff_python_ast::str_prefix::{
|
||||
AnyStringPrefix, ByteStringPrefix, FStringPrefix, StringLiteralPrefix,
|
||||
};
|
||||
use ruff_python_ast::{AnyStringFlags, BoolOp, Int, IpyEscapeKind, Operator, StringFlags, UnaryOp};
|
||||
use ruff_python_ast::{BoolOp, Int, IpyEscapeKind, Operator, StringFlags, UnaryOp};
|
||||
use ruff_text_size::{Ranged, TextRange};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
|
@ -770,11 +770,6 @@ impl TokenFlags {
|
|||
pub(crate) const fn is_raw_string(self) -> bool {
|
||||
self.intersects(TokenFlags::RAW_STRING)
|
||||
}
|
||||
|
||||
/// Converts this type to [`AnyStringFlags`], setting the equivalent flags.
|
||||
pub(crate) fn as_any_string_flags(self) -> AnyStringFlags {
|
||||
AnyStringFlags::new(self.prefix(), self.quote_style(), self.triple_quotes())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue