Skip to main content

rustpython_compiler_core/
mode.rs

1#[derive(Clone, Copy)]
2pub enum Mode {
3    Exec,
4    Eval,
5    Single,
6    /// Returns the value of the last statement in the statement list.
7    BlockExpr,
8}
9
10impl core::str::FromStr for Mode {
11    type Err = ModeParseError;
12
13    // To support `builtins.compile()` `mode` argument
14    fn from_str(s: &str) -> Result<Self, Self::Err> {
15        match s {
16            "exec" => Ok(Self::Exec),
17            "eval" => Ok(Self::Eval),
18            "single" => Ok(Self::Single),
19            _ => Err(ModeParseError),
20        }
21    }
22}
23
24/// Returned when a given mode is not valid.
25#[derive(Clone, Copy, Debug)]
26pub struct ModeParseError;
27
28impl core::fmt::Display for ModeParseError {
29    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30        write!(f, r#"mode must be "exec", "eval", or "single""#)
31    }
32}