oxedyne/fe2o3/fe2o3_jdat/src/derive.rs
4.3 KiB, 1 run
created by r1870400018:75861, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Support for the code `#[derive(ToDat, FromDat)]` writes. Not for direct use: the names here |
| 2 | //! may change with the derive. The encoding is described on the derive, in `dat_map`. |
| 3 | |
| 4 | use crate::{ |
| 5 | daticle::Daticle, |
| 6 | conv::{ |
| 7 | FromDat, |
| 8 | ToDat, |
| 9 | }, |
| 10 | }; |
| 11 | |
| 12 | use oxedyne_fe2o3_core::prelude::*; |
| 13 | |
| 14 | pub use crate::{ |
| 15 | Dat, |
| 16 | map::DaticleMap, |
| 17 | }; |
| 18 | pub use oxedyne_fe2o3_core::Outcome; |
| 19 | |
| 20 | |
| 21 | /// Encodes one field, naming the type and field in any failure. |
| 22 | pub fn enc<T: ToDat>(v: &T, ty: &str, at: &str) -> Outcome<Dat> { |
| 23 | match v.to_dat() { |
| 24 | Ok(dat) => Ok(dat), |
| 25 | Err(e) => Err(err!(e, "Encoding {}, field '{}'.", ty, at; Encode)), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | pub fn put(map: &mut DaticleMap, key: &str, dat: Dat) { |
| 30 | map.insert(Dat::Str(key.to_string()), dat); |
| 31 | } |
| 32 | |
| 33 | /// An enum variant on the wire: a one-entry map from its name to its payload. |
| 34 | pub fn variant(name: &str, payload: Dat) -> Dat { |
| 35 | let mut map = DaticleMap::new(); |
| 36 | map.insert(Dat::Str(name.to_string()), payload); |
| 37 | Dat::Map(map) |
| 38 | } |
| 39 | |
| 40 | pub fn take_map(dat: Dat, ty: &str) -> Outcome<DaticleMap> { |
| 41 | match dat { |
| 42 | Dat::Map(map) => Ok(map), |
| 43 | other => Err(err!( |
| 44 | "Decoding {}: a map of fields was expected, a {:?} was found.", ty, other.kind(); |
| 45 | Decode, Mismatch)), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Takes and decodes the named field, refusing it when missing. |
| 50 | pub fn field<T: FromDat>(map: &mut DaticleMap, ty: &str, key: &str) -> Outcome<T> { |
| 51 | match map.remove(&Dat::Str(key.to_string())) { |
| 52 | Some(dat) => match T::from_dat(dat) { |
| 53 | Ok(v) => Ok(v), |
| 54 | Err(e) => Err(err!(e, "Decoding {}, field '{}'.", ty, key; Decode)), |
| 55 | }, |
| 56 | None => Err(err!("Decoding {}: field '{}' is missing.", ty, key; Decode, Missing)), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Refuses any field left over once every known field has been taken. |
| 61 | pub fn done(map: DaticleMap, ty: &str) -> Outcome<()> { |
| 62 | if map.is_empty() { |
| 63 | return Ok(()); |
| 64 | } |
| 65 | let keys: Vec<String> = map.keys().map(|k| fmt!("{:?}", k)).collect(); |
| 66 | Err(err!("Decoding {}: unknown field(s) {}.", ty, keys.join(", "); Decode, Unknown)) |
| 67 | } |
| 68 | |
| 69 | pub fn take_list(dat: Dat, ty: &str, n: usize) -> Outcome<std::vec::IntoIter<Dat>> { |
| 70 | let list = match dat { |
| 71 | Dat::List(list) => list, |
| 72 | other => return Err(err!( |
| 73 | "Decoding {}: a list of {} fields was expected, a {:?} was found.", ty, n, other.kind(); |
| 74 | Decode, Mismatch)), |
| 75 | }; |
| 76 | if list.len() != n { |
| 77 | return Err(err!( |
| 78 | "Decoding {}: a list of {} fields was expected, one of {} was found.", ty, n, list.len(); |
| 79 | Decode, Size, Mismatch)); |
| 80 | } |
| 81 | Ok(list.into_iter()) |
| 82 | } |
| 83 | |
| 84 | pub fn item<T: FromDat>(it: &mut std::vec::IntoIter<Dat>, ty: &str, i: usize) -> Outcome<T> { |
| 85 | match it.next() { |
| 86 | Some(dat) => match T::from_dat(dat) { |
| 87 | Ok(v) => Ok(v), |
| 88 | Err(e) => Err(err!(e, "Decoding {}, field {}.", ty, i; Decode)), |
| 89 | }, |
| 90 | None => Err(err!("Decoding {}: field {} is missing.", ty, i; Decode, Missing)), |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | pub fn take_unit(dat: Dat, ty: &str) -> Outcome<()> { |
| 95 | match dat { |
| 96 | Dat::Empty => Ok(()), |
| 97 | other => Err(err!( |
| 98 | "Decoding {}: an empty daticle was expected, a {:?} was found.", ty, other.kind(); |
| 99 | Decode, Mismatch)), |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// The name and payload of an enum variant: a map of exactly one entry, keyed by a string. |
| 104 | pub fn take_variant(dat: Dat, ty: &str) -> Outcome<(String, Dat)> { |
| 105 | let map = match dat { |
| 106 | Dat::Map(map) => map, |
| 107 | other => return Err(err!( |
| 108 | "Decoding {}: a one-entry variant map was expected, a {:?} was found.", |
| 109 | ty, other.kind(); |
| 110 | Decode, Mismatch)), |
| 111 | }; |
| 112 | if map.len() != 1 { |
| 113 | return Err(err!( |
| 114 | "Decoding {}: a variant map holds one entry, this one holds {}.", ty, map.len(); |
| 115 | Decode, Size, Mismatch)); |
| 116 | } |
| 117 | match map.into_iter().next() { |
| 118 | Some((Dat::Str(name), payload)) => Ok((name, payload)), |
| 119 | Some((key, _)) => Err(err!( |
| 120 | "Decoding {}: a variant is named by a string, not a {:?}.", ty, key.kind(); |
| 121 | Decode, Mismatch)), |
| 122 | None => Err(err!("Decoding {}: the variant map is empty.", ty; Decode, Missing)), |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | pub fn unknown_variant(ty: &str, name: &str) -> Error<ErrTag> { |
| 127 | err!("Decoding {}: there is no variant '{}'.", ty, name; Decode, Unknown) |
| 128 | } |