opensearch_client/common/
stringified_double.rs1use serde::{Deserialize, Serialize};
11
12
13
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15#[serde(untagged)]
16pub enum StringifiedDouble {
17 StringValue(String),
18 NumberValue(f64),
19}
20
21impl std::fmt::Display for StringifiedDouble {
22 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23 match self {
24 StringifiedDouble::StringValue(s) => write!(f, "{}", s),
25 StringifiedDouble::NumberValue(n) => write!(f, "{}", n),
26 }
27 }
28}
29
30
31impl StringifiedDouble {
32 pub fn as_str(&self) -> String {
33 match self {
34 StringifiedDouble::StringValue(s) => s.clone(),
35 StringifiedDouble::NumberValue(n) => n.to_string(),
36 }
37 }
38
39 pub fn as_num(&self) -> Option<f64> {
40 match self {
41 StringifiedDouble::NumberValue(n) => Some(*n),
42 _ => None,
43 }
44 }
45}
46
47impl From<f64> for StringifiedDouble {
48 fn from(n: f64) -> Self {
49 StringifiedDouble::NumberValue(n)
50 }
51}
52
53impl From<&str> for StringifiedDouble {
54 fn from(s: &str) -> Self {
55 StringifiedDouble::StringValue(s.to_string())
56 }
57}
58
59impl From<String> for StringifiedDouble {
60 fn from(s: String) -> Self {
61 StringifiedDouble::StringValue(s)
62 }
63}