1pub mod conformance;
18pub mod frame;
19pub mod host;
20pub mod markdown;
21pub mod msg;
22pub mod options;
23pub mod plugin;
24pub mod sessions;
25pub mod util;
26
27pub const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
29
30pub const MIN_VERSION: u32 = 0;
32pub const MAX_VERSION: u32 = 0;
33
34pub const CAP_SESSIONS: &str = "sessions";
36pub const CAP_READ_ONLY: &str = "read_only";
38pub const CAP_CHECK: &str = "check";
40pub const CAP_MULTI_FILE: &str = "multi_file";
42pub const CAP_MESSAGE: &str = "message";
44pub const CAP_MESSAGE_ONLY: &str = "message_only";
46pub const CAP_LOAD: &str = "load";
48pub const CAP_VALIDATE: &str = "validate";
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)]
52#[serde(rename_all = "lowercase")]
53pub enum Kind {
54 Source,
55 Format,
56 Destination,
57}
58
59impl Kind {
60 pub fn as_str(self) -> &'static str {
61 match self {
62 Kind::Source => "source",
63 Kind::Format => "format",
64 Kind::Destination => "destination",
65 }
66 }
67
68 pub fn parse(s: &str) -> Option<Kind> {
70 match s {
71 "source" | "sources" => Some(Kind::Source),
72 "format" | "formats" => Some(Kind::Format),
73 "destination" | "destinations" => Some(Kind::Destination),
74 _ => None,
75 }
76 }
77}
78
79impl std::fmt::Display for Kind {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 f.write_str(self.as_str())
82 }
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
88pub struct PluginId {
89 pub kind: Kind,
90 pub name: String,
91}
92
93impl PluginId {
94 pub fn new(kind: Kind, name: impl Into<String>) -> PluginId {
95 PluginId {
96 kind,
97 name: name.into(),
98 }
99 }
100}
101
102impl std::fmt::Display for PluginId {
103 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104 write!(f, "{}/{}", self.kind, self.name)
105 }
106}
107
108impl std::str::FromStr for PluginId {
109 type Err = String;
110
111 fn from_str(s: &str) -> Result<PluginId, String> {
112 let bad = || format!("`{s}` isn't `<kind>/<name>` (e.g. `destination/s3`)");
113 let (k, n) = s.split_once('/').ok_or_else(bad)?;
114 let kind = Kind::parse(k).ok_or_else(bad)?;
115 valid_name(n).then(|| PluginId::new(kind, n)).ok_or_else(bad)
116 }
117}
118
119impl serde::Serialize for PluginId {
120 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
121 s.collect_str(self)
122 }
123}
124
125impl<'de> serde::Deserialize<'de> for PluginId {
126 fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
127 String::deserialize(d)?.parse().map_err(serde::de::Error::custom)
128 }
129}
130
131pub fn valid_name(name: &str) -> bool {
133 !name.is_empty()
134 && name
135 .bytes()
136 .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_')
137}
138
139pub fn package_executable_name(package: &str) -> String {
141 format!("dre-plugin-{package}{}", std::env::consts::EXE_SUFFIX)
142}
143
144pub fn parse_package_executable_name(file: &str) -> Option<String> {
146 let stem = file.strip_suffix(std::env::consts::EXE_SUFFIX).unwrap_or(file);
147 if !std::env::consts::EXE_SUFFIX.is_empty() && stem == file {
148 return None;
149 }
150 let name = stem.strip_prefix("dre-plugin-")?;
151 valid_name(name).then(|| name.to_string())
152}
153
154pub fn executable_name(kind: Kind, name: &str) -> String {
156 format!("dre-{}-{name}{}", kind.as_str(), std::env::consts::EXE_SUFFIX)
157}
158
159pub fn parse_executable_name(file: &str) -> Option<(Kind, String)> {
161 let stem = file.strip_suffix(std::env::consts::EXE_SUFFIX).unwrap_or(file);
162 if !std::env::consts::EXE_SUFFIX.is_empty() && stem == file {
163 return None;
164 }
165 let rest = stem.strip_prefix("dre-")?;
166 let (kind, name) = rest.split_once('-')?;
167 let kind = Kind::parse(kind)?;
168 valid_name(name).then(|| (kind, name.to_string()))
169}
170
171#[cfg(test)]
172mod tests {
173 use super::*;
174
175 #[test]
176 fn parses_plugin_executable_names() {
177 let exe = |s: &str| format!("{s}{}", std::env::consts::EXE_SUFFIX);
178 assert_eq!(
179 parse_executable_name(&exe("dre-source-duckdb")),
180 Some((Kind::Source, "duckdb".into()))
181 );
182 assert_eq!(
183 parse_executable_name(&exe("dre-destination-azure_blob")),
184 Some((Kind::Destination, "azure_blob".into()))
185 );
186 assert_eq!(parse_executable_name(&exe("dre-format-csv.d")), None);
187 assert_eq!(parse_executable_name(&exe("dre-widget-x")), None);
188 assert_eq!(parse_executable_name(&exe("dre")), None);
189 assert_eq!(parse_executable_name(&exe("dre-plugin-csv")), None);
190 }
191
192 #[test]
193 fn parses_package_executable_names() {
194 let exe = |s: &str| format!("{s}{}", std::env::consts::EXE_SUFFIX);
195 assert_eq!(
196 parse_package_executable_name(&exe("dre-plugin-object_store")).as_deref(),
197 Some("object_store")
198 );
199 assert_eq!(parse_package_executable_name(&exe("dre-source-duckdb")), None);
200 assert_eq!(parse_package_executable_name(&exe("dre-plugin-")), None);
201 }
202
203 #[test]
204 fn plugin_ids_read_and_write_as_kind_slash_name() {
205 let id: PluginId = "destination/s3".parse().unwrap();
206 assert_eq!(id, PluginId::new(Kind::Destination, "s3"));
207 assert_eq!(id.to_string(), "destination/s3");
208 assert_eq!(serde_json::to_string(&id).unwrap(), "\"destination/s3\"");
209 assert!("s3".parse::<PluginId>().is_err());
210 assert!("widget/s3".parse::<PluginId>().is_err());
211 }
212}