1use std::fmt::{self, Display};
2
3use anyhow::Result;
4use common::fail;
5use common::fmt::EscapeKwFreeIdent;
6use surrealdb_types::{SqlFormat, ToSql, write_sql};
7
8use crate::expr::Kind;
9use crate::val::File;
10
11#[derive(Clone, Debug, Eq, PartialEq, Hash)]
12pub enum ModuleExecutable {
13 Surrealism(SurrealismExecutable),
14 Silo(SiloExecutable),
15}
16
17impl ToSql for ModuleExecutable {
18 fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
19 match self {
20 ModuleExecutable::Surrealism(surrealism) => surrealism.fmt_sql(f, fmt),
21 ModuleExecutable::Silo(silo) => silo.fmt_sql(f, fmt),
22 }
23 }
24}
25
26#[derive(Clone, Debug, Eq, PartialEq, Hash)]
27pub struct Signature {
28 pub args: Vec<Kind>,
29 pub returns: Option<Kind>,
30 pub writeable: bool,
31}
32
33#[derive(Clone, Debug, Eq, PartialEq, Hash)]
34pub struct SurrealismExecutable(pub File);
35
36impl ToSql for SurrealismExecutable {
37 fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
38 self.0.fmt_sql(f, fmt);
39 }
40}
41
42#[derive(Clone, Debug, Eq, PartialEq, Hash)]
43pub struct SiloExecutable {
44 pub organisation: String,
45 pub package: String,
46 pub major: u32,
47 pub minor: u32,
48 pub patch: u32,
49}
50
51impl ToSql for SiloExecutable {
52 fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
53 write_sql!(
54 f,
55 fmt,
56 "silo::{}::{}<{}.{}.{}>",
57 self.organisation,
58 self.package,
59 self.major,
60 self.minor,
61 self.patch
62 )
63 }
64}
65
66#[derive(Clone, Debug, Eq, PartialEq, Hash)]
74pub enum ModuleName {
75 Module(String),
76 Silo(String, String, u32, u32, u32),
77}
78
79impl ModuleName {
80 pub fn get_storage_name(&self) -> String {
81 match self {
82 ModuleName::Module(name) => format!("mod::{}", name),
83 ModuleName::Silo(org, pkg, major, minor, patch) => {
84 format!("silo::{org}::{pkg}::<{major}.{minor}.{patch}>")
85 }
86 }
87 }
88}
89
90impl<'a> TryFrom<&'a crate::expr::statements::DefineModuleStatement> for ModuleName {
91 type Error = anyhow::Error;
92 fn try_from(
93 value: &'a crate::expr::statements::DefineModuleStatement,
94 ) -> Result<Self, Self::Error> {
95 if let Some(name) = &value.name {
96 Ok(ModuleName::Module(name.clone()))
97 } else if let crate::expr::ModuleExecutable::Silo(silo) = &value.executable {
98 Ok(ModuleName::Silo(
99 silo.organisation.clone(),
100 silo.package.clone(),
101 silo.major,
102 silo.minor,
103 silo.patch,
104 ))
105 } else {
106 fail!("A module without a name cannot be stored")
109 }
110 }
111}
112
113impl Display for ModuleName {
114 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115 match self {
116 ModuleName::Module(name) => write!(f, "ext::{}", name),
117 ModuleName::Silo(org, pkg, major, minor, patch) => {
118 write!(f, "silo::{org}::{pkg}<{major}.{minor}.{patch}>")
119 }
120 }
121 }
122}
123
124impl ToSql for ModuleName {
125 fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
126 match self {
127 ModuleName::Module(name) => write_sql!(f, fmt, "ext::{}", EscapeKwFreeIdent(name)),
128 ModuleName::Silo(org, pkg, major, minor, patch) => {
129 write_sql!(
130 f,
131 fmt,
132 "silo::{}::{}<{major}.{minor}.{patch}>",
133 EscapeKwFreeIdent(org),
134 EscapeKwFreeIdent(pkg)
135 );
136 }
137 }
138 }
139}