Add ser/de impls for mikros File(Creation/Write/Open)Mode
Some checks failed
CI / Test suite (push) Has been cancelled
CI / Test suite (windows) (push) Has been cancelled
CI / Rust ${{matrix.rust}} (beta) (push) Has been cancelled
CI / Rust ${{matrix.rust}} (stable) (push) Has been cancelled
CI / Rust nightly ${{matrix.os == 'windows' && '(windows)' || ''}} (ubuntu) (push) Has been cancelled
CI / Rust nightly ${{matrix.os == 'windows' && '(windows)' || ''}} (windows) (push) Has been cancelled
CI / Rust ${{matrix.rust}} (1.31.0) (push) Has been cancelled
CI / Rust ${{matrix.rust}} (1.34.0) (push) Has been cancelled
CI / Rust 1.56.0 (push) Has been cancelled
CI / Rust 1.36.0 (push) Has been cancelled
CI / Minimal versions (push) Has been cancelled
CI / Documentation (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Miri (push) Has been cancelled
CI / Outdated (push) Has been cancelled

This commit is contained in:
pjht 2024-11-11 15:48:33 -06:00
parent 1b40077a58
commit 678d2be06c
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
2 changed files with 197 additions and 1 deletions

View File

@ -3221,3 +3221,143 @@ impl<'de> Visitor<'de> for ErrnoVisitor {
}
}
}
variant_identifier! {
FileCreationModeKind (NoCreate; b"NoCreate"; 0, Create; b"Create"; 1, ForceCreate; b"ForceCreate"; 2)
"`NoCreate` or `Create` or `ForceCreate`",
FILE_CREATION_MODE_VARIANTS
}
#[cfg(target_os = "mikros")]
impl<'de> Deserialize<'de> for std::os::mikros::FileCreationMode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_enum("FileCreationMode", &["NoCreate", "Create", "ForceCreate"], FileCreationModeVisitor)
}
}
#[cfg(target_os = "mikros")]
struct FileCreationModeVisitor;
#[cfg(target_os = "mikros")]
impl<'de> Visitor<'de> for FileCreationModeVisitor {
type Value = std::os::mikros::FileCreationMode;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("FileCreationMode")
}
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where
A: EnumAccess<'de>,
{
match tri!(data.variant()) {
(FileCreationModeKind::NoCreate,v) => v.unit_variant().map(|_| std::os::mikros::FileCreationMode::NoCreate),
(FileCreationModeKind::Create,v) => v.unit_variant().map(|_| std::os::mikros::FileCreationMode::Create),
(FileCreationModeKind::ForceCreate,v) => v.unit_variant().map(|_| std::os::mikros::FileCreationMode::ForceCreate),
}
}
}
variant_identifier! {
FileWriteModeKind (Start; b"Start"; 0, Truncate; b"Truncate"; 1, Append; b"Append"; 2)
"`Start` or `Truncate` or `Append`",
FILE_WRITE_MODE_VARIANTS
}
#[cfg(target_os = "mikros")]
impl<'de> Deserialize<'de> for std::os::mikros::FileWriteMode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_enum("FileWriteMode", &["Start", "Truncate", "Append"], FileWriteModeVisitor)
}
}
#[cfg(target_os = "mikros")]
struct FileWriteModeVisitor;
#[cfg(target_os = "mikros")]
impl<'de> Visitor<'de> for FileWriteModeVisitor {
type Value = std::os::mikros::FileWriteMode;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("FileWriteMode")
}
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where
A: EnumAccess<'de>,
{
match tri!(data.variant()) {
(FileWriteModeKind::Start,v) => v.unit_variant().map(|_| std::os::mikros::FileWriteMode::Start),
(FileWriteModeKind::Truncate,v) => v.unit_variant().map(|_| std::os::mikros::FileWriteMode::Truncate),
(FileWriteModeKind::Append,v) => v.unit_variant().map(|_| std::os::mikros::FileWriteMode::Append),
}
}
}
#[cfg(target_os = "mikros")]
struct FileOpenModeTupleVisitor;
#[cfg(target_os = "mikros")]
impl<'de> Visitor<'de> for FileOpenModeTupleVisitor {
type Value = (std::os::mikros::FileWriteMode, std::os::mikros::FileCreationMode);
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("(FileWriteMode, FileCreationMode)")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let wr_mode = match tri!(seq.next_element()) {
Some(x) => x,
None => return Err(Error::invalid_length(2, &self)),
};
let cr_mode = match tri!(seq.next_element()) {
Some(x) => x,
None => return Err(Error::invalid_length(2, &self)),
};
Ok((wr_mode, cr_mode))
}
}
variant_identifier! {
FileOpenModeKind (Read; b"Read"; 0, Write; b"Write"; 1, ReadWrite; b"ReadWrite"; 2)
"`Read` or `Write` or `ReadWrite`",
FILE_OPEN_MODE_VARIANTS
}
#[cfg(target_os = "mikros")]
struct FileOpenModeVisitor;
#[cfg(target_os = "mikros")]
impl<'de> Visitor<'de> for FileOpenModeVisitor {
type Value = std::os::mikros::FileOpenMode;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("FileOpenMode")
}
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where
A: EnumAccess<'de>,
{
match tri!(data.variant()) {
(FileOpenModeKind::Read,v) => v.unit_variant().map(|_| std::os::mikros::FileOpenMode::Read),
(FileOpenModeKind::Write,v) => v.tuple_variant(2, FileOpenModeTupleVisitor).map(|(x,y)| std::os::mikros::FileOpenMode::Write(x,y)),
(FileOpenModeKind::ReadWrite,v) => v.tuple_variant(2, FileOpenModeTupleVisitor).map(|(x,y)| std::os::mikros::FileOpenMode::ReadWrite(x,y)),
}
}
}

View File

@ -1,6 +1,6 @@
use crate::lib::*;
use crate::ser::{Error, Serialize, SerializeTuple, Serializer};
use crate::ser::{Error, Serialize, SerializeTuple, SerializeTupleVariant, Serializer};
////////////////////////////////////////////////////////////////////////////////
@ -1111,3 +1111,59 @@ impl Serialize for std::os::mikros::Errno {
(*self as i32).serialize(serializer)
}
}
#[cfg(target_os = "mikros")]
impl Serialize for std::os::mikros::FileCreationMode {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::NoCreate => serializer.serialize_unit_variant("FileCreationMode", 0, "NoCreate"),
Self::Create => serializer.serialize_unit_variant("FileCreationMode", 1, "Create"),
Self::ForceCreate => serializer.serialize_unit_variant("FileCreationMode", 2, "ForceCreate"),
}
}
}
#[cfg(target_os = "mikros")]
impl Serialize for std::os::mikros::FileWriteMode {
#[inline]
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"),
}
}
}
#[cfg(target_os = "mikros")]
impl Serialize for std::os::mikros::FileOpenMode {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Read => serializer.serialize_unit_variant("FileOpenMode", 0, "Read"),
Self::Write(ref wr_mode, ref cr_mode) => {
let mut tv = serializer.serialize_tuple_variant("FileOpenMode", 1, "Write", 2)?;
tv.serialize_field(wr_mode)?;
tv.serialize_field(cr_mode)?;
tv.end()
},
Self::ReadWrite(ref wr_mode, ref cr_mode) => {
let mut tv = serializer.serialize_tuple_variant("FileOpenMode", 2, "ReadWrite", 2)?;
tv.serialize_field(wr_mode)?;
tv.serialize_field(cr_mode)?;
tv.end()
},
}
}
}