1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Store-backed I/O: the `store` module's C surface (`src/store/ffi.rs`) plus the
//! schematic-level transparent open/save and format-manager queries
//! (`ffi/store_io.rs`), unified in one module.
//!
//! Omitted from port: `nuc_store_free` — destructor is generated.
//! Omitted from port: `nuc_store_last_error` — error transport is generated.
//! Omitted from port: `nuc_store_string_free`, `nuc_store_bytes_free` — buffer
//! memory management is obsolete (strings cross via `DiplomatWrite`, bytes as
//! base64 per PORTING rule 6).
#[diplomat::bridge]
pub mod ffi {
use super::super::schematic::ffi::Schematic;
use super::super::shared::ffi::NucleationError;
use base64::Engine;
use diplomat_runtime::DiplomatWrite;
use std::fmt::Write;
/// A key/value store opened from a URL (e.g. `mem://`, `file:///path`,
/// `ssh://user@host/path`, `s3://bucket/prefix`, `redis://…`, `postgres://…`).
#[diplomat::opaque]
pub struct Store(pub(crate) Box<dyn crate::store::Store>);
impl Store {
fn utf8(s: &[u8]) -> Result<&str, NucleationError> {
std::str::from_utf8(s).map_err(|_| NucleationError::InvalidArgument)
}
/// Open a store from a URL. Errors with `Store` on an unknown scheme or
/// connection failure.
pub fn open(url: &DiplomatStr) -> Result<Box<Store>, NucleationError> {
let url = Self::utf8(url)?;
crate::store::open(url)
.map(|s| Box::new(Store(s)))
.map_err(|_| NucleationError::Store)
}
/// Fetch `key`, writing the value as base64 (PORTING rule 6). Errors with
/// `NotFound` when the key is absent.
pub fn get_b64(
&self,
key: &DiplomatStr,
out: &mut DiplomatWrite,
) -> Result<(), NucleationError> {
let key = Self::utf8(key)?;
match self.0.get(key).map_err(|_| NucleationError::Store)? {
Some(bytes) => {
let _ = write!(
out,
"{}",
base64::engine::general_purpose::STANDARD.encode(&bytes)
);
Ok(())
}
None => Err(NucleationError::NotFound),
}
}
/// Store `data` at `key`.
pub fn put(&self, key: &DiplomatStr, data: &[u8]) -> Result<(), NucleationError> {
let key = Self::utf8(key)?;
self.0.put(key, data).map_err(|_| NucleationError::Store)
}
/// Whether `key` exists.
pub fn exists(&self, key: &DiplomatStr) -> Result<bool, NucleationError> {
let key = Self::utf8(key)?;
self.0.exists(key).map_err(|_| NucleationError::Store)
}
/// Delete `key` (idempotent).
pub fn delete(&self, key: &DiplomatStr) -> Result<(), NucleationError> {
let key = Self::utf8(key)?;
self.0.delete(key).map_err(|_| NucleationError::Store)
}
/// List keys under `prefix`, written as a JSON array string.
pub fn list(
&self,
prefix: &DiplomatStr,
out: &mut DiplomatWrite,
) -> Result<(), NucleationError> {
let prefix = Self::utf8(prefix)?;
let keys = self.0.list(prefix).map_err(|_| NucleationError::Store)?;
let json = serde_json::to_string(&keys).map_err(|_| NucleationError::Serialize)?;
let _ = write!(out, "{}", json);
Ok(())
}
/// Atomically write `data` at `key` only if it does not already exist.
/// Returns `true` if written, `false` if the key existed.
pub fn put_if_absent(
&self,
key: &DiplomatStr,
data: &[u8],
) -> Result<bool, NucleationError> {
let key = Self::utf8(key)?;
self.0
.put_if_absent(key, data)
.map_err(|_| NucleationError::Store)
}
/// A keyset page of keys under `prefix`. `after` is the exclusive cursor
/// (empty string for the first page); at most `limit` keys are returned.
/// Writes a JSON object string `{"keys":[...],"next":"…"|null}`.
pub fn list_paginated(
&self,
prefix: &DiplomatStr,
after: &DiplomatStr,
limit: u32,
out: &mut DiplomatWrite,
) -> Result<(), NucleationError> {
let prefix = Self::utf8(prefix)?;
let after = Self::utf8(after)?;
let after = if after.is_empty() { None } else { Some(after) };
let (keys, next) = self
.0
.list_paginated(prefix, after, limit as usize)
.map_err(|_| NucleationError::Store)?;
let json = serde_json::to_string(&serde_json::json!({ "keys": keys, "next": next }))
.map_err(|_| NucleationError::Serialize)?;
let _ = write!(out, "{}", json);
Ok(())
}
/// Health check: `Ok` when the store is usable.
pub fn health(&self) -> Result<(), NucleationError> {
self.0.health().map_err(|_| NucleationError::Store)
}
/// Open a schematic stored at `key` in this store. Works for every
/// backend, including `redis://`/`postgres://`/`mem://` that the
/// single-string URI form (`StoreIo::open`) rejects.
pub fn open_schematic(&self, key: &DiplomatStr) -> Result<Box<Schematic>, NucleationError> {
let key = Self::utf8(key)?;
crate::UniversalSchematic::from_store(self.0.as_ref(), key)
.map(|s| Box::new(Schematic(s)))
.map_err(|_| NucleationError::Store)
}
/// Save a schematic at `key` in this store. `version` selects the format
/// version (empty string = format default). Works for every backend,
/// including `redis://`/`postgres://`/`mem://` that the single-string URI
/// form (`StoreIo::save`) rejects.
pub fn save_schematic(
&self,
schematic: &Schematic,
key: &DiplomatStr,
version: &DiplomatStr,
) -> Result<(), NucleationError> {
let key = Self::utf8(key)?;
let version = Self::utf8(version)?;
let version = if version.is_empty() {
None
} else {
Some(version)
};
schematic
.0
.save_to_store(self.0.as_ref(), key, version)
.map_err(|_| NucleationError::Store)
}
}
/// Namespace type for the URI-based transparent I/O and format-manager
/// queries (PORTING rule 12).
#[diplomat::opaque]
pub struct StoreIo;
impl StoreIo {
fn utf8(s: &[u8]) -> Result<&str, NucleationError> {
std::str::from_utf8(s).map_err(|_| NucleationError::InvalidArgument)
}
/// Open a schematic from a URI: a local path, `file://...`, or
/// `s3://bucket/key.schem`. The format is auto-detected from the URI's
/// extension. Single-string URIs for `redis://`, `postgres://`, and
/// `mem://` are rejected by the core resolver; use `Store::open_schematic`
/// with an explicit store for those backends.
pub fn open(uri: &DiplomatStr) -> Result<Box<Schematic>, NucleationError> {
let uri = Self::utf8(uri)?;
crate::UniversalSchematic::open(uri)
.map(|s| Box::new(Schematic(s)))
.map_err(|_| NucleationError::Store)
}
/// Save a schematic to a URI: a local path, `file://...`, or
/// `s3://bucket/key.schem`. The format is auto-detected from the URI's
/// extension; `version` selects the format version (empty string =
/// format default). Single-string URIs for `redis://`, `postgres://`, and
/// `mem://` are rejected by the core resolver; use `Store::save_schematic`
/// with an explicit store for those backends.
pub fn save(
schematic: &Schematic,
uri: &DiplomatStr,
version: &DiplomatStr,
) -> Result<(), NucleationError> {
let uri = Self::utf8(uri)?;
let version = Self::utf8(version)?;
let version = if version.is_empty() {
None
} else {
Some(version)
};
schematic
.0
.save(uri, version)
.map_err(|_| NucleationError::Store)
}
/// The JSON schema describing the export settings of `format`. Errors
/// with `NotFound` for an unknown format.
pub fn export_settings_schema(
format: &DiplomatStr,
out: &mut DiplomatWrite,
) -> Result<(), NucleationError> {
let format = Self::utf8(format)?;
let manager = crate::formats::manager::get_manager();
let manager = manager.lock().map_err(|_| NucleationError::Lock)?;
let schema = manager
.get_export_settings_schema(format)
.ok_or(NucleationError::NotFound)?;
let _ = write!(out, "{}", schema);
Ok(())
}
/// The JSON schema describing the import settings of `format`. Errors
/// with `NotFound` for an unknown format.
pub fn import_settings_schema(
format: &DiplomatStr,
out: &mut DiplomatWrite,
) -> Result<(), NucleationError> {
let format = Self::utf8(format)?;
let manager = crate::formats::manager::get_manager();
let manager = manager.lock().map_err(|_| NucleationError::Lock)?;
let schema = manager
.get_import_settings_schema(format)
.ok_or(NucleationError::NotFound)?;
let _ = write!(out, "{}", schema);
Ok(())
}
/// The supported import formats, written as a JSON array string.
pub fn supported_import_formats(out: &mut DiplomatWrite) -> Result<(), NucleationError> {
let manager = crate::formats::manager::get_manager();
let manager = manager.lock().map_err(|_| NucleationError::Lock)?;
let json = serde_json::to_string(&manager.list_importers())
.map_err(|_| NucleationError::Serialize)?;
let _ = write!(out, "{}", json);
Ok(())
}
/// The supported export formats, written as a JSON array string.
pub fn supported_export_formats(out: &mut DiplomatWrite) -> Result<(), NucleationError> {
let manager = crate::formats::manager::get_manager();
let manager = manager.lock().map_err(|_| NucleationError::Lock)?;
let json = serde_json::to_string(&manager.list_exporters())
.map_err(|_| NucleationError::Serialize)?;
let _ = write!(out, "{}", json);
Ok(())
}
/// The known versions of an export format, written as a JSON array string
/// (empty array for an unknown format, matching the old ABI).
pub fn format_versions(
format: &DiplomatStr,
out: &mut DiplomatWrite,
) -> Result<(), NucleationError> {
let format = Self::utf8(format)?;
let manager = crate::formats::manager::get_manager();
let manager = manager.lock().map_err(|_| NucleationError::Lock)?;
let versions = manager.get_exporter_versions(format).unwrap_or_default();
let json = serde_json::to_string(&versions).map_err(|_| NucleationError::Serialize)?;
let _ = write!(out, "{}", json);
Ok(())
}
/// The default version of an export format. Errors with `NotFound` for an
/// unknown format.
pub fn default_format_version(
format: &DiplomatStr,
out: &mut DiplomatWrite,
) -> Result<(), NucleationError> {
let format = Self::utf8(format)?;
let manager = crate::formats::manager::get_manager();
let manager = manager.lock().map_err(|_| NucleationError::Lock)?;
let version = manager
.get_exporter_default_version(format)
.ok_or(NucleationError::NotFound)?;
let _ = write!(out, "{}", version);
Ok(())
}
}
}