Skip to main content

redevplugin_contracts/
lib.rs

1#![forbid(unsafe_code)]
2
3use std::error::Error;
4use std::fmt;
5
6mod contracts_gen;
7mod release_signing;
8
9pub use release_signing::*;
10
11#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
12pub struct ContractId(&'static str);
13
14impl ContractId {
15    const fn from_static(value: &'static str) -> Self {
16        Self(value)
17    }
18
19    pub const fn as_str(self) -> &'static str {
20        self.0
21    }
22
23    pub fn parse(value: &str) -> Result<Self, ContractError> {
24        contracts_gen::parse_contract_id(value).ok_or(ContractError::UnknownId)
25    }
26}
27
28impl TryFrom<&str> for ContractId {
29    type Error = ContractError;
30
31    fn try_from(value: &str) -> Result<Self, Self::Error> {
32        Self::parse(value)
33    }
34}
35
36impl fmt::Display for ContractId {
37    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
38        formatter.write_str(self.0)
39    }
40}
41
42#[derive(Clone, Debug, Eq, PartialEq)]
43pub enum ContractError {
44    UnknownId,
45}
46
47impl fmt::Display for ContractError {
48    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            Self::UnknownId => formatter.write_str("contract id is unknown"),
51        }
52    }
53}
54
55impl Error for ContractError {}
56
57#[derive(Clone, Copy, Debug, Eq, PartialEq)]
58pub struct Contract {
59    id: ContractId,
60    version: &'static str,
61    sha256: &'static str,
62    bytes: &'static [u8],
63}
64
65impl Contract {
66    const fn new(
67        id: ContractId,
68        version: &'static str,
69        sha256: &'static str,
70        bytes: &'static [u8],
71    ) -> Self {
72        Self {
73            id,
74            version,
75            sha256,
76            bytes,
77        }
78    }
79
80    pub const fn id(&self) -> ContractId {
81        self.id
82    }
83    pub const fn version(&self) -> &'static str {
84        self.version
85    }
86    pub const fn sha256(&self) -> &'static str {
87        self.sha256
88    }
89    pub const fn bytes(&self) -> &'static [u8] {
90        self.bytes
91    }
92}
93
94#[derive(Clone, Copy, Debug, Eq, PartialEq)]
95pub struct GoModuleCoordinate {
96    pub module: &'static str,
97    pub version: &'static str,
98}
99
100#[derive(Clone, Copy, Debug, Eq, PartialEq)]
101pub struct NpmPackageCoordinate {
102    pub name: &'static str,
103    pub version: &'static str,
104}
105
106#[derive(Clone, Copy, Debug, Eq, PartialEq)]
107pub struct RustCrateCoordinate {
108    pub name: &'static str,
109    pub version: &'static str,
110    pub role: &'static str,
111}
112
113#[derive(Clone, Copy, Debug, Eq, PartialEq)]
114pub struct PackageSet {
115    pub schema_version: &'static str,
116    pub platform_version: &'static str,
117    pub go_module: GoModuleCoordinate,
118    pub npm_packages: &'static [NpmPackageCoordinate],
119    pub rust_crates: &'static [RustCrateCoordinate],
120    pub contract_registry_version: &'static str,
121    pub contract_set_sha256: &'static str,
122}
123
124pub fn package_set() -> &'static PackageSet {
125    &contracts_gen::PACKAGE_SET
126}
127pub fn artifacts() -> &'static [Contract] {
128    &contracts_gen::ARTIFACTS
129}
130pub fn registry_contract() -> &'static Contract {
131    &contracts_gen::ALL[contracts_gen::REGISTRY_CONTRACT_INDEX]
132}
133pub fn all() -> &'static [Contract] {
134    &contracts_gen::ALL
135}
136pub fn get(id: ContractId) -> &'static Contract {
137    contracts_gen::get(id)
138}