mirror of
https://github.com/RustPython/Parser.git
synced 2025-09-02 16:47:47 +00:00
Simplify string check
This commit is contained in:
parent
958c7e33ad
commit
fd8468c5eb
2 changed files with 33 additions and 49 deletions
|
@ -240,6 +240,23 @@ pub enum StringKind {
|
|||
Unicode,
|
||||
}
|
||||
|
||||
impl TryFrom<String> for StringKind {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, String> {
|
||||
match value.as_str() {
|
||||
"" => Ok(StringKind::String),
|
||||
"r" | "R" => Ok(StringKind::RawString),
|
||||
"u" | "U" => Ok(StringKind::Unicode),
|
||||
"b" | "B" => Ok(StringKind::Bytes),
|
||||
"f" | "F" => Ok(StringKind::FString),
|
||||
"fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF" => Ok(StringKind::RawFString),
|
||||
"br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB" => Ok(StringKind::RawBytes),
|
||||
s => Err(format!("Unexpected string prefix: {s}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for StringKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
use StringKind::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue