|
| 1 | +use serde::{de, Deserialize}; |
| 2 | +use thiserror::Error; |
| 3 | +use winnow::{stream::Recoverable, Located}; |
| 4 | + |
| 5 | +use crate::{v2_parser::KdlParseError, KdlParseFailure}; |
| 6 | + |
| 7 | +/// serde deserializer for KDL documents |
| 8 | +#[derive(Debug)] |
| 9 | +pub struct Deserializer<'de> { |
| 10 | + input: Recoverable<Located<&'de str>, KdlParseError>, |
| 11 | +} |
| 12 | + |
| 13 | +impl<'de> Deserializer<'de> { |
| 14 | + /// Create a new deserializer from a string |
| 15 | + pub fn from_str(input: &'de str) -> Self { |
| 16 | + Self { |
| 17 | + input: Recoverable::new(Located::new(input)), |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// Deserialize a type from a KDL string |
| 23 | +pub fn from_str<'a, T>(input: &'a str) -> Result<T, KdlParseFailure> |
| 24 | +where |
| 25 | + T: Deserialize<'a>, |
| 26 | +{ |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Error)] |
| 30 | +struct DeError(String); |
| 31 | + |
| 32 | +impl std::fmt::Display for DeError { |
| 33 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 34 | + write!(f, "{}", self.0) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl de::Error for DeError { |
| 39 | + fn custom<T: std::fmt::Display>(msg: T) -> Self { |
| 40 | + DeError(msg.to_string()) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +struct KdlVisitor; |
| 45 | + |
| 46 | +impl<'de> de::Visitor<'de> for KdlVisitor { |
| 47 | + type Value = (); |
| 48 | + |
| 49 | + fn expecting<'a>(&self, formatter: &mut std::fmt::Formatter<'a>) -> std::fmt::Result { |
| 50 | + write!(formatter, "a KDL value") |
| 51 | + } |
| 52 | + |
| 53 | + fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 54 | + where |
| 55 | + A: de::MapAccess<'de>, |
| 56 | + { |
| 57 | + while let Some(key) = map.next_key()? { |
| 58 | + match key { |
| 59 | + "type" => { |
| 60 | + let value = map.next_value::<String>()?; |
| 61 | + println!("type: {}", value); |
| 62 | + } |
| 63 | + "value" => { |
| 64 | + let value = map.next_value::<String>()?; |
| 65 | + println!("value: {}", value); |
| 66 | + } |
| 67 | + _ => { |
| 68 | + map.next_value::<serde::de::IgnoredAny>()?; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { |
| 78 | + type Error = DeError; |
| 79 | + |
| 80 | + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 81 | + where |
| 82 | + V: de::Visitor<'de>, |
| 83 | + { |
| 84 | + self.deserialize_map(visitor) |
| 85 | + } |
| 86 | + |
| 87 | + fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 88 | + where |
| 89 | + V: de::Visitor<'de>, |
| 90 | + { |
| 91 | + todo!() |
| 92 | + } |
| 93 | + |
| 94 | + fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 95 | + where |
| 96 | + V: de::Visitor<'de>, |
| 97 | + { |
| 98 | + todo!() |
| 99 | + } |
| 100 | + |
| 101 | + fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 102 | + where |
| 103 | + V: de::Visitor<'de>, |
| 104 | + { |
| 105 | + todo!() |
| 106 | + } |
| 107 | + |
| 108 | + fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 109 | + where |
| 110 | + V: de::Visitor<'de>, |
| 111 | + { |
| 112 | + todo!() |
| 113 | + } |
| 114 | + |
| 115 | + fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 116 | + where |
| 117 | + V: de::Visitor<'de>, |
| 118 | + { |
| 119 | + todo!() |
| 120 | + } |
| 121 | + |
| 122 | + fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 123 | + where |
| 124 | + V: de::Visitor<'de>, |
| 125 | + { |
| 126 | + todo!() |
| 127 | + } |
| 128 | + |
| 129 | + fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 130 | + where |
| 131 | + V: de::Visitor<'de>, |
| 132 | + { |
| 133 | + todo!() |
| 134 | + } |
| 135 | + |
| 136 | + fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 137 | + where |
| 138 | + V: de::Visitor<'de>, |
| 139 | + { |
| 140 | + todo!() |
| 141 | + } |
| 142 | + |
| 143 | + fn deserialize_unit_struct<V>( |
| 144 | + self, |
| 145 | + name: &'static str, |
| 146 | + visitor: V, |
| 147 | + ) -> Result<V::Value, Self::Error> |
| 148 | + where |
| 149 | + V: de::Visitor<'de>, |
| 150 | + { |
| 151 | + todo!() |
| 152 | + } |
| 153 | + |
| 154 | + fn deserialize_newtype_struct<V>( |
| 155 | + self, |
| 156 | + name: &'static str, |
| 157 | + visitor: V, |
| 158 | + ) -> Result<V::Value, Self::Error> |
| 159 | + where |
| 160 | + V: de::Visitor<'de>, |
| 161 | + { |
| 162 | + todo!() |
| 163 | + } |
| 164 | + |
| 165 | + fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 166 | + where |
| 167 | + V: de::Visitor<'de>, |
| 168 | + { |
| 169 | + todo!() |
| 170 | + } |
| 171 | + |
| 172 | + fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
| 173 | + where |
| 174 | + V: de::Visitor<'de>, |
| 175 | + { |
| 176 | + todo!() |
| 177 | + } |
| 178 | + |
| 179 | + fn deserialize_tuple_struct<V>( |
| 180 | + self, |
| 181 | + name: &'static str, |
| 182 | + len: usize, |
| 183 | + visitor: V, |
| 184 | + ) -> Result<V::Value, Self::Error> |
| 185 | + where |
| 186 | + V: de::Visitor<'de>, |
| 187 | + { |
| 188 | + todo!() |
| 189 | + } |
| 190 | + |
| 191 | + fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 192 | + where |
| 193 | + V: de::Visitor<'de>, |
| 194 | + { |
| 195 | + todo!() |
| 196 | + } |
| 197 | + |
| 198 | + fn deserialize_struct<V>( |
| 199 | + self, |
| 200 | + name: &'static str, |
| 201 | + fields: &'static [&'static str], |
| 202 | + visitor: V, |
| 203 | + ) -> Result<V::Value, Self::Error> |
| 204 | + where |
| 205 | + V: de::Visitor<'de>, |
| 206 | + { |
| 207 | + todo!() |
| 208 | + } |
| 209 | + |
| 210 | + fn deserialize_enum<V>( |
| 211 | + self, |
| 212 | + name: &'static str, |
| 213 | + variants: &'static [&'static str], |
| 214 | + visitor: V, |
| 215 | + ) -> Result<V::Value, Self::Error> |
| 216 | + where |
| 217 | + V: de::Visitor<'de>, |
| 218 | + { |
| 219 | + todo!() |
| 220 | + } |
| 221 | + |
| 222 | + fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 223 | + where |
| 224 | + V: de::Visitor<'de>, |
| 225 | + { |
| 226 | + todo!() |
| 227 | + } |
| 228 | + |
| 229 | + fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
| 230 | + where |
| 231 | + V: de::Visitor<'de>, |
| 232 | + { |
| 233 | + todo!() |
| 234 | + } |
| 235 | +} |
0 commit comments