rustpython_parser_core/
mode.rs

1//! Control in the different modes by which a source file can be parsed.
2
3/// The mode argument specifies in what way code must be parsed.
4#[derive(Clone, Copy, Hash, PartialEq, Eq)]
5pub enum Mode {
6    /// The code consists of a sequence of statements.
7    Module,
8    /// The code consists of a sequence of interactive statement.
9    Interactive,
10    /// The code consists of a single expression.
11    Expression,
12}
13
14impl std::str::FromStr for Mode {
15    type Err = ModeParseError;
16    fn from_str(s: &str) -> Result<Self, ModeParseError> {
17        match s {
18            "exec" | "single" => Ok(Mode::Module),
19            "eval" => Ok(Mode::Expression),
20            _ => Err(ModeParseError),
21        }
22    }
23}
24
25/// Returned when a given mode is not valid.
26#[derive(Debug)]
27pub struct ModeParseError;
28
29impl std::fmt::Display for ModeParseError {
30    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
31        write!(f, r#"mode must be "exec", "eval", or "single""#)
32    }
33}