Skip to main content

model_core/
value.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4
5/// Open-ended facts a frontend or an analysis attaches to an entity
6/// (annotations, modifiers, visibility, HTTP endpoints...). Keys are
7/// namespaced by convention: `java.annotations`, `http.endpoints`.
8pub type Props = BTreeMap<String, Value>;
9
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(untagged)]
12pub enum Value {
13    Bool(bool),
14    Int(i64),
15    Str(String),
16    List(Vec<Value>),
17    Map(BTreeMap<String, Value>),
18}
19
20impl From<bool> for Value {
21    fn from(v: bool) -> Self {
22        Value::Bool(v)
23    }
24}
25
26impl From<i64> for Value {
27    fn from(v: i64) -> Self {
28        Value::Int(v)
29    }
30}
31
32impl From<&str> for Value {
33    fn from(v: &str) -> Self {
34        Value::Str(v.to_string())
35    }
36}
37
38impl From<String> for Value {
39    fn from(v: String) -> Self {
40        Value::Str(v)
41    }
42}
43
44impl<T: Into<Value>> From<Vec<T>> for Value {
45    fn from(v: Vec<T>) -> Self {
46        Value::List(v.into_iter().map(Into::into).collect())
47    }
48}