| 1 |
#![allow(unused_variables)] |
| 2 |
use serde::{ser, Serialize, Serializer}; |
| 3 |
use thiserror::*; |
| 4 |
|
| 5 |
use std::fmt; |
| 6 |
|
| 7 |
struct KVString { |
| 8 |
output: String, |
| 9 |
} |
| 10 |
|
| 11 |
#[derive(Copy, Clone, PartialEq, Eq, Debug, Error)] |
| 12 |
enum Error { |
| 13 |
#[error("unsupported")] |
| 14 |
Unsupported, |
| 15 |
} |
| 16 |
|
| 17 |
impl ser::Error for Error { |
| 18 |
#[cold] |
| 19 |
fn custom<T: fmt::Display>(msg: T) -> Error { |
| 20 |
Error::Unsupported |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
/// Serialize a configuration as a Key-Value string. |
| 25 |
pub fn to_string<T>(value: &T) -> Result<String, ()> |
| 26 |
where |
| 27 |
T: Serialize, |
| 28 |
{ |
| 29 |
let mut serializer = KVString { output: String::new() }; |
| 30 |
value.serialize(&mut serializer).map_err(|_| ())?; |
| 31 |
Ok(serializer.output) |
| 32 |
} |
| 33 |
|
| 34 |
impl Serializer for &mut KVString { |
| 35 |
type Ok = (); |
| 36 |
type Error = Error; |
| 37 |
type SerializeSeq = ser::Impossible<(), Self::Error>; |
| 38 |
type SerializeTuple = ser::Impossible<(), Self::Error>; |
| 39 |
type SerializeTupleStruct = ser::Impossible<(), Self::Error>; |
| 40 |
type SerializeTupleVariant = ser::Impossible<(), Self::Error>; |
| 41 |
type SerializeMap = ser::Impossible<(), Self::Error>; |
| 42 |
type SerializeStruct = Self; |
| 43 |
type SerializeStructVariant = ser::Impossible<(), Self::Error>; |
| 44 |
|
| 45 |
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { |
| 46 |
self.output += if v { "true" } else { "false" }; |
| 47 |
Ok(()) |
| 48 |
} |
| 49 |
fn serialize_i8(self, v: i8) -> Result<(), Self::Error> { |
| 50 |
self.serialize_i64(i64::from(v)) |
| 51 |
} |
| 52 |
|
| 53 |
fn serialize_i16(self, v: i16) -> Result<(), Self::Error> { |
| 54 |
self.serialize_i64(i64::from(v)) |
| 55 |
} |
| 56 |
|
| 57 |
fn serialize_i32(self, v: i32) -> Result<(), Self::Error> { |
| 58 |
self.serialize_i64(i64::from(v)) |
| 59 |
} |
| 60 |
|
| 61 |
fn serialize_i64(self, v: i64) -> Result<(), Self::Error> { |
| 62 |
self.output += &v.to_string(); |
| 63 |
Ok(()) |
| 64 |
} |
| 65 |
|
| 66 |
fn serialize_u8(self, v: u8) -> Result<(), Self::Error> { |
| 67 |
self.serialize_u64(u64::from(v)) |
| 68 |
} |
| 69 |
|
| 70 |
fn serialize_u16(self, v: u16) -> Result<(), Self::Error> { |
| 71 |
self.serialize_u64(u64::from(v)) |
| 72 |
} |
| 73 |
|
| 74 |
fn serialize_u32(self, v: u32) -> Result<(), Self::Error> { |
| 75 |
self.serialize_u64(u64::from(v)) |
| 76 |
} |
| 77 |
|
| 78 |
fn serialize_u64(self, v: u64) -> Result<(), Self::Error> { |
| 79 |
self.output += &v.to_string(); |
| 80 |
Ok(()) |
| 81 |
} |
| 82 |
fn serialize_f32(self, v: f32) -> Result<(), Self::Error> { |
| 83 |
self.serialize_f64(f64::from(v)) |
| 84 |
} |
| 85 |
|
| 86 |
fn serialize_f64(self, v: f64) -> Result<(), Self::Error> { |
| 87 |
self.output += &v.to_string(); |
| 88 |
Ok(()) |
| 89 |
} |
| 90 |
fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { |
| 91 |
unimplemented!() |
| 92 |
} |
| 93 |
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
| 94 |
self.output += v; |
| 95 |
Ok(()) |
| 96 |
} |
| 97 |
|
| 98 |
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 99 |
unimplemented!() |
| 100 |
} |
| 101 |
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 102 |
self.output += "None"; |
| 103 |
Ok(()) |
| 104 |
} |
| 105 |
fn serialize_some<T: ?Sized>( |
| 106 |
self, value: &T, |
| 107 |
) -> Result<Self::Ok, Self::Error> |
| 108 |
where |
| 109 |
T: Serialize, |
| 110 |
{ |
| 111 |
value.serialize(self) |
| 112 |
} |
| 113 |
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 114 |
self.output += ""; |
| 115 |
Ok(()) |
| 116 |
} |
| 117 |
fn serialize_unit_struct( |
| 118 |
self, name: &'static str, |
| 119 |
) -> Result<Self::Ok, Self::Error> { |
| 120 |
self.serialize_unit() |
| 121 |
} |
| 122 |
|
| 123 |
fn serialize_unit_variant( |
| 124 |
self, name: &'static str, variant_index: u32, variant: &'static str, |
| 125 |
) -> Result<Self::Ok, Self::Error> { |
| 126 |
self.serialize_str(variant) |
| 127 |
} |
| 128 |
|
| 129 |
fn serialize_newtype_struct<T: ?Sized>( |
| 130 |
self, name: &'static str, value: &T, |
| 131 |
) -> Result<Self::Ok, Self::Error> |
| 132 |
where |
| 133 |
T: Serialize, |
| 134 |
{ |
| 135 |
unimplemented!() |
| 136 |
} |
| 137 |
|
| 138 |
fn serialize_newtype_variant<T: ?Sized>( |
| 139 |
self, name: &'static str, variant_index: u32, variant: &'static str, |
| 140 |
value: &T, |
| 141 |
) -> Result<Self::Ok, Self::Error> |
| 142 |
where |
| 143 |
T: Serialize, |
| 144 |
{ |
| 145 |
unimplemented!() |
| 146 |
} |
| 147 |
|
| 148 |
fn serialize_seq( |
| 149 |
self, len: Option<usize>, |
| 150 |
) -> Result<Self::SerializeSeq, Self::Error> { |
| 151 |
unimplemented!() |
| 152 |
} |
| 153 |
fn serialize_tuple( |
| 154 |
self, len: usize, |
| 155 |
) -> Result<Self::SerializeTuple, Self::Error> { |
| 156 |
unimplemented!() |
| 157 |
} |
| 158 |
fn serialize_tuple_struct( |
| 159 |
self, name: &'static str, len: usize, |
| 160 |
) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 161 |
unimplemented!() |
| 162 |
} |
| 163 |
fn serialize_tuple_variant( |
| 164 |
self, name: &'static str, variant_index: u32, variant: &'static str, |
| 165 |
len: usize, |
| 166 |
) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 167 |
unimplemented!() |
| 168 |
} |
| 169 |
fn serialize_map( |
| 170 |
self, len: Option<usize>, |
| 171 |
) -> Result<Self::SerializeMap, Self::Error> { |
| 172 |
unimplemented!() |
| 173 |
} |
| 174 |
fn serialize_struct( |
| 175 |
self, name: &'static str, len: usize, |
| 176 |
) -> Result<Self::SerializeStruct, Self::Error> { |
| 177 |
Ok(self) |
| 178 |
} |
| 179 |
fn serialize_struct_variant( |
| 180 |
self, name: &'static str, variant_index: u32, variant: &'static str, |
| 181 |
len: usize, |
| 182 |
) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 183 |
unimplemented!() |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
impl<'a> ser::SerializeStruct for &mut KVString { |
| 188 |
type Ok = (); |
| 189 |
type Error = Error; |
| 190 |
|
| 191 |
// Assume a single flat struct for now |
| 192 |
fn serialize_field<T>( |
| 193 |
&mut self, key: &'static str, value: &T, |
| 194 |
) -> Result<(), Self::Error> |
| 195 |
where |
| 196 |
T: ?Sized + Serialize, |
| 197 |
{ |
| 198 |
if !self.output.is_empty() { |
| 199 |
self.output += ", " |
| 200 |
} |
| 201 |
self.output += key; |
| 202 |
self.output += " = "; |
| 203 |
value.serialize(&mut **self) |
| 204 |
} |
| 205 |
|
| 206 |
fn end(self) -> Result<(), Self::Error> { |
| 207 |
Ok(()) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
#[cfg(test)] |
| 212 |
mod test { |
| 213 |
use rav1e::prelude::SpeedSettings; |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn serialize_speed_settings() { |
| 217 |
for preset in 0..=10 { |
| 218 |
let s = SpeedSettings::from_preset(preset); |
| 219 |
let out = super::to_string(&s).unwrap(); |
| 220 |
println!("preset {}: {}", preset, out); |
| 221 |
} |
| 222 |
} |
| 223 |
} |