pub enum FileMode {
Read(bool),
ReadUpdate(bool),
Write(bool),
WriteUpdate(bool),
Append(bool),
AppendUpdate(bool),
}Expand description
A parsed Python open() mode.
This single enum captures everything that matters about how a file was
opened: the access pattern (r/w/a and the + update flag) and
whether the file is binary. The variant name encodes the access pattern;
the bool payload is true for binary and false for text — i.e.
Read(true) is 'rb' and Read(false) is 'r'.
Construct one with the FromStr impl (mode_str.parse::<FileMode>()).
The original input string is
intentionally not preserved; FileMode::as_str rebuilds the canonical
CPython form ('r', 'rb+', 'wb', …), matching how CPython itself
normalizes input like 'rt' → 'r' and 'r+b' → 'rb+'.
+ update modes (ReadUpdate/WriteUpdate/AppendUpdate) are reserved
in the enum so the mode space is fully represented, but FromStr
currently rejects them — properly modelling them needs read-position
tracking that the file wrapper does not yet implement. Treat the Update
variants as unreachable at runtime; do not pattern-match against them as
if they were a valid result of parsing user input.
Carried publicly by MontyObject::FileHandle so a host servicing file
operations can inspect the mode without re-parsing the raw string.
Variants§
Read(bool)
r / rb: read-only; the file must already exist.
ReadUpdate(bool)
r+ / rb+: read and write an existing file. Reserved; not yet
produced by FromStr.
Write(bool)
w / wb: write-only; truncate the file (creating it if missing) on open.
WriteUpdate(bool)
w+ / wb+: read and write; truncate the file (creating it if missing).
Reserved; not yet produced by FromStr.
Append(bool)
a / ab: write-only appending; create the file if missing, preserving content.
AppendUpdate(bool)
a+ / ab+: read and append; create the file if missing, preserving content.
Reserved; not yet produced by FromStr.
Implementations§
Source§impl FileMode
impl FileMode
Sourcepub fn as_str(&self) -> &'static str
pub fn as_str(&self) -> &'static str
Returns the canonical Python open() mode string for this mode,
matching what CPython exposes via file.mode.
The result is always one of the 12 well-formed mode strings (r, rb,
r+, rb+, w, wb, w+, wb+, a, ab, a+, ab+). This is
the canonical form CPython itself normalizes user input into — e.g.
'rt' → 'r', 'r+b' → 'rb+', 'br' → 'rb'.
Sourcepub fn truncate(&self) -> bool
pub fn truncate(&self) -> bool
Whether open() must truncate the file to empty immediately (w/w+).
Sourcepub fn create(&self) -> bool
pub fn create(&self) -> bool
Whether open() must create the file immediately if missing.
True for the w/w+ and a/a+ families. For append modes this must
not disturb existing content.
Sourcepub fn type_name(&self) -> &'static str
pub fn type_name(&self) -> &'static str
Returns the bare Python type name (type(f).__name__) for this mode.
Sourcepub fn file_type_name(&self) -> &'static str
pub fn file_type_name(&self) -> &'static str
Returns the fully-qualified _io wrapper type name a file opened with
this mode presents as, matching CPython’s repr(f) (e.g.
"_io.TextIOWrapper"). The module-less form is type_name.
Trait Implementations§
impl Copy for FileMode
Source§impl<'de> Deserialize<'de> for FileMode
impl<'de> Deserialize<'de> for FileMode
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for FileMode
Source§impl FromStr for FileMode
Parses a Python open() mode string into a FileMode.
impl FromStr for FileMode
Parses a Python open() mode string into a FileMode.
Monty supports the common read, write, append, and update combinations in
text or binary form. Exclusive creation (x) is rejected for now because
it needs a dedicated mount-table operation to be race-free.
The Err payload is a CPython-matched message — an unknown mode
character, duplicated b/t/+, conflicting binary+text flags, more
than one of the r/w/a actions, or none at all ('', 'b', 't').