1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
use super::*; use std::{ error, fmt, ops::{Deref, DerefMut}, }; const INVALID: &str = "(){}[]<> ,./\\="; /// The atomic value. /// /// `Kserd` contains an optional identity (`id`), and the [`Value`]. Taken together the `Kserd` object can /// describe (almost) all Rust data structures. The identity can be mostly ignored for self-explanatory /// data such as primitives. It can be used to name structures, and plays a special /// role in unit structs and enum variants. /// /// `Kserd` dereferences mutably to [`Value`] so all reading and mutating methods on [`Value`] can /// use directly. It is encouraged to use the methods rather than accessing the fields directly. /// /// `Kserd` implements ordering and equality, it is important to note that ordering and equality is /// done **only on the value**, the identity is ignored. /// /// # Example /// Use the methods to quickly see the data if the type is known. Mutating can be done directly. /// ```rust /// # use kserd::*; /// let mut kserd = Kserd::new_str("Hi"); /// /// // access the data without needing to pattern match /// assert_eq!(kserd.str(), Some("Hi")); /// // using ctors will name your ds for you /// assert_eq!(kserd.id(), Some("str")); /// // if not of the type, nothing returned. /// assert_eq!(kserd.int(), None); /// /// kserd.str_mut().map(|s| { /// s.pop(); /// s.push_str("ello, world!"); /// }); // mutate directly. /// // this method has the additional effect of cloning the string. /// assert_eq!(kserd.str(), Some("Hello, world!")); /// ``` /// /// [`Value`]: crate::Value pub struct Kserd<'a> { /// The _identity_ of the value. /// /// This can be optional. /// Typically the identity is something like the struct or enum name. pub id: Option<Kstr<'a>>, /// The [`Value`]. /// /// This store the actual value, which comprises of primitive types or more complex /// nested types such as sequences or maps. /// /// [`Value`]: crate::Value pub val: Value<'a>, } /// Static lifetime constructors. These do not need a [`Serialize`] trait. /// /// [`Serialize`]: crate::encode::Serialize impl Kserd<'static> { /// A new unit value `()`. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_unit(); /// assert_eq!(kserd.unit(), true); /// assert_eq!(kserd.id(), None); /// ``` pub const fn new_unit() -> Self { Kserd::new(Value::Unit) } /// A new boolean value. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_bool(true); /// assert_eq!(kserd.bool(), Some(true)); /// assert_eq!(kserd.id(), Some("bool")); /// ``` pub fn new_bool(value: bool) -> Self { Kserd::with_id_unchk("bool", Value::Bool(value)) } /// A new number value. The trait [`NumberType`] is implemented on all Rust primitive /// numbers so number literals can be used. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_num(123456); /// assert_eq!(kserd.uint(), Some(123456)); /// /// let kserd = Kserd::new_num(-123456); /// assert_eq!(kserd.int(), Some(-123456)); /// /// let kserd = Kserd::new_num(3.14); /// assert_eq!(kserd.float(), Some(3.14)); /// ``` pub fn new_num<T: NumberType>(value: T) -> Self { Kserd::with_id_unchk(value.identity(), Value::new_num(value)) } /// A new string value. The ownership of the string is transferred and as such /// the `Kserd` has a _static_ lifetime. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_string(String::from("Hello, world!")); /// assert_eq!(kserd.str(), Some("Hello, world!")); /// assert_eq!(kserd.id(), Some("String")); /// ``` pub fn new_string(string: String) -> Self { Kserd::with_id_unchk("String", Value::new_string(string)) } /// A new byte array value. The ownership of the vector is transferred and as such the `Kserd` /// has a _static_ lifetime. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_barrv(vec![0,1,2,3]); /// assert_eq!(kserd.barr(), Some([0,1,2,3].as_ref())); /// assert_eq!(kserd.id(), Some("ByteVec")); /// ``` pub fn new_barrv(byte_array: Vec<u8>) -> Self { Kserd::with_id_unchk("ByteVec", Value::new_barrv(byte_array)) } } /// General lifetime contructors. These _do not need_ a [`Serialize`] trait. /// /// [`Serialize`]: crate::encode::Serialize impl<'a> Kserd<'a> { /// A new `Kserd` with the specified [`Value`]. /// No identity is ascribed to the `Kserd`. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new(Value::Unit); /// assert_eq!(kserd.val, Value::Unit); /// assert_eq!(kserd.id, None); /// ``` /// /// [`Value`]: crate::Value pub const fn new(value: Value<'a>) -> Self { Self { id: None, val: value, } } /// A new `Kserd` with a specified identity and [`Value`]. /// /// The identity is taken as a [`Kstr`], meaning it can be a reference or owned. /// `String` and `&str` can be used as the identity. The identity must also be /// valid, that is, it cannot contain certain characters or `Err` will be returned. /// /// > It is recommended to construct `Kserd` in this manner rather than manually /// via setting the `id` and `val` fields as an invalid identity will not parse back /// from text. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::with_id("an-identity", Value::Bool(true)).unwrap(); /// assert_eq!(kserd.bool(), Some(true)); /// assert_eq!(kserd.id(), Some("an-identity")); /// /// let kserd = Kserd::with_id("<an,> in(valid.)/{identity}\\=", Value::Unit); /// assert_eq!(kserd.is_err(), true); /// ``` /// /// [`Value`]: crate::Value /// [`Kstr`]: crate::Kstr pub fn with_id<S: Into<Kstr<'a>>>(identity: S, value: Value<'a>) -> Result<Self, InvalidId> { let id = identity.into(); if id.chars().any(|c| INVALID.contains(c)) { Err(InvalidId(id.to_string())) } else { Ok(Self { id: Some(id), val: value, }) } } /// Much like `with_id` but does _not_ check the identity for validity. pub(crate) fn with_id_unchk<S: Into<Kstr<'a>>>(identity: S, value: Value<'a>) -> Self { Self { id: Some(identity.into()), val: value, } } /// A new string value. The `Kserd` borrows the string and has the same lifetime. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_str("Hello, world!"); /// assert_eq!(kserd.str(), Some("Hello, world!")); /// assert_eq!(kserd.id(), Some("str")); /// ``` pub fn new_str(string: &'a str) -> Self { Kserd::with_id_unchk("str", Value::new_str(string)) } /// A new byte array value. The `Kserd` borrows the array and has the same lifetime. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_barr([0,1,2,5,10].as_ref()); /// assert_eq!(kserd.barr(), Some([0,1,2,5,10].as_ref())); /// assert_eq!(kserd.id(), Some("barr")); /// ``` pub fn new_barr(byte_array: &'a [u8]) -> Self { Kserd::with_id_unchk("barr", Value::new_barr(byte_array)) } /// Construct a new container `Kserd` from a list of field-value pairs. /// /// # Example /// ```rust /// # use kserd::*; /// let pass = Kserd::new_cntr(vec![ /// ("a", Kserd::new_num(0)) /// ]).unwrap(); /// /// let fail = Kserd::new_cntr(vec![ /// ("1 wrong/name", Kserd::new_num(0)) /// ]); /// assert_eq!(fail.is_err(), true); /// ``` pub fn new_cntr<I, S>(iter: I) -> Result<Self, InvalidFieldName> where S: Into<Kstr<'a>>, I: IntoIterator<Item = (S, Kserd<'a>)>, { Ok(Kserd::new(Value::new_cntr(iter)?)) } /// Construct a new map value from a list of key-value pairs. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_map(vec![ /// (Kserd::new_unit(), Kserd::new_num(0)) /// ]); /// ``` pub fn new_map<I>(iter: I) -> Self where I: IntoIterator<Item = (Kserd<'a>, Kserd<'a>)>, { Kserd::new(Value::new_map(iter)) } } impl<'a> Kserd<'a> { /// The identity. Same as the `.id` field but mapped as a `&str`. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::with_id("Hello", Value::Unit).unwrap(); /// assert_eq!(kserd.id(), Some("Hello")); /// ``` pub fn id(&self) -> Option<&str> { self.id.as_ref().map(|x| x.as_str()) } } impl<'a> Deref for Kserd<'a> { type Target = Value<'a>; fn deref(&self) -> &Value<'a> { &self.val } } impl<'a> DerefMut for Kserd<'a> { fn deref_mut(&mut self) -> &mut Value<'a> { &mut self.val } } /// Conversions. impl<'a> Kserd<'a> { /// Clones all data to make a static `Kserd`. pub fn into_owned(self) -> Kserd<'static> { let Kserd { id, val } = self; let id = id.map(|x| x.into_owned()); let val = val.into_owned(); Kserd { id, val } } /// Makes a copy of this `Kserd` that references data in the this `Kserd`. /// /// This is particularly useful if you want to gaurantee that _all_ data is of the borrowed /// variety when decoding back to a data structure (see [`Decoder`] for explanation). /// /// There is a performance penalty as nested structures have to be rebuilt. /// /// # Example /// ```rust /// # use kserd::*; /// let kserd = Kserd::new_string("Hello, world!".to_owned()); /// let brwed = kserd.mk_brw(); /// assert_eq!(kserd, brwed); /// ``` /// /// [`Decoder`]: crate::encode::Decoder pub fn mk_brw(&self) -> Kserd { let id = self.id().map(Kstr::brwed); let val = self.val.mk_brw(); Kserd { id, val } } } impl<'a> PartialEq for Kserd<'a> { fn eq(&self, other: &Kserd) -> bool { self.val == other.val // equality ignores identity, only value based } } impl<'a> Eq for Kserd<'a> {} impl<'a> PartialOrd for Kserd<'a> { fn partial_cmp(&self, other: &Kserd<'a>) -> Option<std::cmp::Ordering> { Some(self.cmp(other)) } } impl<'a> Ord for Kserd<'a> { fn cmp(&self, other: &Kserd<'a>) -> std::cmp::Ordering { self.val.cmp(&other.val) } } impl<'a> Clone for Kserd<'a> { fn clone(&self) -> Self { let id = self.id.clone(); let val = self.val.clone(); Kserd { id, val } } } impl<'a> fmt::Debug for Kserd<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { struct Id<'b>(Option<&'b str>); impl<'b> fmt::Debug for Id<'b> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if let Some(id) = self.0 { write!(f, "Some({:?})", id) } else { write!(f, "None") } } } f.debug_struct("Kserd") .field("id", &Id(self.id())) .field("val", &self.val) .finish() } } /// The id has invalid characters and would not parse back. #[derive(Debug, PartialEq, Eq, Clone)] pub struct InvalidId(String); impl error::Error for InvalidId {} impl fmt::Display for InvalidId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, r#"identity '{}' contains invalid characters. Invalid characters: '{}'"#, self.0, INVALID ) } } #[cfg(test)] mod tests { use super::*; #[test] fn new_barrv_test() { let kserd = Kserd::new_barrv(vec![0, 1, 2, 3]); assert_eq!(kserd.barr(), Some([0, 1, 2, 3].as_ref())); assert_eq!(kserd.id(), Some("ByteVec")); } #[test] fn invalid_id_test() { let kserd = Kserd::with_id("an-identity", Value::Bool(true)).unwrap(); assert_eq!(kserd.bool(), Some(true)); assert_eq!(kserd.id(), Some("an-identity")); let kserd = Kserd::with_id("<an,> in(valid.)/{identity}\\=", Value::Unit); assert_eq!(kserd.is_err(), true); } #[test] fn deref_mut_test() { let mut kserd = Kserd::new_bool(true); let val: &mut Value = &mut kserd; *val = Value::new_num(123); assert_eq!(kserd.uint(), Some(123)); } #[test] fn partial_cmp_test() { let kserd1 = Kserd::new_num(0); let kserd2 = Kserd::new_num(1); assert!(kserd1 < kserd2); } #[test] fn debug_fmt_test() { let kserd = Kserd::new(Value::Bool(false)); let s = format!("{:?}", kserd); assert_eq!(&s, "Kserd { id: None, val: Bool(false) }"); } #[test] fn invalid_id_formatting_test() { let kserd = Kserd::with_id("\\// ", Value::Bool(true)).map_err(|e| e.to_string()); assert_eq!(kserd, Err(r#"identity '\// ' contains invalid characters. Invalid characters: '(){}[]<> ,./\='"#.to_owned())); } }