votable 0.7.0

Rust implementation of a VOTable serializer/deserializer with support for format other than XML, such as JSON, TOML or YAML.
Documentation
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
//! Contains common code for `ATTRIBUTE` child of `COLLECTION` and `INSTANCE`
//! in both `GLOBALS` and `TEMPLATES`.

use std::str;

use paste::paste;

use crate::{
  error::VOTableError, mivot::VodmlVisitor, utils::unexpected_attr_err, EmptyElem, VOTableElement,
};

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum RefOrValueOrBoth {
  Ref {
    #[serde(rename = "ref")]
    ref_: String,
  },
  Value {
    value: String,
  },
  /// In this pattern, the value has the role of a default value
  /// in case the referenced `FIELD` contains a NULL value or if the
  /// the referenced `PARAM` does not exists.
  RefAndValue {
    #[serde(rename = "ref")]
    ref_: String,
    value: String,
  },
}

impl RefOrValueOrBoth {
  pub fn from_ref<N: Into<String>>(ref_: N) -> Self {
    RefOrValueOrBoth::Ref { ref_: ref_.into() }
  }

  pub fn from_value<N: Into<String>>(value: N) -> Self {
    RefOrValueOrBoth::Value {
      value: value.into(),
    }
  }

  pub fn from_ref_with_default<N: Into<String>>(ref_: N, default_value: N) -> Self {
    RefOrValueOrBoth::RefAndValue {
      ref_: ref_.into(),
      value: default_value.into(),
    }
  }

  pub fn from_possibly_empty_ref_or_val(ref_: String, value: String) -> Result<Self, VOTableError> {
    match ((!ref_.is_empty() as u8) << 1) + (!value.is_empty() as u8) {
      0 => Err(VOTableError::Custom(String::from(
        "Attributes 'ref' and 'value' are both empty in tag ATTRIBUTE",
      ))),
      1 => Ok(Self::from_value(value)),
      2 => Ok(Self::from_ref(ref_)),
      3 => Ok(Self::from_ref_with_default(ref_, value)),
      _ => unreachable!(),
    }
  }

  fn for_each_attribute<F>(&self, mut f: F)
  where
    F: FnMut(&str, &str),
  {
    match self {
      Self::Ref { ref_ } => f("ref", ref_.as_str()),
      Self::Value { value } => f("value", value.as_str()),
      Self::RefAndValue { ref_, value } => {
        f("ref", ref_.as_str());
        f("value", value.as_str())
      }
    };
  }
}

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AttributeChildOfInstance {
  /// Attribute name.
  pub dmrole: String,
  /// Attribute type.
  pub dmtype: String,
  /// Reference (to a PARAM) or value of the attribute.
  #[serde(flatten)]
  pub ref_or_val_or_both: RefOrValueOrBoth,
  /// In case the value/referenced param is an array,
  /// provide the index of the attribute value inh the array.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub arrayindex: Option<u32>,
  /// Unit of the attribute.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub unit: Option<String>,
}
impl AttributeChildOfInstance {
  pub fn new<N: Into<String>>(dmrole: N, dmtype: N, ref_or_val_or_both: RefOrValueOrBoth) -> Self {
    Self {
      dmrole: dmrole.into(),
      dmtype: dmtype.into(),
      ref_or_val_or_both,
      arrayindex: None,
      unit: None,
    }
  }

  pub fn from_ref<N: Into<String>>(dmrole: N, dmtype: N, ref_: N) -> Self {
    Self {
      dmrole: dmrole.into(),
      dmtype: dmtype.into(),
      ref_or_val_or_both: RefOrValueOrBoth::from_ref(ref_),
      arrayindex: None,
      unit: None,
    }
  }

  pub fn from_val<N: Into<String>>(dmrole: N, dmtype: N, value: N) -> Self {
    Self {
      dmrole: dmrole.into(),
      dmtype: dmtype.into(),
      ref_or_val_or_both: RefOrValueOrBoth::from_value(value),
      arrayindex: None,
      unit: None,
    }
  }

  pub fn from_ref_with_default<N: Into<String>>(
    dmrole: N,
    dmtype: N,
    ref_: N,
    default_value: N,
  ) -> Self {
    Self {
      dmrole: dmrole.into(),
      dmtype: dmtype.into(),
      ref_or_val_or_both: RefOrValueOrBoth::from_ref_with_default(ref_, default_value),
      arrayindex: None,
      unit: None,
    }
  }

  impl_builder_mandatory_string_attr!(dmrole);
  impl_builder_mandatory_string_attr!(dmtype);
  impl_builder_mandatory_attr!(ref_or_val_or_both, RefOrValueOrBoth);
  impl_builder_opt_attr!(arrayindex, u32);
  impl_builder_opt_string_attr!(unit);

  pub fn visit<V: VodmlVisitor>(&mut self, visitor: &mut V) -> Result<(), V::E> {
    visitor.visit_attribute_childof_instance(self)
  }
}

impl VOTableElement for AttributeChildOfInstance {
  const TAG: &'static str = "ATTRIBUTE";

  type MarkerType = EmptyElem;

  fn from_attrs<K, V, I>(attrs: I) -> Result<Self, VOTableError>
  where
    K: AsRef<str> + Into<String>,
    V: AsRef<str> + Into<String>,
    I: Iterator<Item = (K, V)>,
  {
    const DEFAULT_VALUE: &str = "@TBD";
    Self::from_ref(DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE).set_attrs(attrs).and_then(|attr| {
      if attr.dmrole.as_str() == DEFAULT_VALUE {
        Err(VOTableError::Custom(format!(
          "Mandatory attribute 'dmrole' not found in tag '{}' child of INSTANCE child of GLOBALS",
          Self::TAG
        )))
      } else if attr.dmtype.as_str() == DEFAULT_VALUE {
        Err(VOTableError::Custom(format!(
          "Mandatory attribute 'dmtype' not found in tag '{}' child of INSTANCE child of GLOBALS",
          &Self::TAG
        )))
      } else if let RefOrValueOrBoth::Ref { ref_: e } = &attr.ref_or_val_or_both {
        if e.as_str() == DEFAULT_VALUE {
          Err(VOTableError::Custom(format!(
            "Mandatory attributes 'ref' or 'value' not found in tag '{}' child of INSTANCE child of GLOBALS",
            &Self::TAG
          )))
        } else {
          Ok(attr)
        }
      } else {
        Ok(attr)
      }
    })
  }

  fn set_attrs_by_ref<K, V, I>(&mut self, attrs: I) -> Result<(), VOTableError>
  where
    K: AsRef<str> + Into<String>,
    V: AsRef<str> + Into<String>,
    I: Iterator<Item = (K, V)>,
  {
    let mut ref_ = String::from("");
    let mut value = String::from("");
    for (key, val) in attrs {
      let key = key.as_ref();
      match key {
        "dmrole" => self.set_dmrole_by_ref(val),
        "dmtype" => self.set_dmtype_by_ref(val),
        "ref" => ref_ = val.into(),
        "value" => value = val.into(),
        "arrayindex" => {
          self.set_arrayindex_by_ref(val.as_ref().parse().map_err(VOTableError::ParseInt)?)
        }
        "unit" => self.set_unit_by_ref(val),
        _ => return Err(unexpected_attr_err(key, Self::TAG)),
      }
    }
    self.set_ref_or_val_or_both_by_ref(RefOrValueOrBoth::from_possibly_empty_ref_or_val(
      ref_, value,
    )?);
    Ok(())
  }

  fn for_each_attribute<F>(&self, mut f: F)
  where
    F: FnMut(&str, &str),
  {
    f("dmrole", self.dmrole.as_str());
    f("dmtype", self.dmtype.as_str());
    self.ref_or_val_or_both.for_each_attribute(&mut f);
    if let Some(arrayindex) = &self.arrayindex {
      f("arrayindex", arrayindex.to_string().as_str());
    }
    if let Some(unit) = &self.unit {
      f("unit", unit.as_str());
    }
  }
}

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AttributeChildOfCollection {
  /// Attribute type.
  pub dmtype: String,
  /// Reference (to a PARAM) or value of the attribute.
  #[serde(flatten)]
  pub ref_or_val_or_both: RefOrValueOrBoth,
  /// In case the value/referenced param is an array,
  /// provide the index of the attribute value inh the array.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub arrayindex: Option<u32>,
  /// Unit of the attribute.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub unit: Option<String>,
}
impl AttributeChildOfCollection {
  pub fn new<N: Into<String>>(dmtype: N, ref_or_val_or_both: RefOrValueOrBoth) -> Self {
    Self {
      dmtype: dmtype.into(),
      ref_or_val_or_both,
      arrayindex: None,
      unit: None,
    }
  }

  pub fn from_ref<N: Into<String>>(dmtype: N, ref_: N) -> Self {
    Self {
      dmtype: dmtype.into(),
      ref_or_val_or_both: RefOrValueOrBoth::from_ref(ref_),
      arrayindex: None,
      unit: None,
    }
  }

  pub fn from_val<N: Into<String>>(dmtype: N, value: N) -> Self {
    Self {
      dmtype: dmtype.into(),
      ref_or_val_or_both: RefOrValueOrBoth::from_value(value),
      arrayindex: None,
      unit: None,
    }
  }

  pub fn from_ref_with_default<N: Into<String>>(dmtype: N, ref_: N, default_value: N) -> Self {
    Self {
      dmtype: dmtype.into(),
      ref_or_val_or_both: RefOrValueOrBoth::from_ref_with_default(ref_, default_value),
      arrayindex: None,
      unit: None,
    }
  }

  impl_builder_mandatory_string_attr!(dmtype);
  impl_builder_mandatory_attr!(ref_or_val_or_both, RefOrValueOrBoth);
  impl_builder_opt_attr!(arrayindex, u32);
  impl_builder_opt_string_attr!(unit);

  pub fn visit<V: VodmlVisitor>(&mut self, visitor: &mut V) -> Result<(), V::E> {
    visitor.visit_attribute_childof_collection(self)
  }
}

impl VOTableElement for AttributeChildOfCollection {
  const TAG: &'static str = "ATTRIBUTE";

  type MarkerType = EmptyElem;

  fn from_attrs<K, V, I>(attrs: I) -> Result<Self, VOTableError>
  where
    K: AsRef<str> + Into<String>,
    V: AsRef<str> + Into<String>,
    I: Iterator<Item = (K, V)>,
  {
    const DEFAULT_VALUE: &str = "@TBD";
    Self::from_ref(DEFAULT_VALUE, DEFAULT_VALUE).set_attrs(attrs).and_then(|attr| {
      if attr.dmtype.as_str() == DEFAULT_VALUE {
        Err(VOTableError::Custom(format!(
          "Mandatory attribute 'dmtype' not found in tag '{}' child of COLLECTION child of GLOBALS",
          &Self::TAG
        )))
      } else if let RefOrValueOrBoth::Ref { ref_: e } = &attr.ref_or_val_or_both {
        if e.as_str() == DEFAULT_VALUE {
          Err(VOTableError::Custom(format!(
            "Mandatory attributes 'ref' or 'value' not found in tag '{}' child of COLLECTION child of GLOBALS",
            &Self::TAG
          )))
        } else {
          Ok(attr)
        }
      } else {
        Ok(attr)
      }
    })
  }

  fn set_attrs_by_ref<K, V, I>(&mut self, attrs: I) -> Result<(), VOTableError>
  where
    K: AsRef<str> + Into<String>,
    V: AsRef<str> + Into<String>,
    I: Iterator<Item = (K, V)>,
  {
    let mut ref_ = String::from("");
    let mut value = String::from("");
    for (key, val) in attrs {
      let key = key.as_ref();
      match key {
        "dmtype" => self.set_dmtype_by_ref(val),
        "ref" => ref_ = val.into(),
        "value" => value = val.into(),
        "arrayindex" => {
          self.set_arrayindex_by_ref(val.as_ref().parse().map_err(VOTableError::ParseInt)?)
        }
        "unit" => self.set_unit_by_ref(val),
        _ => return Err(unexpected_attr_err(key, Self::TAG)),
      }
    }
    self.set_ref_or_val_or_both_by_ref(RefOrValueOrBoth::from_possibly_empty_ref_or_val(
      ref_, value,
    )?);
    Ok(())
  }

  fn for_each_attribute<F>(&self, mut f: F)
  where
    F: FnMut(&str, &str),
  {
    f("dmtype", self.dmtype.as_str());
    self.ref_or_val_or_both.for_each_attribute(&mut f);
    if let Some(arrayindex) = &self.arrayindex {
      f("arrayindex", arrayindex.to_string().as_str());
    }
    if let Some(unit) = &self.unit {
      f("unit", unit.as_str());
    }
  }
}

#[cfg(test)]
mod tests {
  use crate::{
    mivot::test::{get_xml, test_error},
    tests::test_read,
  };

  use super::AttributeChildOfInstance;

  #[test]
  fn test_attribute_a_read() {
    // OK MODELS
    let xml = get_xml("./resources/mivot/7/test_7_ok_7.1.xml");
    println!("testing 7.1");
    test_read::<AttributeChildOfInstance>(&xml);
    let xml = get_xml("./resources/mivot/7/test_7_ok_7.2.xml");
    println!("testing 7.2");
    test_read::<AttributeChildOfInstance>(&xml);
    let xml = get_xml("./resources/mivot/7/test_7_ok_7.3.xml");
    println!("testing 7.3");
    test_read::<AttributeChildOfInstance>(&xml);
    let xml = get_xml("./resources/mivot/7/test_7_ok_7.5.xml");
    println!("testing 7.5");
    test_read::<AttributeChildOfInstance>(&xml);
    let xml = get_xml("./resources/mivot/7/test_7_ok_7.6.xml");
    println!("testing 7.6");
    test_read::<AttributeChildOfInstance>(&xml);
    let xml = get_xml("./resources/mivot/7/test_7_ok_7.7.xml");
    println!("testing 7.7");
    test_read::<AttributeChildOfInstance>(&xml);

    // Ref and value are null, should not be ok...
    //  let xml = get_xml("./resources/mivot/7/test_7_ok_7.14.xml");
    //  println!("testing 7.14");
    //  test_read::<AttributeChildOfInstance>(&xml);

    // KO MODELS
    let xml = get_xml("./resources/mivot/7/test_7_ko_7.4.xml");
    println!("testing 7.4"); // valid dmrole + dmtype; must have one or both of (value, ref)
    test_error::<AttributeChildOfInstance>(&xml, false);
    /*
    // Laurent told me a value can be an array so it is ok to have an arrayindex with a value...
    let xml = get_xml("./resources/mivot/7/test_7_ko_7.8.xml");
    println!("testing 7.8"); // valid dmrole + dmtype + value + arrayindex; arrayindex only assoc. with ref.
    test_error::<AttributeChildOfInstance>(&xml, false);
    let xml = get_xml("./resources/mivot/7/test_7_ko_7.9.xml");
    println!("testing 7.9"); // no dmrole + dmtype + value; must have non-empty dmrole
    test_error::<AttributeChildOfInstance>(&xml, false);
    let xml = get_xml("./resources/mivot/7/test_7_ko_7.10.xml");
    println!("testing 7.10"); // valid dmrole + no dmtype + value; must have non-empty dmtype.
    test_error::<AttributeChildOfInstance>(&xml, false);
    let xml = get_xml("./resources/mivot/7/test_7_ko_7.11.xml");
    println!("testing 7.11"); // empty dmrole + dmtype + ref; must have non-empty dmrole in this context
    test_error::<AttributeChildOfInstance>(&xml, false);
    let xml = get_xml("./resources/mivot/7/test_7_ko_7.12.xml");
    println!("testing 7.12"); // dmtype must not be empty
    test_error::<AttributeChildOfInstance>(&xml, false);
    let xml = get_xml("./resources/mivot/7/test_7_ko_7.13.xml");
    println!("testing 7.13"); // ref must not be empty
    test_error::<AttributeChildOfInstance>(&xml, false);
    let xml = get_xml("./resources/mivot/7/test_7_ko_7.15.xml");
    println!("testing 7.15"); // valid dmrole + dmtype + ref + (arrayindex < 0); invalid arrayindex value (< 0)
    test_error::<AttributeChildOfInstance>(&xml, false);
    */
  }
}