summaryrefslogtreecommitdiffstats
path: root/third_party/rust/winreg/src/encoder
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/winreg/src/encoder
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/winreg/src/encoder')
-rw-r--r--third_party/rust/winreg/src/encoder/mod.rs97
-rw-r--r--third_party/rust/winreg/src/encoder/serialization_serde.rs513
2 files changed, 610 insertions, 0 deletions
diff --git a/third_party/rust/winreg/src/encoder/mod.rs b/third_party/rust/winreg/src/encoder/mod.rs
new file mode 100644
index 0000000000..f552dc05df
--- /dev/null
+++ b/third_party/rust/winreg/src/encoder/mod.rs
@@ -0,0 +1,97 @@
+// Copyright 2017, Igor Shaula
+// Licensed under the MIT License <LICENSE or
+// http://opensource.org/licenses/MIT>. This file
+// may not be copied, modified, or distributed
+// except according to those terms.
+use self::EncoderState::*;
+use super::enums::*;
+use super::transaction::Transaction;
+use super::RegKey;
+use std::error::Error;
+use std::fmt;
+use std::io;
+use winapi::shared::minwindef::DWORD;
+
+macro_rules! emit_value {
+ ($s:ident, $v:ident) => {
+ match mem::replace(&mut $s.state, Start) {
+ NextKey(ref s) => $s.keys[$s.keys.len() - 1]
+ .set_value(s, &$v)
+ .map_err(EncoderError::IoError),
+ Start => Err(EncoderError::NoFieldName),
+ }
+ };
+}
+
+macro_rules! no_impl {
+ ($e:expr) => {
+ Err(EncoderError::EncodeNotImplemented($e.to_owned()))
+ };
+}
+
+#[cfg(feature = "serialization-serde")]
+mod serialization_serde;
+
+#[derive(Debug)]
+pub enum EncoderError {
+ EncodeNotImplemented(String),
+ SerializerError(String),
+ IoError(io::Error),
+ NoFieldName,
+ KeyMustBeAString,
+}
+
+impl fmt::Display for EncoderError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{:?}", self)
+ }
+}
+
+impl Error for EncoderError {}
+
+pub type EncodeResult<T> = Result<T, EncoderError>;
+
+impl From<io::Error> for EncoderError {
+ fn from(err: io::Error) -> EncoderError {
+ EncoderError::IoError(err)
+ }
+}
+
+#[derive(Debug)]
+enum EncoderState {
+ Start,
+ NextKey(String),
+ // NextMapKey,
+}
+
+#[derive(Debug)]
+pub struct Encoder {
+ keys: Vec<RegKey>,
+ tr: Transaction,
+ state: EncoderState,
+}
+
+const ENCODER_SAM: DWORD = KEY_CREATE_SUB_KEY | KEY_SET_VALUE;
+
+impl Encoder {
+ pub fn from_key(key: &RegKey) -> EncodeResult<Encoder> {
+ let tr = Transaction::new()?;
+ key.open_subkey_transacted_with_flags("", &tr, ENCODER_SAM)
+ .map(|k| Encoder::new(k, tr))
+ .map_err(EncoderError::IoError)
+ }
+
+ fn new(key: RegKey, tr: Transaction) -> Encoder {
+ let mut keys = Vec::with_capacity(5);
+ keys.push(key);
+ Encoder {
+ keys,
+ tr,
+ state: Start,
+ }
+ }
+
+ pub fn commit(&mut self) -> EncodeResult<()> {
+ self.tr.commit().map_err(EncoderError::IoError)
+ }
+}
diff --git a/third_party/rust/winreg/src/encoder/serialization_serde.rs b/third_party/rust/winreg/src/encoder/serialization_serde.rs
new file mode 100644
index 0000000000..f764cce6ec
--- /dev/null
+++ b/third_party/rust/winreg/src/encoder/serialization_serde.rs
@@ -0,0 +1,513 @@
+// Copyright 2017, Igor Shaula
+// Licensed under the MIT License <LICENSE or
+// http://opensource.org/licenses/MIT>. This file
+// may not be copied, modified, or distributed
+// except according to those terms.
+use super::EncoderState::*;
+use super::{EncodeResult, Encoder, EncoderError, ENCODER_SAM};
+use serde::ser::*;
+use std::fmt;
+use std::mem;
+
+impl Error for EncoderError {
+ fn custom<T: fmt::Display>(msg: T) -> Self {
+ EncoderError::SerializerError(format!("{}", msg))
+ }
+}
+
+impl<'a> Serializer for &'a mut Encoder {
+ type Ok = ();
+ type Error = EncoderError;
+
+ type SerializeSeq = SeqEncoder;
+ type SerializeTuple = TupleEncoder;
+ type SerializeTupleStruct = TupleStructEncoder;
+ type SerializeTupleVariant = TupleVariantEncoder;
+ type SerializeMap = StructMapEncoder<'a>;
+ type SerializeStruct = StructMapEncoder<'a>;
+ type SerializeStructVariant = StructVariantEncoder;
+
+ fn serialize_bool(self, value: bool) -> EncodeResult<Self::Ok> {
+ self.serialize_u32(value as u32)
+ }
+
+ fn serialize_i8(self, value: i8) -> EncodeResult<Self::Ok> {
+ self.serialize_i64(value as i64)
+ }
+
+ fn serialize_i16(self, value: i16) -> EncodeResult<Self::Ok> {
+ self.serialize_i64(value as i64)
+ }
+
+ fn serialize_i32(self, value: i32) -> EncodeResult<Self::Ok> {
+ self.serialize_i64(value as i64)
+ }
+
+ fn serialize_i64(self, value: i64) -> EncodeResult<Self::Ok> {
+ let s = value.to_string();
+ emit_value!(self, s)
+ }
+
+ fn serialize_u8(self, value: u8) -> EncodeResult<Self::Ok> {
+ self.serialize_u32(value as u32)
+ }
+
+ fn serialize_u16(self, value: u16) -> EncodeResult<Self::Ok> {
+ self.serialize_u32(value as u32)
+ }
+
+ fn serialize_u32(self, value: u32) -> EncodeResult<Self::Ok> {
+ emit_value!(self, value)
+ }
+
+ fn serialize_u64(self, value: u64) -> EncodeResult<Self::Ok> {
+ emit_value!(self, value)
+ }
+
+ fn serialize_f32(self, value: f32) -> EncodeResult<Self::Ok> {
+ let s = value.to_string();
+ emit_value!(self, s)
+ }
+
+ fn serialize_f64(self, value: f64) -> EncodeResult<Self::Ok> {
+ let s = value.to_string();
+ emit_value!(self, s)
+ }
+
+ fn serialize_char(self, value: char) -> EncodeResult<Self::Ok> {
+ let mut s = String::new();
+ s.push(value);
+ emit_value!(self, s)
+ }
+
+ fn serialize_str(self, value: &str) -> EncodeResult<Self::Ok> {
+ emit_value!(self, value)
+ }
+
+ fn serialize_bytes(self, _value: &[u8]) -> EncodeResult<Self::Ok> {
+ no_impl!("serialize_bytes")
+ }
+
+ fn serialize_none(self) -> EncodeResult<Self::Ok> {
+ no_impl!("serialize_none")
+ }
+
+ fn serialize_some<T: ?Sized + Serialize>(self, _value: &T) -> EncodeResult<Self::Ok> {
+ no_impl!("serialize_some")
+ }
+
+ fn serialize_unit(self) -> EncodeResult<Self::Ok> {
+ no_impl!("serialize_unit")
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> EncodeResult<Self::Ok> {
+ no_impl!("serialize_unit_struct")
+ }
+
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ ) -> EncodeResult<Self::Ok> {
+ no_impl!("serialize_unit_variant")
+ }
+
+ fn serialize_newtype_struct<T: ?Sized + Serialize>(
+ self,
+ _name: &'static str,
+ _value: &T,
+ ) -> EncodeResult<Self::Ok> {
+ no_impl!("serialize_newtype_struct")
+ }
+
+ fn serialize_newtype_variant<T: ?Sized + Serialize>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> EncodeResult<Self::Ok> {
+ no_impl!("serialize_newtype_variant")
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> EncodeResult<Self::SerializeSeq> {
+ no_impl!("serialize_seq")
+ }
+
+ fn serialize_tuple(self, _len: usize) -> EncodeResult<Self::SerializeTuple> {
+ no_impl!("serialize_tuple")
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> EncodeResult<Self::SerializeTupleStruct> {
+ no_impl!("serialize_tuple_struct")
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> EncodeResult<Self::SerializeTupleVariant> {
+ no_impl!("serialize_tuple_variant")
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> EncodeResult<Self::SerializeMap> {
+ match mem::replace(&mut self.state, Start) {
+ // ---
+ Start => {
+ // root structure
+ Ok(StructMapEncoder {
+ enc: self,
+ is_root: true,
+ })
+ }
+ NextKey(ref s) => {
+ // nested structure
+ match self.keys[self.keys.len() - 1].create_subkey_transacted_with_flags(
+ &s,
+ &self.tr,
+ ENCODER_SAM,
+ ) {
+ Ok((subkey, _disp)) => {
+ self.keys.push(subkey);
+ Ok(StructMapEncoder {
+ enc: self,
+ is_root: true,
+ })
+ }
+ Err(err) => Err(EncoderError::IoError(err)),
+ }
+ }
+ }
+ }
+
+ fn serialize_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> EncodeResult<Self::SerializeStruct> {
+ self.serialize_map(Some(_len))
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> EncodeResult<Self::SerializeStructVariant> {
+ no_impl!("serialize_struct_variant")
+ }
+}
+
+pub struct SeqEncoder {}
+
+impl SerializeSeq for SeqEncoder {
+ type Ok = ();
+ type Error = EncoderError;
+ fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeSeq::serialize_element")
+ }
+ fn end(self) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeSeq::end")
+ }
+}
+
+pub struct TupleEncoder {}
+
+impl SerializeTuple for TupleEncoder {
+ type Ok = ();
+ type Error = EncoderError;
+
+ fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeTuple::serialize_element")
+ }
+
+ fn end(self) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeTuple::end")
+ }
+}
+
+pub struct TupleStructEncoder {}
+
+impl SerializeTupleStruct for TupleStructEncoder {
+ type Ok = ();
+ type Error = EncoderError;
+
+ fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeTupleStruct::serialize_field")
+ }
+
+ fn end(self) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeTupleStruct::end")
+ }
+}
+
+pub struct TupleVariantEncoder {}
+
+impl SerializeTupleVariant for TupleVariantEncoder {
+ type Ok = ();
+ type Error = EncoderError;
+
+ fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeTupleVariant::serialize_field")
+ }
+
+ fn end(self) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeTupleVariant::end")
+ }
+}
+
+struct MapKeySerializer;
+
+impl serde::Serializer for MapKeySerializer {
+ type Ok = String;
+ type Error = EncoderError;
+
+ type SerializeSeq = Impossible<String, EncoderError>;
+ type SerializeTuple = Impossible<String, EncoderError>;
+ type SerializeTupleStruct = Impossible<String, EncoderError>;
+ type SerializeTupleVariant = Impossible<String, EncoderError>;
+ type SerializeMap = Impossible<String, EncoderError>;
+ type SerializeStruct = Impossible<String, EncoderError>;
+ type SerializeStructVariant = Impossible<String, EncoderError>;
+
+ #[inline]
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ ) -> EncodeResult<Self::Ok> {
+ Ok(variant.to_owned())
+ }
+
+ #[inline]
+ fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> EncodeResult<Self::Ok>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ fn serialize_bool(self, _value: bool) -> EncodeResult<Self::Ok> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_i8(self, value: i8) -> EncodeResult<Self::Ok> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_i16(self, value: i16) -> EncodeResult<Self::Ok> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_i32(self, value: i32) -> EncodeResult<Self::Ok> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_i64(self, value: i64) -> EncodeResult<Self::Ok> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_u8(self, value: u8) -> EncodeResult<Self::Ok> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_u16(self, value: u16) -> EncodeResult<Self::Ok> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_u32(self, value: u32) -> EncodeResult<Self::Ok> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_u64(self, value: u64) -> EncodeResult<Self::Ok> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_f32(self, _value: f32) -> EncodeResult<Self::Ok> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_f64(self, _value: f64) -> EncodeResult<Self::Ok> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ #[inline]
+ fn serialize_char(self, value: char) -> EncodeResult<Self::Ok> {
+ Ok({
+ let mut s = String::new();
+ s.push(value);
+ s
+ })
+ }
+
+ #[inline]
+ fn serialize_str(self, value: &str) -> EncodeResult<Self::Ok> {
+ Ok(value.to_owned())
+ }
+
+ fn serialize_bytes(self, _value: &[u8]) -> EncodeResult<Self::Ok> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_unit(self) -> EncodeResult<Self::Ok> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> EncodeResult<Self::Ok> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> EncodeResult<Self::Ok>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_none(self) -> EncodeResult<Self::Ok> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_some<T>(self, _value: &T) -> EncodeResult<Self::Ok>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> EncodeResult<Self::SerializeSeq> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_tuple(self, _len: usize) -> EncodeResult<Self::SerializeTuple> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> EncodeResult<Self::SerializeTupleStruct> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> EncodeResult<Self::SerializeTupleVariant> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> EncodeResult<Self::SerializeMap> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> EncodeResult<Self::SerializeStruct> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> EncodeResult<Self::SerializeStructVariant> {
+ Err(EncoderError::KeyMustBeAString)
+ }
+
+ fn collect_str<T: ?Sized>(self, value: &T) -> EncodeResult<String>
+ where
+ T: fmt::Display,
+ {
+ Ok(value.to_string())
+ }
+}
+
+pub struct StructMapEncoder<'a> {
+ enc: &'a mut Encoder,
+ is_root: bool,
+}
+
+impl<'a> SerializeStruct for StructMapEncoder<'a> {
+ type Ok = ();
+ type Error = EncoderError;
+
+ fn serialize_field<T: ?Sized + Serialize>(
+ &mut self,
+ key: &'static str,
+ value: &T,
+ ) -> EncodeResult<Self::Ok> {
+ self.enc.state = NextKey(String::from(key));
+ value.serialize(&mut *self.enc)
+ }
+
+ fn end(self) -> EncodeResult<Self::Ok> {
+ if self.is_root {
+ self.enc.keys.pop();
+ }
+ Ok(())
+ }
+}
+
+impl<'a> SerializeMap for StructMapEncoder<'a> {
+ type Ok = ();
+ type Error = EncoderError;
+
+ fn serialize_key<T: ?Sized + Serialize>(&mut self, key: &T) -> EncodeResult<Self::Ok> {
+ self.enc.state = NextKey(key.serialize(MapKeySerializer)?);
+ Ok(())
+ }
+
+ fn serialize_value<T: ?Sized + Serialize>(&mut self, value: &T) -> EncodeResult<Self::Ok> {
+ value.serialize(&mut *self.enc)
+ }
+
+ fn end(self) -> EncodeResult<Self::Ok> {
+ if self.is_root {
+ self.enc.keys.pop();
+ }
+ Ok(())
+ }
+}
+
+pub struct StructVariantEncoder {}
+
+impl SerializeStructVariant for StructVariantEncoder {
+ type Ok = ();
+ type Error = EncoderError;
+
+ fn serialize_field<T: ?Sized + Serialize>(
+ &mut self,
+ _key: &'static str,
+ _value: &T,
+ ) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeStructVariant::serialize_field")
+ }
+
+ fn end(self) -> EncodeResult<Self::Ok> {
+ no_impl!("SerializeStructVariant::end")
+ }
+}