diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 7dd5c61c..915b53a7 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -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(deserializer: D) -> Result + 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(self, data: A) -> Result + 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(deserializer: D) -> Result + 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(self, data: A) -> Result + 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(self, mut seq: A) -> Result + 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(self, data: A) -> Result + 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)), + } + } +} diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index bb3e27b4..af2593fa 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -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(&self, serializer: S) -> Result + 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(&self, serializer: S) -> Result + 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(&self, serializer: S) -> Result + 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() + }, + + } + } +}