Skip to main content

FileMode

Enum FileMode 

Source
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

Source

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'.

Source

pub fn is_binary(&self) -> bool

Whether the file is binary ('rb', 'wb', …) rather than text.

Source

pub fn readable(&self) -> bool

Whether read() is allowed by this mode.

Source

pub fn writable(&self) -> bool

Whether write() is allowed by this mode.

Source

pub fn is_append(&self) -> bool

Whether writes should always append (a/a+).

Source

pub fn truncate(&self) -> bool

Whether open() must truncate the file to empty immediately (w/w+).

Source

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.

Source

pub fn type_name(&self) -> &'static str

Returns the bare Python type name (type(f).__name__) for this mode.

Source

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§

Source§

impl Clone for FileMode

Source§

fn clone(&self) -> FileMode

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for FileMode

Source§

impl Debug for FileMode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for FileMode

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for FileMode

Source§

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').

Source§

type Err = Cow<'static, str>

The associated error which can be returned from parsing.
Source§

fn from_str(mode: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for FileMode

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for FileMode

Source§

fn eq(&self, other: &FileMode) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for FileMode

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for FileMode

Source§

impl ToMontyObject for FileMode

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.