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