From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/winreg/src/encoder/mod.rs | 97 ++++ .../rust/winreg/src/encoder/serialization_serde.rs | 513 +++++++++++++++++++++ 2 files changed, 610 insertions(+) create mode 100644 third_party/rust/winreg/src/encoder/mod.rs create mode 100644 third_party/rust/winreg/src/encoder/serialization_serde.rs (limited to 'third_party/rust/winreg/src/encoder') 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 . 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 = Result; + +impl From 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, + tr: Transaction, + state: EncoderState, +} + +const ENCODER_SAM: DWORD = KEY_CREATE_SUB_KEY | KEY_SET_VALUE; + +impl Encoder { + pub fn from_key(key: &RegKey) -> EncodeResult { + 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 . 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(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.serialize_u32(value as u32) + } + + fn serialize_i8(self, value: i8) -> EncodeResult { + self.serialize_i64(value as i64) + } + + fn serialize_i16(self, value: i16) -> EncodeResult { + self.serialize_i64(value as i64) + } + + fn serialize_i32(self, value: i32) -> EncodeResult { + self.serialize_i64(value as i64) + } + + fn serialize_i64(self, value: i64) -> EncodeResult { + let s = value.to_string(); + emit_value!(self, s) + } + + fn serialize_u8(self, value: u8) -> EncodeResult { + self.serialize_u32(value as u32) + } + + fn serialize_u16(self, value: u16) -> EncodeResult { + self.serialize_u32(value as u32) + } + + fn serialize_u32(self, value: u32) -> EncodeResult { + emit_value!(self, value) + } + + fn serialize_u64(self, value: u64) -> EncodeResult { + emit_value!(self, value) + } + + fn serialize_f32(self, value: f32) -> EncodeResult { + let s = value.to_string(); + emit_value!(self, s) + } + + fn serialize_f64(self, value: f64) -> EncodeResult { + let s = value.to_string(); + emit_value!(self, s) + } + + fn serialize_char(self, value: char) -> EncodeResult { + let mut s = String::new(); + s.push(value); + emit_value!(self, s) + } + + fn serialize_str(self, value: &str) -> EncodeResult { + emit_value!(self, value) + } + + fn serialize_bytes(self, _value: &[u8]) -> EncodeResult { + no_impl!("serialize_bytes") + } + + fn serialize_none(self) -> EncodeResult { + no_impl!("serialize_none") + } + + fn serialize_some(self, _value: &T) -> EncodeResult { + no_impl!("serialize_some") + } + + fn serialize_unit(self) -> EncodeResult { + no_impl!("serialize_unit") + } + + fn serialize_unit_struct(self, _name: &'static str) -> EncodeResult { + no_impl!("serialize_unit_struct") + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + ) -> EncodeResult { + no_impl!("serialize_unit_variant") + } + + fn serialize_newtype_struct( + self, + _name: &'static str, + _value: &T, + ) -> EncodeResult { + no_impl!("serialize_newtype_struct") + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _value: &T, + ) -> EncodeResult { + no_impl!("serialize_newtype_variant") + } + + fn serialize_seq(self, _len: Option) -> EncodeResult { + no_impl!("serialize_seq") + } + + fn serialize_tuple(self, _len: usize) -> EncodeResult { + no_impl!("serialize_tuple") + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> EncodeResult { + no_impl!("serialize_tuple_struct") + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> EncodeResult { + no_impl!("serialize_tuple_variant") + } + + fn serialize_map(self, _len: Option) -> EncodeResult { + 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.serialize_map(Some(_len)) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> EncodeResult { + no_impl!("serialize_struct_variant") + } +} + +pub struct SeqEncoder {} + +impl SerializeSeq for SeqEncoder { + type Ok = (); + type Error = EncoderError; + fn serialize_element(&mut self, _value: &T) -> EncodeResult { + no_impl!("SerializeSeq::serialize_element") + } + fn end(self) -> EncodeResult { + no_impl!("SerializeSeq::end") + } +} + +pub struct TupleEncoder {} + +impl SerializeTuple for TupleEncoder { + type Ok = (); + type Error = EncoderError; + + fn serialize_element(&mut self, _value: &T) -> EncodeResult { + no_impl!("SerializeTuple::serialize_element") + } + + fn end(self) -> EncodeResult { + no_impl!("SerializeTuple::end") + } +} + +pub struct TupleStructEncoder {} + +impl SerializeTupleStruct for TupleStructEncoder { + type Ok = (); + type Error = EncoderError; + + fn serialize_field(&mut self, _value: &T) -> EncodeResult { + no_impl!("SerializeTupleStruct::serialize_field") + } + + fn end(self) -> EncodeResult { + no_impl!("SerializeTupleStruct::end") + } +} + +pub struct TupleVariantEncoder {} + +impl SerializeTupleVariant for TupleVariantEncoder { + type Ok = (); + type Error = EncoderError; + + fn serialize_field(&mut self, _value: &T) -> EncodeResult { + no_impl!("SerializeTupleVariant::serialize_field") + } + + fn end(self) -> EncodeResult { + no_impl!("SerializeTupleVariant::end") + } +} + +struct MapKeySerializer; + +impl serde::Serializer for MapKeySerializer { + type Ok = String; + type Error = EncoderError; + + type SerializeSeq = Impossible; + type SerializeTuple = Impossible; + type SerializeTupleStruct = Impossible; + type SerializeTupleVariant = Impossible; + type SerializeMap = Impossible; + type SerializeStruct = Impossible; + type SerializeStructVariant = Impossible; + + #[inline] + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> EncodeResult { + Ok(variant.to_owned()) + } + + #[inline] + fn serialize_newtype_struct(self, _name: &'static str, value: &T) -> EncodeResult + where + T: ?Sized + Serialize, + { + value.serialize(self) + } + + fn serialize_bool(self, _value: bool) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_i8(self, value: i8) -> EncodeResult { + Ok(value.to_string()) + } + + fn serialize_i16(self, value: i16) -> EncodeResult { + Ok(value.to_string()) + } + + fn serialize_i32(self, value: i32) -> EncodeResult { + Ok(value.to_string()) + } + + fn serialize_i64(self, value: i64) -> EncodeResult { + Ok(value.to_string()) + } + + fn serialize_u8(self, value: u8) -> EncodeResult { + Ok(value.to_string()) + } + + fn serialize_u16(self, value: u16) -> EncodeResult { + Ok(value.to_string()) + } + + fn serialize_u32(self, value: u32) -> EncodeResult { + Ok(value.to_string()) + } + + fn serialize_u64(self, value: u64) -> EncodeResult { + Ok(value.to_string()) + } + + fn serialize_f32(self, _value: f32) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_f64(self, _value: f64) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + #[inline] + fn serialize_char(self, value: char) -> EncodeResult { + Ok({ + let mut s = String::new(); + s.push(value); + s + }) + } + + #[inline] + fn serialize_str(self, value: &str) -> EncodeResult { + Ok(value.to_owned()) + } + + fn serialize_bytes(self, _value: &[u8]) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_unit(self) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_unit_struct(self, _name: &'static str) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _value: &T, + ) -> EncodeResult + where + T: ?Sized + Serialize, + { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_none(self) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_some(self, _value: &T) -> EncodeResult + where + T: ?Sized + Serialize, + { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_seq(self, _len: Option) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_tuple(self, _len: usize) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_map(self, _len: Option) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> EncodeResult { + Err(EncoderError::KeyMustBeAString) + } + + fn collect_str(self, value: &T) -> EncodeResult + 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( + &mut self, + key: &'static str, + value: &T, + ) -> EncodeResult { + self.enc.state = NextKey(String::from(key)); + value.serialize(&mut *self.enc) + } + + fn end(self) -> EncodeResult { + if self.is_root { + self.enc.keys.pop(); + } + Ok(()) + } +} + +impl<'a> SerializeMap for StructMapEncoder<'a> { + type Ok = (); + type Error = EncoderError; + + fn serialize_key(&mut self, key: &T) -> EncodeResult { + self.enc.state = NextKey(key.serialize(MapKeySerializer)?); + Ok(()) + } + + fn serialize_value(&mut self, value: &T) -> EncodeResult { + value.serialize(&mut *self.enc) + } + + fn end(self) -> EncodeResult { + if self.is_root { + self.enc.keys.pop(); + } + Ok(()) + } +} + +pub struct StructVariantEncoder {} + +impl SerializeStructVariant for StructVariantEncoder { + type Ok = (); + type Error = EncoderError; + + fn serialize_field( + &mut self, + _key: &'static str, + _value: &T, + ) -> EncodeResult { + no_impl!("SerializeStructVariant::serialize_field") + } + + fn end(self) -> EncodeResult { + no_impl!("SerializeStructVariant::end") + } +} -- cgit v1.2.3