Skip to main content

appcore_contracts/policy/
dependency.rs

1// =============================================================================
2//        #######
3//     ###       ###     F: dependency.rs
4//    ##   ## ##   ##    P: AppCore-Runtime
5//         ## ##
6//                       C: 2026/07/22 15:41:18 by dnettoRaw
7//    ##   ## ##   ##    U: 2026/07/22 15:41:18 by dnettoRaw
8//      ###########      S: 1.0.1-rc.8
9// =============================================================================
10
11use super::*;
12
13/// Dependency on another application contract.
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct ApplicationDependency {
16    application_id: ApplicationId,
17    version_requirement: String,
18    optional: bool,
19}
20
21impl ApplicationDependency {
22    /// Creates an application dependency.
23    pub fn new(
24        application_id: ApplicationId,
25        version_requirement: impl Into<String>,
26        optional: bool,
27    ) -> ContractResult<Self> {
28        let dependency = Self {
29            application_id,
30            version_requirement: version_requirement.into(),
31            optional,
32        };
33        dependency.validate()?;
34        Ok(dependency)
35    }
36
37    /// Returns the depended-on application.
38    pub fn application_id(&self) -> &ApplicationId {
39        &self.application_id
40    }
41
42    /// Returns the version requirement expression.
43    pub fn version_requirement(&self) -> &str {
44        &self.version_requirement
45    }
46
47    /// Reports whether the dependency is optional.
48    pub fn is_optional(&self) -> bool {
49        self.optional
50    }
51
52    pub(crate) fn validate(&self) -> ContractResult<()> {
53        validate_text(
54            "dependency.version_requirement",
55            &self.version_requirement,
56            128,
57        )
58    }
59}