use ruff_text_size::TextSize;
use std::fmt;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, is_macro::Is)]
pub enum StringLiteralPrefix {
Empty,
Unicode,
Raw { uppercase: bool },
}
impl StringLiteralPrefix {
pub const fn as_str(self) -> &'static str {
match self {
Self::Empty => "",
Self::Unicode => "u",
Self::Raw { uppercase: true } => "R",
Self::Raw { uppercase: false } => "r",
}
}
pub const fn text_len(self) -> TextSize {
match self {
Self::Empty => TextSize::new(0),
Self::Unicode | Self::Raw { .. } => TextSize::new(1),
}
}
}
impl fmt::Display for StringLiteralPrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum FStringPrefix {
Regular,
Raw { uppercase_r: bool },
}
impl FStringPrefix {
pub const fn as_str(self) -> &'static str {
match self {
Self::Regular => "f",
Self::Raw { uppercase_r: true } => "Rf",
Self::Raw { uppercase_r: false } => "rf",
}
}
pub const fn text_len(self) -> TextSize {
match self {
Self::Regular => TextSize::new(1),
Self::Raw { .. } => TextSize::new(2),
}
}
pub const fn is_raw(self) -> bool {
matches!(self, Self::Raw { .. })
}
}
impl fmt::Display for FStringPrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum TStringPrefix {
Regular,
Raw { uppercase_r: bool },
}
impl TStringPrefix {
pub const fn as_str(self) -> &'static str {
match self {
Self::Regular => "t",
Self::Raw { uppercase_r: true } => "Rt",
Self::Raw { uppercase_r: false } => "rt",
}
}
pub const fn text_len(self) -> TextSize {
match self {
Self::Regular => TextSize::new(1),
Self::Raw { .. } => TextSize::new(2),
}
}
pub const fn is_raw(self) -> bool {
matches!(self, Self::Raw { .. })
}
}
impl fmt::Display for TStringPrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ByteStringPrefix {
Regular,
Raw { uppercase_r: bool },
}
impl ByteStringPrefix {
pub const fn as_str(self) -> &'static str {
match self {
Self::Regular => "b",
Self::Raw { uppercase_r: true } => "Rb",
Self::Raw { uppercase_r: false } => "rb",
}
}
pub const fn text_len(self) -> TextSize {
match self {
Self::Regular => TextSize::new(1),
Self::Raw { .. } => TextSize::new(2),
}
}
pub const fn is_raw(self) -> bool {
matches!(self, Self::Raw { .. })
}
}
impl fmt::Display for ByteStringPrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, is_macro::Is)]
pub enum AnyStringPrefix {
Bytes(ByteStringPrefix),
Format(FStringPrefix),
Template(TStringPrefix),
Regular(StringLiteralPrefix),
}
impl AnyStringPrefix {
pub const fn as_str(self) -> &'static str {
match self {
Self::Regular(regular_prefix) => regular_prefix.as_str(),
Self::Bytes(bytestring_prefix) => bytestring_prefix.as_str(),
Self::Format(fstring_prefix) => fstring_prefix.as_str(),
Self::Template(tstring_prefix) => tstring_prefix.as_str(),
}
}
pub const fn text_len(self) -> TextSize {
match self {
Self::Regular(regular_prefix) => regular_prefix.text_len(),
Self::Bytes(bytestring_prefix) => bytestring_prefix.text_len(),
Self::Format(fstring_prefix) => fstring_prefix.text_len(),
Self::Template(tstring_prefix) => tstring_prefix.text_len(),
}
}
pub const fn is_raw(self) -> bool {
match self {
Self::Regular(regular_prefix) => regular_prefix.is_raw(),
Self::Bytes(bytestring_prefix) => bytestring_prefix.is_raw(),
Self::Format(fstring_prefix) => fstring_prefix.is_raw(),
Self::Template(tstring_prefix) => tstring_prefix.is_raw(),
}
}
}
impl fmt::Display for AnyStringPrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Default for AnyStringPrefix {
fn default() -> Self {
Self::Regular(StringLiteralPrefix::Empty)
}
}