calcit 0.13.21

Interpreter and js codegen for Calcit
Documentation
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use cirru_edn::{Edn, EdnMapView, EdnStructView, from_edn};
use cirru_parser::Cirru;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::Path;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SnapshotRunMode {
  #[default]
  Native,
  Js,
}

fn deserialize_run_mode<'de, D>(deserializer: D) -> Result<SnapshotRunMode, D::Error>
where
  D: serde::Deserializer<'de>,
{
  let value = Edn::deserialize(deserializer)?;
  let mode = match value {
    Edn::Tag(tag) => tag.ref_str().to_owned(),
    Edn::Str(text) | Edn::Symbol(text) => text.trim_start_matches(':').to_owned(),
    other => return Err(serde::de::Error::custom(format!("expected :native or :js, got {other:?}"))),
  };
  match mode.as_str() {
    "native" => Ok(SnapshotRunMode::Native),
    "js" => Ok(SnapshotRunMode::Js),
    _ => Err(serde::de::Error::custom(format!("expected :native or :js, got {mode}"))),
  }
}

fn deserialize_ns_def<'de, D>(deserializer: D) -> Result<String, D::Error>
where
  D: serde::Deserializer<'de>,
{
  match Edn::deserialize(deserializer)? {
    Edn::Str(text) | Edn::Symbol(text) => Ok(text.to_string()),
    other => Err(serde::de::Error::custom(format!(
      "expected namespace/definition string or symbol, got {other:?}"
    ))),
  }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SnapshotEntry {
  #[serde(default, deserialize_with = "deserialize_run_mode")]
  pub mode: SnapshotRunMode,
  #[serde(rename = "init-fn", deserialize_with = "deserialize_ns_def")]
  pub init_fn: String,
  #[serde(rename = "reload-fn", deserialize_with = "deserialize_ns_def")]
  pub reload_fn: String,
  #[serde(default)]
  pub description: String,
  #[serde(default)]
  pub modules: Vec<String>,
  #[serde(default, rename = "type-slots")]
  pub type_slots: HashMap<String, String>,
  #[serde(default, rename = "feature-policy")]
  pub feature_policy: HashMap<String, String>,
  #[serde(default)]
  pub target: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LegacySnapshotConfigs {
  #[serde(rename = "init-fn", deserialize_with = "deserialize_ns_def")]
  pub init_fn: String,
  #[serde(rename = "reload-fn", deserialize_with = "deserialize_ns_def")]
  pub reload_fn: String,
  #[serde(default)]
  pub modules: Vec<String>,
  #[serde(default)]
  pub version: String,
  #[serde(default, rename = "type-slots")]
  pub type_slots: HashMap<String, String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CodeEntry {
  pub doc: String,
  #[serde(default)]
  pub examples: Vec<Cirru>,
  #[serde(default)]
  pub tests: Vec<TestEntry>,
  #[serde(default)]
  pub tags: Vec<String>,
  pub code: Cirru,
  #[serde(default)]
  pub schema: Option<Edn>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TestEntry {
  pub name: String,
  pub code: Cirru,
  #[serde(default)]
  pub tags: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NsEntry {
  pub doc: String,
  pub code: Cirru,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FileInSnapShot {
  pub ns: NsEntry,
  pub defs: HashMap<String, CodeEntry>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Snapshot {
  pub package: String,
  pub about: Option<String>,
  #[serde(default)]
  pub version: String,
  pub entries: HashMap<String, SnapshotEntry>,
  pub files: HashMap<String, FileInSnapShot>,
}

fn parse_snapshot_entry(data: Edn) -> Result<SnapshotEntry, String> {
  let map = data.view_map().map_err(|e| format!("entry must be a map: {e}"))?;
  let ns_def = |key: &str| match map.get_or_nil(key) {
    Edn::Str(text) | Edn::Symbol(text) => Ok(text.to_string()),
    other => Err(format!(
      "entry `:{key}` must be a namespace/definition string or symbol, got {other:?}"
    )),
  };
  let mode = match map.get_or_nil("mode") {
    Edn::Tag(tag) if tag.ref_str() == "native" => SnapshotRunMode::Native,
    Edn::Tag(tag) if tag.ref_str() == "js" => SnapshotRunMode::Js,
    Edn::Str(text) | Edn::Symbol(text) if text.trim_start_matches(':') == "native" => SnapshotRunMode::Native,
    Edn::Str(text) | Edn::Symbol(text) if text.trim_start_matches(':') == "js" => SnapshotRunMode::Js,
    other => return Err(format!("entry `:mode` must be :native or :js, got {other:?}")),
  };
  Ok(SnapshotEntry {
    mode,
    init_fn: ns_def("init-fn")?,
    reload_fn: ns_def("reload-fn")?,
    description: from_edn(map.get_or_nil("description")).map_err(|e| format!("entry `:description`: {e}"))?,
    modules: from_edn(map.get_or_nil("modules")).map_err(|e| format!("entry `:modules`: {e}"))?,
    type_slots: from_edn(map.get_or_nil("type-slots")).map_err(|e| format!("entry `:type-slots`: {e}"))?,
    feature_policy: match map.get_or_nil("feature-policy") {
      Edn::Nil => HashMap::new(),
      value => parse_feature_policy(value)?,
    },
    target: match map.get_or_nil("target") {
      Edn::Nil => None,
      value => Some(from_edn(value).map_err(|e| format!("entry `:target`: {e}"))?),
    },
  })
}

fn parse_feature_policy(data: Edn) -> Result<HashMap<String, String>, String> {
  let map = data.view_map().map_err(|e| format!("entry `:feature-policy` must be a map: {e}"))?;
  let text = |value: &Edn| match value {
    Edn::Tag(tag) => Ok(tag.ref_str().to_owned()),
    Edn::Str(value) | Edn::Symbol(value) => Ok(value.to_string().trim_start_matches(':').to_owned()),
    other => Err(format!("entry `:feature-policy` expects tag/string/symbol values, got {other:?}")),
  };
  map.0.iter().map(|(key, value)| Ok((text(key)?, text(value)?))).collect()
}

fn parse_entries(data: Edn) -> Result<HashMap<String, SnapshotEntry>, String> {
  let map = data.view_map().map_err(|e| format!("entries must be a map: {e}"))?;
  let mut entries = HashMap::with_capacity(map.0.len());
  for (name, value) in map.0 {
    let name: String = from_edn(name).map_err(|e| format!("invalid entry name: {e}"))?;
    entries.insert(name, parse_snapshot_entry(value)?);
  }
  Ok(entries)
}

fn format_edn_preview(value: &Edn) -> String {
  let raw = cirru_edn::format(value, true).unwrap_or_else(|_| format!("{value:?}"));
  const LIMIT: usize = 220;
  if raw.chars().count() > LIMIT {
    let truncated = raw.chars().take(LIMIT).collect::<String>();
    format!("{truncated}…")
  } else {
    raw
  }
}

fn truncate_preview(raw: &str, limit: usize) -> String {
  if raw.chars().count() > limit {
    let truncated = raw.chars().take(limit).collect::<String>();
    format!("{truncated}…")
  } else {
    raw.to_owned()
  }
}

fn truncate_edn_error_nodes(message: &str) -> String {
  const NODE_LIMIT: usize = 200;

  message
    .lines()
    .map(|line| {
      if let Some((prefix, preview)) = line.split_once("Node: ") {
        format!("{prefix}Node: {}", truncate_preview(preview, NODE_LIMIT))
      } else {
        line.to_owned()
      }
    })
    .collect::<Vec<_>>()
    .join("\n")
}

fn format_edn_error<E: std::fmt::Display>(error: E) -> String {
  truncate_edn_error_nodes(&error.to_string())
}

fn schema_path_label(path: &[String]) -> String {
  if path.is_empty() { "<root>".to_owned() } else { path.join("") }
}

fn map_key_path_segment(key: &Edn) -> String {
  match key {
    Edn::Tag(tag) => format!(".{}", tag.ref_str()),
    Edn::Str(text) => format!(".{text}"),
    Edn::Symbol(text) => format!(".{text}"),
    _ => ".<key>".to_owned(),
  }
}

fn canonical_schema_field_name(text: &str) -> Option<&'static str> {
  match text.trim_start_matches(':') {
    "kind" => Some("kind"),
    "args" => Some("args"),
    "return" => Some("return"),
    "rest" => Some("rest"),
    "generics" => Some("generics"),
    "where" => Some("where"),
    _ => None,
  }
}

fn canonical_schema_kind_name(text: &str) -> Option<&'static str> {
  match text.trim_start_matches(':') {
    "fn" => Some("fn"),
    "macro" => Some("macro"),
    _ => None,
  }
}

fn normalize_schema_map(map: &EdnMapView) -> Edn {
  let mut normalized = EdnMapView::default();

  for (key, value) in &map.0 {
    let normalized_key = match key {
      Edn::Tag(tag) => Edn::tag(tag.ref_str()),
      Edn::Str(text) => canonical_schema_field_name(text.as_ref())
        .map(Edn::tag)
        .unwrap_or_else(|| key.clone()),
      Edn::Symbol(text) => canonical_schema_field_name(text.as_ref())
        .map(Edn::tag)
        .unwrap_or_else(|| key.clone()),
      _ => key.clone(),
    };

    let normalized_value = match (&normalized_key, value) {
      (Edn::Tag(tag), Edn::Str(text)) | (Edn::Tag(tag), Edn::Symbol(text)) if tag.ref_str() == "kind" => {
        canonical_schema_kind_name(text.as_ref())
          .map(Edn::tag)
          .unwrap_or_else(|| value.clone())
      }
      _ => value.clone(),
    };

    normalized.insert(normalized_key, normalized_value);
  }

  Edn::Map(normalized)
}

/// Convert a schema Edn value (either old Quote-wrapped or new direct map) into Edn map form.
fn parse_schema_from_edn(value: &Edn, owner: &str) -> Result<Edn, String> {
  // Simple scalar schemas (e.g. :dynamic, :nil) are valid as-is — skip Cirru path
  match value {
    Edn::Tag(_) | Edn::Nil => {
      validate_schema_edn_no_legacy_quotes(value, owner)?;
      return Ok(value.clone());
    }
    Edn::Map(map) => {
      let normalized = normalize_schema_map(map);
      validate_schema_edn_no_legacy_quotes(&normalized, owner)?;
      return Ok(normalized);
    }
    Edn::Enum(view) if matches!(view.variant.as_ref(), "fn" | "macro") && matches!(view.extra.first(), Some(Edn::Map(_))) => {
      let Some(Edn::Map(map)) = view.extra.first() else {
        unreachable!();
      };
      let mut normalized = match normalize_schema_map(map) {
        Edn::Map(map) => map,
        _ => unreachable!(),
      };
      if normalized.tag_get("kind").is_none() && view.variant.as_ref() == "macro" {
        normalized.insert_key("kind", Edn::tag("macro"));
      }
      let normalized = Edn::Map(normalized);
      validate_schema_edn_no_legacy_quotes(&normalized, owner)?;
      return Ok(normalized);
    }
    _ => {}
  }
  // Old format: Edn::Quote wrapping Cirru — convert to direct map Edn
  if let Ok(cirru) = from_edn::<Cirru>(value.clone()) {
    let text = cirru_parser::format(&[cirru], true.into())
      .map_err(|e| format!("{owner}: failed to format quoted schema before validation: {}", format_edn_error(e)))?;
    let parsed = cirru_edn::parse(&text).map_err(|e| {
      format!(
        "{owner}: failed to parse quoted schema after formatting: {}; schema={}",
        format_edn_error(e),
        truncate_preview(&text, 200)
      )
    })?;
    validate_schema_edn_no_legacy_quotes(&parsed, owner)?;
    return Ok(parsed);
  }
  // New format: already a direct Edn map
  validate_schema_edn_no_legacy_quotes(value, owner)?;
  Ok(value.clone())
}

fn validate_schema_edn_no_legacy_quotes(value: &Edn, owner: &str) -> Result<(), String> {
  fn walk(value: &Edn, owner: &str, path: &mut Vec<String>) -> Result<(), String> {
    match value {
      Edn::Symbol(s) => {
        if s.starts_with('\'') {
          let inner = s.trim_start_matches('\'');
          return Err(format!(
            "{owner}: invalid schema generic symbol `{s}` at {}. Use source syntax like `'{inner}`, but store it as plain EDN symbol `{inner}`.",
            schema_path_label(path)
          ));
        }
        Ok(())
      }
      Edn::List(xs) => {
        for (idx, item) in xs.0.iter().enumerate() {
          path.push(format!("[{idx}]"));
          walk(item, owner, path)?;
          path.pop();
        }
        Ok(())
      }
      Edn::Map(map) => {
        for (k, v) in &map.0 {
          path.push(map_key_path_segment(k));
          walk(v, owner, path)?;
          path.pop();
        }
        Ok(())
      }
      Edn::Enum(view) => {
        if view.variant.starts_with('\'') {
          return Err(format!(
            "{owner}: invalid schema enum symbol `{}` at {}",
            view.variant,
            schema_path_label(path)
          ));
        }
        for (idx, item) in view.extra.iter().enumerate() {
          path.push(format!("[{idx}]"));
          walk(item, owner, path)?;
          path.pop();
        }
        Ok(())
      }
      Edn::Set(set) => {
        for (idx, item) in set.0.iter().enumerate() {
          path.push(format!("[#{idx}]"));
          walk(item, owner, path)?;
          path.pop();
        }
        Ok(())
      }
      Edn::Struct(_) => Ok(()),
      _ => Ok(()),
    }
  }

  let mut path = vec![];
  walk(value, owner, &mut path)
}

fn parse_tags_from_edn(value: &Edn, owner: &str, field: &str) -> Result<Vec<String>, String> {
  match value {
    Edn::Set(set) => {
      let mut tags = Vec::with_capacity(set.0.len());
      for item in &set.0 {
        match item {
          Edn::Tag(tag) => tags.push(format!(":{}", tag.ref_str())),
          other => {
            return Err(format!("{owner}: {field} expects tag items, got {}", format_edn_preview(other)));
          }
        }
      }
      tags.sort();
      tags.dedup();
      Ok(tags)
    }
    other => Err(format!("{owner}: {field} expects a hashset, got {}", format_edn_preview(other))),
  }
}

fn parse_code_entry(edn: Edn, owner: &str) -> Result<CodeEntry, String> {
  let struct_value: EdnStructView = match edn {
    Edn::Struct(r) => r,
    other => return Err(format!("{owner}: expected CodeEntry struct, got {}", format_edn_preview(&other))),
  };
  let mut doc = String::new();
  let mut examples: Vec<Cirru> = vec![];
  let mut tests: Vec<TestEntry> = vec![];
  let mut tags: Vec<String> = Vec::new();
  let mut code: Option<Cirru> = None;
  let mut schema: Option<Edn> = None;
  for (key, value) in &struct_value.pairs {
    match key.arc_str().as_ref() {
      "doc" => doc = from_edn(value.clone()).map_err(|e| format!("{owner}: invalid `:doc`: {e}"))?,
      "examples" => examples = from_edn(value.clone()).map_err(|e| format!("{owner}: invalid `:examples`: {e}"))?,
      "tests" => {
        let Edn::List(items) = value else {
          return Err(format!("{owner}: `:tests` expects a list, got {}", format_edn_preview(value)));
        };
        tests = items
          .0
          .iter()
          .cloned()
          .map(|item| parse_test_entry(item, owner))
          .collect::<Result<Vec<_>, _>>()?;
        let mut names = std::collections::HashSet::new();
        for test in &tests {
          if !names.insert(test.name.as_str()) {
            return Err(format!("{owner}: duplicate test name `{}`", test.name));
          }
        }
      }
      "tags" => tags = parse_tags_from_edn(value, owner, "CodeEntry.tags")?,
      "code" => code = Some(from_edn(value.clone()).map_err(|e| format!("{owner}: invalid `:code`: {e}"))?),
      "schema" if !matches!(value, Edn::Nil) => {
        schema = Some(parse_schema_from_edn(value, owner).map_err(|e| format!("{owner}: invalid `:schema`: {e}"))?);
      }
      _ => {}
    }
  }
  Ok(CodeEntry {
    doc,
    examples,
    tests,
    tags,
    code: code.ok_or_else(|| format!("{owner}: missing `:code` field in CodeEntry"))?,
    schema,
  })
}

fn parse_test_entry(edn: Edn, owner: &str) -> Result<TestEntry, String> {
  let struct_value = match edn {
    Edn::Struct(value) => value,
    other => return Err(format!("{owner}: expected TestEntry struct, got {}", format_edn_preview(&other))),
  };
  let mut name = None;
  let mut code = None;
  let mut tags = Vec::new();
  for (key, value) in &struct_value.pairs {
    match key.ref_str() {
      "name" => name = Some(from_edn(value.clone()).map_err(|error| format!("{owner}: invalid test `:name`: {error}"))?),
      "code" => code = Some(from_edn(value.clone()).map_err(|error| format!("{owner}: invalid test `:code`: {error}"))?),
      "tags" => tags = parse_tags_from_edn(value, owner, "TestEntry.tags")?,
      _ => {}
    }
  }
  let name: String = name.ok_or_else(|| format!("{owner}: test is missing `:name`"))?;
  if name.trim().is_empty() {
    return Err(format!("{owner}: test name must not be empty"));
  }
  Ok(TestEntry {
    name,
    code: code.ok_or_else(|| format!("{owner}: test is missing `:code`"))?,
    tags,
  })
}

fn parse_ns_entry(edn: Edn, owner: &str) -> Result<NsEntry, String> {
  let struct_value: EdnStructView = match edn {
    Edn::Struct(r) => r,
    other => {
      return Err(format!(
        "{owner}: expected NsEntry/CodeEntry struct, got {}",
        format_edn_preview(&other)
      ));
    }
  };
  let mut doc = String::new();
  let mut code: Option<Cirru> = None;
  for (key, value) in &struct_value.pairs {
    match key.arc_str().as_ref() {
      "doc" => doc = from_edn(value.clone()).map_err(|e| format!("{owner}: invalid `:doc`: {e}"))?,
      "code" => code = Some(from_edn(value.clone()).map_err(|e| format!("{owner}: invalid `:code`: {e}"))?),
      _ => {}
    }
  }
  Ok(NsEntry {
    doc,
    code: code.ok_or_else(|| format!("{owner}: missing `:code` field in NsEntry"))?,
  })
}

fn parse_file_in_snapshot(edn: Edn, file_name: &str) -> Result<FileInSnapShot, String> {
  let struct_value: EdnStructView = match edn {
    Edn::Struct(r) => r,
    other => {
      return Err(format!(
        "{file_name}: expected FileEntry struct, got {}",
        format_edn_preview(&other)
      ));
    }
  };
  let mut ns: Option<NsEntry> = None;
  let mut defs: HashMap<String, CodeEntry> = HashMap::new();
  for (key, value) in &struct_value.pairs {
    match key.arc_str().as_ref() {
      "ns" => ns = Some(parse_ns_entry(value.clone(), &format!("{file_name}/:ns"))?),
      "defs" => {
        let map = match value {
          Edn::Map(m) => m,
          other => return Err(format!("{file_name}: expected `:defs` map, got {}", format_edn_preview(other))),
        };
        for (def_key, def_value) in &map.0 {
          let name: String = from_edn(def_key.clone()).map_err(|e| format!("{file_name}: invalid def key: {e}"))?;
          let owner = format!("{file_name}/{name}");
          defs.insert(name, parse_code_entry(def_value.clone(), &owner)?);
        }
      }
      _ => {}
    }
  }
  Ok(FileInSnapShot {
    ns: ns.ok_or_else(|| format!("{file_name}: missing `:ns` field in FileEntry"))?,
    defs,
  })
}

fn parse_files(edn: Edn) -> Result<HashMap<String, FileInSnapShot>, String> {
  match edn {
    Edn::Map(map) => {
      let mut result = HashMap::with_capacity(map.0.len());
      for (key, value) in map.0 {
        let name: String = from_edn(key).map_err(|e| format!("invalid file key: {e}"))?;
        result.insert(name.clone(), parse_file_in_snapshot(value, &name)?);
      }
      Ok(result)
    }
    other => Err(format!("snapshot `:files` must be a map, got {}", format_edn_preview(&other))),
  }
}

fn main() {
  println!("cargo:rerun-if-changed=src/cirru/calcit-core.cirru");

  let out_dir = env::var_os("OUT_DIR").unwrap();
  let dest_path = Path::new(&out_dir).join("calcit-core.rmp");

  let core_content =
    fs::read_to_string("src/cirru/calcit-core.cirru").unwrap_or_else(|e| panic!("failed to read src/cirru/calcit-core.cirru: {e}"));
  let core_data = cirru_edn::parse(&core_content)
    .unwrap_or_else(|e| panic!("failed to parse src/cirru/calcit-core.cirru as Cirru EDN: {}", format_edn_error(e)));

  // Minimal logic to convert Edn to Snapshot as in src/snapshot.rs
  let data = core_data
    .view_map()
    .unwrap_or_else(|e| panic!("calcit-core snapshot root must be a map: {e}"));
  let pkg: String = from_edn(data.get_or_nil("package")).unwrap_or_else(|e| panic!("failed to parse calcit-core `:package`: {e}"));
  let about = match data.get_or_nil("about") {
    Edn::Nil => None,
    value => Some(from_edn::<String>(value).unwrap_or_else(|e| panic!("failed to parse calcit-core `:about`: {e}"))),
  };

  let files = parse_files(data.get_or_nil("files")).unwrap_or_else(|e| panic!("failed to parse calcit-core `:files`: {e}"));

  let legacy_configs = match data.get_or_nil("configs") {
    Edn::Nil => None,
    value => {
      Some(from_edn::<LegacySnapshotConfigs>(value).unwrap_or_else(|e| panic!("failed to parse calcit-core legacy `:configs`: {e}")))
    }
  };
  let mut entries = parse_entries(data.get_or_nil("entries")).unwrap_or_else(|e| panic!("failed to parse calcit-core `:entries`: {e}"));
  if let Some(configs) = &legacy_configs {
    entries.insert(
      "default".to_owned(),
      SnapshotEntry {
        mode: SnapshotRunMode::Native,
        init_fn: configs.init_fn.clone(),
        reload_fn: configs.reload_fn.clone(),
        description: String::new(),
        modules: configs.modules.clone(),
        type_slots: configs.type_slots.clone(),
        feature_policy: HashMap::new(),
        target: None,
      },
    );
  }
  let version = match data.get_or_nil("version") {
    Edn::Nil => legacy_configs.map(|configs| configs.version).unwrap_or_default(),
    value => from_edn(value).unwrap_or_else(|e| panic!("failed to parse calcit-core `:version`: {e}")),
  };

  let snapshot = Snapshot {
    package: pkg,
    about,
    version,
    entries,
    files,
  };

  let mut buf = Vec::new();
  snapshot
    .serialize(&mut rmp_serde::Serializer::new(&mut buf))
    .unwrap_or_else(|e| panic!("failed to serialize embedded calcit-core snapshot: {e}"));
  fs::write(dest_path, buf).unwrap_or_else(|e| panic!("failed to write embedded calcit-core snapshot: {e}"));
}