code_split_plugin_api/
attrs.rs1use serde::{Deserialize, Serialize};
11use std::collections::BTreeMap;
12
13pub type Attributes = BTreeMap<String, AttrValue>;
16
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22#[serde(untagged)]
23pub enum AttrValue {
24 Bool(bool),
27 Int(i64),
28 Float(f64),
29 Str(String),
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
37#[serde(rename_all = "snake_case")]
38pub enum ValueType {
39 Bool,
40 Int,
41 Float,
42 Str,
43}
44
45impl AttrValue {
46 pub fn value_type(&self) -> ValueType {
49 match self {
50 AttrValue::Bool(_) => ValueType::Bool,
51 AttrValue::Int(_) => ValueType::Int,
52 AttrValue::Float(_) => ValueType::Float,
53 AttrValue::Str(_) => ValueType::Str,
54 }
55 }
56}
57
58impl From<bool> for AttrValue {
59 fn from(v: bool) -> Self {
60 AttrValue::Bool(v)
61 }
62}
63impl From<i64> for AttrValue {
64 fn from(v: i64) -> Self {
65 AttrValue::Int(v)
66 }
67}
68impl From<u32> for AttrValue {
69 fn from(v: u32) -> Self {
70 AttrValue::Int(v as i64)
71 }
72}
73impl From<f64> for AttrValue {
74 fn from(v: f64) -> Self {
75 AttrValue::Float(v)
76 }
77}
78impl From<String> for AttrValue {
79 fn from(v: String) -> Self {
80 AttrValue::Str(v)
81 }
82}
83impl From<&str> for AttrValue {
84 fn from(v: &str) -> Self {
85 AttrValue::Str(v.to_string())
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn value_type_maps_each_variant() {
95 assert_eq!(AttrValue::Bool(true).value_type(), ValueType::Bool);
96 assert_eq!(AttrValue::Int(1).value_type(), ValueType::Int);
97 assert_eq!(AttrValue::Float(1.5).value_type(), ValueType::Float);
98 assert_eq!(AttrValue::Str("x".into()).value_type(), ValueType::Str);
99 }
100
101 #[test]
102 fn from_impls_cover_each_scalar() {
103 assert_eq!(AttrValue::from(true), AttrValue::Bool(true));
104 assert_eq!(AttrValue::from(7_i64), AttrValue::Int(7));
105 assert_eq!(AttrValue::from(7_u32), AttrValue::Int(7));
106 assert_eq!(AttrValue::from(2.5_f64), AttrValue::Float(2.5));
107 assert_eq!(AttrValue::from("s".to_string()), AttrValue::Str("s".into()));
108 assert_eq!(AttrValue::from("s"), AttrValue::Str("s".into()));
109 }
110}