daml_lf/element/
daml_typevar.rs

1use crate::element::visitor::DamlElementVisitor;
2use crate::element::DamlVisitableElement;
3use bounded_static::ToStatic;
4use serde::Serialize;
5use std::borrow::Cow;
6
7/// A Daml type variable.
8#[derive(Debug, Serialize, Clone, ToStatic)]
9pub struct DamlTypeVarWithKind<'a> {
10    var: Cow<'a, str>,
11    kind: DamlKind,
12}
13
14impl<'a> DamlTypeVarWithKind<'a> {
15    pub const fn new(var: Cow<'a, str>, kind: DamlKind) -> Self {
16        Self {
17            var,
18            kind,
19        }
20    }
21
22    pub fn var(&self) -> &str {
23        &self.var
24    }
25
26    pub const fn kind(&self) -> &DamlKind {
27        &self.kind
28    }
29}
30
31impl<'a> DamlVisitableElement<'a> for DamlTypeVarWithKind<'a> {
32    fn accept(&'a self, visitor: &'a mut impl DamlElementVisitor) {
33        visitor.pre_visit_type_var(self);
34        self.kind.accept(visitor);
35        visitor.post_visit_type_var(self);
36    }
37}
38
39/// A Daml type kind.
40#[derive(Debug, Serialize, Clone, ToStatic)]
41pub enum DamlKind {
42    Star,
43    Arrow(Box<DamlArrow>),
44    Nat,
45}
46impl DamlVisitableElement<'_> for DamlKind {
47    fn accept<'a>(&'a self, visitor: &'a mut impl DamlElementVisitor) {
48        visitor.pre_visit_kind(self);
49        if let DamlKind::Arrow(kind) = self {
50            kind.accept(visitor);
51        }
52        visitor.post_visit_kind(self);
53    }
54}
55
56/// A Daml function.
57#[derive(Debug, Serialize, Clone, ToStatic)]
58pub struct DamlArrow {
59    params: Vec<DamlKind>,
60    result: DamlKind,
61}
62
63impl DamlArrow {
64    pub fn new(params: Vec<DamlKind>, result: DamlKind) -> Self {
65        Self {
66            params,
67            result,
68        }
69    }
70
71    pub fn params(&self) -> &[DamlKind] {
72        &self.params
73    }
74
75    pub const fn result(&self) -> &DamlKind {
76        &self.result
77    }
78}
79
80impl DamlVisitableElement<'_> for DamlArrow {
81    fn accept<'a>(&'a self, visitor: &'a mut impl DamlElementVisitor) {
82        visitor.pre_visit_arrow(self);
83        self.params.iter().for_each(|param| param.accept(visitor));
84        self.result.accept(visitor);
85        visitor.post_visit_arrow(self);
86    }
87}