Skip to main content

surrealdb_catalog/
module.rs

1use revision::revisioned;
2
3use crate::catalog::StoredModuleDefinition;
4pub use crate::expr::module::ModuleName;
5use crate::expr::statements::info::InfoStructure;
6use crate::stored::Stored;
7use crate::val::Value;
8
9#[revisioned(revision = 1)]
10#[derive(Clone, Debug, Eq, PartialEq, Hash, Stored)]
11#[stored(since = "4.0")]
12pub enum ModuleExecutable {
13	#[stored(n = 1)]
14	Surrealism(SurrealismExecutable),
15	#[stored(n = 2)]
16	Silo(SiloExecutable),
17}
18
19impl InfoStructure for ModuleExecutable {
20	fn structure(self) -> Value {
21		match self {
22			ModuleExecutable::Surrealism(surrealism) => surrealism.structure(),
23			ModuleExecutable::Silo(silo) => silo.structure(),
24		}
25	}
26}
27
28#[revisioned(revision = 1)]
29#[derive(Clone, Debug, Eq, PartialEq, Hash, Stored)]
30#[stored(since = "4.0")]
31pub struct SurrealismExecutable {
32	#[stored(n = 2)]
33	pub bucket: String,
34	#[stored(n = 4)]
35	pub key: String,
36}
37
38impl InfoStructure for SurrealismExecutable {
39	fn structure(self) -> Value {
40		Value::from(map! {
41			"type" => Value::from("surrealism"),
42			"bucket" => self.bucket.into(),
43			"key" => self.key.into(),
44		})
45	}
46}
47
48#[revisioned(revision = 1)]
49#[derive(Clone, Debug, Eq, PartialEq, Hash, Stored)]
50#[stored(since = "4.0")]
51pub struct SiloExecutable {
52	#[stored(n = 2)]
53	pub organisation: String,
54	#[stored(n = 4)]
55	pub package: String,
56	#[stored(n = 6)]
57	pub major: u32,
58	#[stored(n = 8)]
59	pub minor: u32,
60	#[stored(n = 10)]
61	pub patch: u32,
62}
63
64impl InfoStructure for SiloExecutable {
65	fn structure(self) -> Value {
66		Value::from(map! {
67			"type" => Value::from("silo"),
68			"organisation" => self.organisation.into(),
69			"package" => self.package.into(),
70			"major" => self.major.into(),
71			"minor" => self.minor.into(),
72			"patch" => self.patch.into(),
73		})
74	}
75}
76
77impl TryFrom<&StoredModuleDefinition> for ModuleName {
78	type Error = anyhow::Error;
79	fn try_from(value: &StoredModuleDefinition) -> Result<Self, Self::Error> {
80		if let Some(name) = &value.name {
81			Ok(ModuleName::Module(name.clone()))
82		} else if let ModuleExecutable::Silo(silo) = &value.executable {
83			Ok(ModuleName::Silo(
84				silo.organisation.clone(),
85				silo.package.clone(),
86				silo.major,
87				silo.minor,
88				silo.patch,
89			))
90		} else {
91			fail!("A module without a name cannot be stored")
92		}
93	}
94}
95
96use common::fail;
97
98use crate::expr::module::{
99	ModuleExecutable as ExprModuleExecutable, SiloExecutable as ExprSiloExecutable,
100	SurrealismExecutable as ExprSurrealismExecutable,
101};
102
103impl From<ModuleExecutable> for ExprModuleExecutable {
104	fn from(executable: ModuleExecutable) -> Self {
105		match executable {
106			ModuleExecutable::Surrealism(surrealism) => {
107				ExprModuleExecutable::Surrealism(surrealism.into())
108			}
109			ModuleExecutable::Silo(silo) => ExprModuleExecutable::Silo(silo.into()),
110		}
111	}
112}
113
114impl From<ExprModuleExecutable> for ModuleExecutable {
115	fn from(executable: ExprModuleExecutable) -> Self {
116		match executable {
117			ExprModuleExecutable::Surrealism(surrealism) => {
118				ModuleExecutable::Surrealism(surrealism.into())
119			}
120			ExprModuleExecutable::Silo(silo) => ModuleExecutable::Silo(silo.into()),
121		}
122	}
123}
124
125impl From<SurrealismExecutable> for ExprSurrealismExecutable {
126	fn from(executable: SurrealismExecutable) -> Self {
127		Self(crate::val::File::new(executable.bucket, executable.key))
128	}
129}
130
131impl From<ExprSurrealismExecutable> for SurrealismExecutable {
132	fn from(executable: ExprSurrealismExecutable) -> Self {
133		Self {
134			bucket: executable.0.bucket,
135			key: executable.0.key,
136		}
137	}
138}
139
140impl From<SiloExecutable> for ExprSiloExecutable {
141	fn from(executable: SiloExecutable) -> Self {
142		Self {
143			organisation: executable.organisation,
144			package: executable.package,
145			major: executable.major,
146			minor: executable.minor,
147			patch: executable.patch,
148		}
149	}
150}
151
152impl From<ExprSiloExecutable> for SiloExecutable {
153	fn from(executable: ExprSiloExecutable) -> Self {
154		Self {
155			organisation: executable.organisation,
156			package: executable.package,
157			major: executable.major,
158			minor: executable.minor,
159			patch: executable.patch,
160		}
161	}
162}