cedar_policy_core/est/
annotation.rsuse std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::ast::{self, Annotation, AnyId};
#[cfg(feature = "wasm")]
extern crate tsify;
#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Annotations(
#[serde(default)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
#[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")]
#[cfg_attr(feature = "wasm", tsify(type = "Record<string, Annotation>"))]
pub BTreeMap<AnyId, Option<Annotation>>,
);
impl Annotations {
pub fn new() -> Self {
Self(BTreeMap::new())
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl From<Annotations> for ast::Annotations {
fn from(value: Annotations) -> Self {
ast::Annotations::from_iter(
value
.0
.into_iter()
.map(|(key, value)| (key, value.unwrap_or_default())),
)
}
}
impl From<ast::Annotations> for Annotations {
fn from(value: ast::Annotations) -> Self {
Self(
value
.into_iter()
.map(|(key, value)| (key, Some(value)))
.collect(),
)
}
}
impl std::fmt::Display for Annotations {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (k, v) in &self.0 {
if let Some(anno) = v {
writeln!(f, "@{k}({anno})")?
} else {
writeln!(f, "@{k}")?
}
}
Ok(())
}
}
impl Default for Annotations {
fn default() -> Self {
Self::new()
}
}