logo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::element::daml_module::DamlModule;
use crate::element::visitor::DamlElementVisitor;
use crate::element::DamlVisitableElement;
use crate::LanguageVersion;
use bounded_static::ToStatic;
use serde::Serialize;
use std::borrow::Cow;

#[derive(Debug, Serialize, Clone, ToStatic)]
pub struct DamlPackage<'a> {
    name: Cow<'a, str>,
    package_id: Cow<'a, str>,
    version: Option<Cow<'a, str>>,
    language_version: LanguageVersion,
    root_module: DamlModule<'a>,
}

impl<'a> DamlPackage<'a> {
    pub const fn new(
        name: Cow<'a, str>,
        package_id: Cow<'a, str>,
        version: Option<Cow<'a, str>>,
        language_version: LanguageVersion,
        root_module: DamlModule<'a>,
    ) -> Self {
        Self {
            name,
            package_id,
            version,
            language_version,
            root_module,
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn package_id(&self) -> &str {
        &self.package_id
    }

    /// Package version.
    pub fn version(&self) -> Option<&str> {
        self.version.as_ref().map(AsRef::as_ref)
    }

    /// The Daml LF language version.
    pub const fn language_version(&self) -> LanguageVersion {
        self.language_version
    }

    pub const fn root_module(&self) -> &DamlModule<'a> {
        &self.root_module
    }
}

impl<'a> DamlVisitableElement<'a> for DamlPackage<'a> {
    fn accept(&'a self, visitor: &'a mut impl DamlElementVisitor) {
        visitor.pre_visit_package(self);
        self.root_module.accept(visitor);
        visitor.post_visit_package(self);
    }
}