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

View File

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