votable/
datatype.rs

1//! Enum of the possible VOTable datatypes.
2
3use std::{fmt, str::FromStr};
4
5/// See [IVOA spec](https://www.ivoa.net/documents/VOTable/20191021/REC-VOTable-1.4-20191021.html#primitives)
6#[derive(PartialEq, Eq, Copy, Clone, Debug, serde::Serialize, serde::Deserialize)]
7pub enum Datatype {
8  #[serde(rename = "boolean")]
9  Logical,
10  #[serde(rename = "bit")]
11  Bit,
12  #[serde(rename = "unsignedByte")]
13  Byte,
14  #[serde(rename = "short")]
15  ShortInt,
16  #[serde(rename = "int")]
17  Int,
18  #[serde(rename = "long")]
19  LongInt,
20  #[serde(rename = "char")]
21  CharASCII,
22  #[serde(rename = "unicodeChar")]
23  CharUnicode,
24  #[serde(rename = "float")]
25  Float,
26  #[serde(rename = "double")]
27  Double,
28  #[serde(rename = "floatComplex")]
29  ComplexFloat,
30  #[serde(rename = "doubleComplex")]
31  ComplexDouble,
32}
33
34impl FromStr for Datatype {
35  type Err = String;
36
37  fn from_str(s: &str) -> Result<Self, Self::Err> {
38    match s {
39      "boolean" => Ok(Datatype::Logical),
40      "bit" => Ok(Datatype::Bit),
41      "unsignedByte" => Ok(Datatype::Byte),
42      "short" => Ok(Datatype::ShortInt),
43      "int" => Ok(Datatype::Int),
44      "long" => Ok(Datatype::LongInt),
45      "char" => Ok(Datatype::CharASCII),
46      "unicodeChar" => Ok(Datatype::CharUnicode),
47      "float" => Ok(Datatype::Float),
48      "double" => Ok(Datatype::Double),
49      "floatComplex" => Ok(Datatype::ComplexFloat),
50      "doubleComplex" => Ok(Datatype::ComplexDouble),
51      _ => Err(format!("Unknown 'datatype' variant. Actual: '{}'.", s)),
52    }
53  }
54}
55
56impl fmt::Display for Datatype {
57  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58    write!(
59      f,
60      "{}",
61      match self {
62        Datatype::Logical => "boolean",
63        Datatype::Bit => "bit",
64        Datatype::Byte => "unsignedByte",
65        Datatype::ShortInt => "short",
66        Datatype::Int => "int",
67        Datatype::LongInt => "long",
68        Datatype::CharASCII => "char",
69        Datatype::CharUnicode => "unicodeChar",
70        Datatype::Float => "float",
71        Datatype::Double => "double",
72        Datatype::ComplexFloat => "floatComplex",
73        Datatype::ComplexDouble => "doubleComplex",
74      }
75    )
76  }
77}