daml_lf/element/
daml_package.rs

1use crate::element::daml_module::DamlModule;
2use crate::element::visitor::DamlElementVisitor;
3use crate::element::DamlVisitableElement;
4use crate::LanguageVersion;
5use bounded_static::ToStatic;
6use serde::Serialize;
7use std::borrow::Cow;
8
9/// A Daml package.
10#[derive(Debug, Serialize, Clone, ToStatic)]
11pub struct DamlPackage<'a> {
12    name: Cow<'a, str>,
13    package_id: Cow<'a, str>,
14    version: Option<Cow<'a, str>>,
15    language_version: LanguageVersion,
16    root_module: DamlModule<'a>,
17}
18
19impl<'a> DamlPackage<'a> {
20    pub const fn new(
21        name: Cow<'a, str>,
22        package_id: Cow<'a, str>,
23        version: Option<Cow<'a, str>>,
24        language_version: LanguageVersion,
25        root_module: DamlModule<'a>,
26    ) -> Self {
27        Self {
28            name,
29            package_id,
30            version,
31            language_version,
32            root_module,
33        }
34    }
35
36    pub fn name(&self) -> &str {
37        &self.name
38    }
39
40    pub fn package_id(&self) -> &str {
41        &self.package_id
42    }
43
44    /// Package version.
45    pub fn version(&self) -> Option<&str> {
46        self.version.as_ref().map(AsRef::as_ref)
47    }
48
49    /// The Daml LF language version.
50    pub const fn language_version(&self) -> LanguageVersion {
51        self.language_version
52    }
53
54    pub const fn root_module(&self) -> &DamlModule<'a> {
55        &self.root_module
56    }
57}
58
59impl<'a> DamlVisitableElement<'a> for DamlPackage<'a> {
60    fn accept(&'a self, visitor: &'a mut impl DamlElementVisitor) {
61        visitor.pre_visit_package(self);
62        self.root_module.accept(visitor);
63        visitor.post_visit_package(self);
64    }
65}