Skip to main content

coil_wasm/host_api/
data_contract.rs

1use crate::validation::validate_token;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum ModuleDataRepositoryKind {
5    Owned,
6    Shared,
7}
8
9impl std::fmt::Display for ModuleDataRepositoryKind {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        match self {
12            Self::Owned => f.write_str("owned"),
13            Self::Shared => f.write_str("shared"),
14        }
15    }
16}
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct ModuleDataRepository {
20    pub id: String,
21    pub kind: ModuleDataRepositoryKind,
22}
23
24impl ModuleDataRepository {
25    pub fn owned(id: impl Into<String>) -> Result<Self, crate::error::WasmModelError> {
26        Ok(Self {
27            id: validate_token("data_repository_id", id.into())?,
28            kind: ModuleDataRepositoryKind::Owned,
29        })
30    }
31
32    pub fn shared(id: impl Into<String>) -> Result<Self, crate::error::WasmModelError> {
33        Ok(Self {
34            id: validate_token("data_repository_id", id.into())?,
35            kind: ModuleDataRepositoryKind::Shared,
36        })
37    }
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct ModuleDataContract {
42    pub owner_extension_id: String,
43    pub owner_handler_id: String,
44    pub repository: ModuleDataRepository,
45    pub resource: String,
46}
47
48impl ModuleDataContract {
49    pub fn new(
50        owner_extension_id: impl Into<String>,
51        owner_handler_id: impl Into<String>,
52        resource: impl Into<String>,
53    ) -> Result<Self, crate::error::WasmModelError> {
54        let resource = validate_token("data_contract_resource", resource.into())?;
55        let repository = ModuleDataRepository::owned(resource.clone())?;
56        Ok(Self {
57            owner_extension_id: validate_token(
58                "data_contract_owner_extension_id",
59                owner_extension_id.into(),
60            )?,
61            owner_handler_id: validate_token(
62                "data_contract_owner_handler_id",
63                owner_handler_id.into(),
64            )?,
65            repository,
66            resource,
67        })
68    }
69
70    pub fn with_repository(
71        owner_extension_id: impl Into<String>,
72        owner_handler_id: impl Into<String>,
73        repository: ModuleDataRepository,
74    ) -> Result<Self, crate::error::WasmModelError> {
75        let resource = repository.id.clone();
76        Ok(Self {
77            owner_extension_id: validate_token(
78                "data_contract_owner_extension_id",
79                owner_extension_id.into(),
80            )?,
81            owner_handler_id: validate_token(
82                "data_contract_owner_handler_id",
83                owner_handler_id.into(),
84            )?,
85            repository,
86            resource,
87        })
88    }
89
90    pub fn owned_repository(
91        owner_extension_id: impl Into<String>,
92        owner_handler_id: impl Into<String>,
93        repository: impl Into<String>,
94    ) -> Result<Self, crate::error::WasmModelError> {
95        Self::with_repository(
96            owner_extension_id,
97            owner_handler_id,
98            ModuleDataRepository::owned(repository)?,
99        )
100    }
101
102    pub fn repository_id(&self) -> &str {
103        &self.repository.id
104    }
105
106    pub fn summary(&self, access: &str, sequence: u64) -> String {
107        format!(
108            "module={} handler={} repository={} repository_kind={} access={} sequence={sequence}",
109            self.owner_extension_id,
110            self.owner_handler_id,
111            self.repository.id,
112            self.repository.kind,
113            access,
114        )
115    }
116}