cedar_policy_core/est/
annotation.rs1use std::collections::BTreeMap;
18
19use serde::{Deserialize, Serialize};
20
21use crate::ast::{self, Annotation, AnyId};
22#[cfg(feature = "wasm")]
23extern crate tsify;
24
25#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, PartialOrd, Ord, Debug)]
27#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
28#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
29pub struct Annotations(
30 #[serde(default)]
31 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
32 #[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")]
33 #[cfg_attr(feature = "wasm", tsify(type = "Record<string, Annotation>"))]
34 pub BTreeMap<AnyId, Option<Annotation>>,
35);
36
37impl Annotations {
38 pub fn new() -> Self {
40 Self(BTreeMap::new())
41 }
42 pub fn is_empty(&self) -> bool {
44 self.0.is_empty()
45 }
46}
47
48impl From<Annotations> for ast::Annotations {
49 fn from(value: Annotations) -> Self {
50 ast::Annotations::from_iter(
51 value
52 .0
53 .into_iter()
54 .map(|(key, value)| (key, value.unwrap_or_default())),
55 )
56 }
57}
58
59impl From<ast::Annotations> for Annotations {
60 fn from(value: ast::Annotations) -> Self {
61 Self(
62 value
63 .into_iter()
64 .map(|(key, value)| (key, Some(value)))
65 .collect(),
66 )
67 }
68}
69
70impl std::fmt::Display for Annotations {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 for (k, v) in &self.0 {
73 if let Some(anno) = v {
74 writeln!(f, "@{k}({anno})")?
75 } else {
76 writeln!(f, "@{k}")?
77 }
78 }
79 Ok(())
80 }
81}
82
83impl Default for Annotations {
84 fn default() -> Self {
85 Self::new()
86 }
87}