mikros: Make FileWriteMode a struct to allow both truncate and append at once

This commit is contained in:
pjht 2024-11-17 21:43:52 -06:00
parent 7005510bc0
commit a40ff11ecc
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
2 changed files with 11 additions and 15 deletions

View File

@ -11,7 +11,7 @@
mod errno; mod errno;
use serde::{Serialize, Serializer, ser::SerializeTupleVariant}; use serde::{Serialize, Serializer, ser::{SerializeTupleVariant, SerializeStruct}};
#[stable(feature = "mikros", since = "1.80.0")] #[stable(feature = "mikros", since = "1.80.0")]
pub use errno::Errno; pub use errno::Errno;
@ -27,10 +27,9 @@ pub enum FileCreationMode {
#[stable(feature = "mikros", since = "1.80.0")] #[stable(feature = "mikros", since = "1.80.0")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum FileWriteMode { pub struct FileWriteMode {
Start, pub truncate: bool,
Truncate, pub append: bool,
Append
} }
#[stable(feature = "mikros", since = "1.80.0")] #[stable(feature = "mikros", since = "1.80.0")]
@ -99,11 +98,10 @@ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
match self { let mut ser = serializer.serialize_struct("FileWriteMode", 2)?;
Self::Start => serializer.serialize_unit_variant("FileWriteMode", 0, "Start"), ser.serialize_field("truncate", &self.truncate)?;
Self::Truncate => serializer.serialize_unit_variant("FileWriteMode", 1, "Truncate"), ser.serialize_field("append", &self.append)?;
Self::Append => serializer.serialize_unit_variant("FileWriteMode", 2, "Append"), ser.end()
}
} }
} }

View File

@ -374,11 +374,9 @@ fn get_open_mode(&self) -> io::Result<FileOpenMode> {
(true, false) => FileCreationMode::Create, (true, false) => FileCreationMode::Create,
(_, true) => FileCreationMode::ForceCreate, (_, true) => FileCreationMode::ForceCreate,
}; };
let wr_mode = match (self.append, self.truncate) { let wr_mode = FileWriteMode {
(false, false) => FileWriteMode::Start, truncate: self.truncate,
(false, true) => FileWriteMode::Truncate, append: self.append,
(true, false) => FileWriteMode::Append,
(true, true) => unreachable!()
}; };
match self.read { match self.read {
false => Ok(FileOpenMode::Write(wr_mode, cr_mode)), false => Ok(FileOpenMode::Write(wr_mode, cr_mode)),