Skip to content

Commit

Permalink
Implement TokenFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed May 29, 2024
1 parent b0c8937 commit b52b83c
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 135 deletions.
39 changes: 0 additions & 39 deletions crates/ruff_python_ast/src/str_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,45 +150,6 @@ impl AnyStringPrefix {
}
}

impl TryFrom<char> for AnyStringPrefix {
type Error = String;

fn try_from(value: char) -> Result<Self, String> {
let result = match value {
'r' => Self::Regular(StringLiteralPrefix::Raw { uppercase: false }),
'R' => Self::Regular(StringLiteralPrefix::Raw { uppercase: true }),
'u' | 'U' => Self::Regular(StringLiteralPrefix::Unicode),
'b' | 'B' => Self::Bytes(ByteStringPrefix::Regular),
'f' | 'F' => Self::Format(FStringPrefix::Regular),
_ => return Err(format!("Unexpected prefix '{value}'")),
};
Ok(result)
}
}

impl TryFrom<[char; 2]> for AnyStringPrefix {
type Error = String;

fn try_from(value: [char; 2]) -> Result<Self, String> {
let result = match value {
['r', 'f' | 'F'] | ['f' | 'F', 'r'] => {
Self::Format(FStringPrefix::Raw { uppercase_r: false })
}
['R', 'f' | 'F'] | ['f' | 'F', 'R'] => {
Self::Format(FStringPrefix::Raw { uppercase_r: true })
}
['r', 'b' | 'B'] | ['b' | 'B', 'r'] => {
Self::Bytes(ByteStringPrefix::Raw { uppercase_r: false })
}
['R', 'b' | 'B'] | ['b' | 'B', 'R'] => {
Self::Bytes(ByteStringPrefix::Raw { uppercase_r: true })
}
_ => return Err(format!("Unexpected prefix '{}{}'", value[0], value[1])),
};
Ok(result)
}
}

impl fmt::Display for AnyStringPrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
Loading

0 comments on commit b52b83c

Please sign in to comment.