calcit 0.12.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
use argh::FromArgs;
use calcit::detailed_snapshot::{
  DetailCirru, DetailedCodeEntry, DetailedFileInSnapshot, DetailedNsEntry, DetailedSnapshot, load_detailed_snapshot_data,
};
use calcit::snapshot::{CodeEntry, FileInSnapShot, NsEntry, Snapshot, load_snapshot_data};
use cirru_edn::Edn;
use cirru_parser::Cirru;
use std::fmt::Debug;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;

#[derive(FromArgs)]
/// Sync changes from compact.cirru to calcit.cirru while preserving metadata
struct Args {
  #[argh(option, short = 'c', default = "PathBuf::from(\"compact.cirru\")")]
  /// path to compact.cirru file
  compact_path: PathBuf,

  #[argh(option, short = 'f', default = "PathBuf::from(\"calcit.cirru\")")]
  /// path to calcit.cirru file
  calcit_path: PathBuf,

  #[argh(switch, short = 'd')]
  /// dry run mode - show changes without applying them
  dry_run: bool,

  #[argh(switch, short = 'v')]
  /// verbose output
  verbose: bool,
}

/// Print code changes with aligned line headers for easier comparison of differences
fn print_code_change(from: &Cirru, to: &Cirru) {
  println!("    Code change:");
  println!("      From: {from:?}");
  println!("      To:   {to:?}");
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let args: Args = argh::from_env();

  if args.verbose {
    println!(
      "Syncing changes from {} to {}",
      args.compact_path.display(),
      args.calcit_path.display()
    );
  }

  // Read both files
  let compact_content = fs::read_to_string(&args.compact_path).map_err(|e| format!("Failed to read compact.cirru: {e}"))?;

  let calcit_content = fs::read_to_string(&args.calcit_path).map_err(|e| format!("Failed to read calcit.cirru: {e}"))?;

  // Parse compact file as Snapshot
  let compact_data = cirru_edn::parse(&compact_content).map_err(|e| format!("Failed to parse compact file: {e}"))?;
  let compact_snapshot = load_snapshot_data(&compact_data, &args.compact_path.to_string_lossy())
    .map_err(|e| format!("Failed to load compact snapshot: {e}"))?;

  // Parse calcit file as DetailedSnapshot
  let calcit_data = cirru_edn::parse(&calcit_content).map_err(|e| format!("Failed to parse calcit file: {e}"))?;
  let mut detailed_snapshot = load_detailed_snapshot_data(&calcit_data, &args.calcit_path.to_string_lossy())
    .map_err(|e| format!("Failed to load detailed snapshot: {e}"))?;

  if args.verbose {
    println!("Loaded compact snapshot with {} files", compact_snapshot.files.len());
    println!("Loaded calcit snapshot with {} files", detailed_snapshot.files.len());
  }

  // Detect changes between the two snapshots
  let changes = detect_snapshot_changes(&compact_snapshot, &detailed_snapshot);

  // Check if configs or entries changed
  let configs_changed = has_configs_changed(&compact_snapshot, &detailed_snapshot);
  let entries_changed = has_entries_changed(&compact_snapshot, &detailed_snapshot);

  if changes.is_empty() && !configs_changed && !entries_changed {
    println!("No changes detected between compact and calcit files.");
    return Ok(());
  }

  if args.verbose {
    if configs_changed {
      println!("Configs changed");
    }
    if entries_changed {
      println!("Entries changed");
    }
  }

  if args.verbose {
    println!("Detected {} changes:", changes.len());
    for change in &changes {
      println!("  {change}");

      // 打印差异原因的详细信息
      if let (Some(SnapshotEntry::Def(new_entry)), ChangePath::FunctionDefinition { file_name, def_name }) =
        (&change.new_entry, &change.path)
      {
        if let Some(detailed_file) = detailed_snapshot.files.get(file_name) {
          if let Some(detailed_entry) = detailed_file.defs.get(def_name) {
            match change.change_type {
              ChangeType::ModifiedCode => {
                let compact_cirru: Cirru = new_entry.code.clone();
                let detailed_cirru: Cirru = detailed_entry.code.clone().into();
                print_code_change(&detailed_cirru, &compact_cirru);
              }
              ChangeType::ModifiedDoc => {
                println!("    Doc change: from \"{}\" to \"{}\"", detailed_entry.doc, new_entry.doc);
              }
              ChangeType::Modified => {
                let compact_cirru: Cirru = new_entry.code.clone();
                let detailed_cirru: Cirru = detailed_entry.code.clone().into();
                print_code_change(&detailed_cirru, &compact_cirru);
                println!("    Doc change: from \"{}\" to \"{}\"", detailed_entry.doc, new_entry.doc);
              }
              _ => {}
            }
          }
        }
      } else if let (Some(SnapshotEntry::Ns(new_entry)), ChangePath::NamespaceDefinition { file_name }) =
        (&change.new_entry, &change.path)
      {
        if let Some(detailed_file) = detailed_snapshot.files.get(file_name) {
          match change.change_type {
            ChangeType::ModifiedCode => {
              let compact_cirru: Cirru = new_entry.code.clone();
              let detailed_cirru: Cirru = detailed_file.ns.code.clone().into();
              print_code_change(&detailed_cirru, &compact_cirru);
            }
            ChangeType::ModifiedDoc => {
              println!("    Doc change: from \"{}\" to \"{}\"", detailed_file.ns.doc, new_entry.doc);
            }
            ChangeType::Modified => {
              let compact_cirru: Cirru = new_entry.code.clone();
              let detailed_cirru: Cirru = detailed_file.ns.code.clone().into();
              print_code_change(&detailed_cirru, &compact_cirru);
              println!("    Doc change: from \"{}\" to \"{}\"", detailed_file.ns.doc, new_entry.doc);
            }
            _ => {}
          }
        }
      }
    }
  }

  if args.dry_run {
    println!("Dry run mode: would apply {} changes", changes.len());
    if configs_changed {
      println!("Would update configs");
    }
    if entries_changed {
      println!("Would update entries");
    }
    return Ok(());
  }

  // Apply changes to detailed snapshot
  apply_snapshot_changes(&mut detailed_snapshot, &changes);

  // Merge configs and entries if changed
  if configs_changed || entries_changed {
    merge_configs_and_entries(&mut detailed_snapshot, &compact_snapshot);
  }

  // Convert DetailedSnapshot back to Edn for saving
  let updated_edn = detailed_snapshot_to_edn(&detailed_snapshot);
  let formatted_content = cirru_edn::format(&updated_edn, true).map_err(|e| format!("Failed to format updated calcit.cirru: {e}"))?;

  fs::write(&args.calcit_path, formatted_content).map_err(|e| format!("Failed to write calcit.cirru: {e}"))?;

  println!("Successfully applied {} changes to {}", changes.len(), args.calcit_path.display());

  Ok(())
}

#[derive(Debug, Clone)]
enum ChangePath {
  NamespaceDefinition { file_name: String },
  FunctionDefinition { file_name: String, def_name: String },
}

impl std::fmt::Display for ChangePath {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      ChangePath::NamespaceDefinition { file_name } => write!(f, "{file_name}.ns"),
      ChangePath::FunctionDefinition { file_name, def_name } => write!(f, "{file_name}.defs.{def_name}"),
    }
  }
}

#[derive(Debug, Clone)]
enum SnapshotEntry {
  Ns(NsEntry),
  Def(CodeEntry),
}

#[derive(Debug, Clone)]
struct SnapshotChange {
  path: ChangePath,
  change_type: ChangeType,
  new_entry: Option<SnapshotEntry>,
}

#[derive(Debug, Clone, PartialEq)]
enum ChangeType {
  Added,
  Modified,
  ModifiedCode,
  ModifiedDoc,
  Removed,
}

impl std::fmt::Display for SnapshotChange {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match &self.change_type {
      ChangeType::Added => write!(f, "Added: {}", self.path),
      ChangeType::Modified => write!(f, "Modified: {}", self.path),
      ChangeType::ModifiedCode => write!(f, "Modified code: {}", self.path),
      ChangeType::ModifiedDoc => write!(f, "Modified doc: {}", self.path),
      ChangeType::Removed => write!(f, "Removed: {}", self.path),
    }
  }
}

fn detailed_snapshot_to_edn(snapshot: &DetailedSnapshot) -> Edn {
  // Create a modified snapshot, handling special cases
  let mut modified_snapshot = snapshot.clone();

  // Filter out $meta namespaces
  modified_snapshot.files.retain(|k, _| !k.ends_with(".$meta"));

  // Fix empty or unknown namespace declarations
  for (file_name, file) in &mut modified_snapshot.files {
    let ns_cirru: Cirru = file.ns.code.clone().into();
    if ns_cirru == vec!["ns", "unknown"].into() || ns_cirru.is_empty() {
      let fixed_cirru: Cirru = vec!["ns", file_name.as_str()].into();
      file.ns.code = fixed_cirru.into();
    }
  }
  // Manually build EDN structure for DetailedSnapshot
  let mut edn_map = cirru_edn::EdnMapView::default();

  // Add package
  edn_map.insert_key("package", Edn::Str(modified_snapshot.package.as_str().into()));

  // Add configs
  edn_map.insert_key("configs", modified_snapshot.configs.clone());

  // Add entries
  edn_map.insert_key("entries", modified_snapshot.entries.clone());

  // Add files
  let mut files_map = cirru_edn::EdnMapView::default();
  for (k, v) in &modified_snapshot.files {
    // Skip $meta namespaces as they are special
    if k.ends_with(".$meta") {
      continue;
    }

    let mut file_record = cirru_edn::EdnRecordView {
      tag: cirru_edn::EdnTag::new("FileEntry"),
      pairs: Vec::new(),
    };

    // Add defs field
    let mut defs_map = cirru_edn::EdnMapView::default();
    for (def_name, def_entry) in &v.defs {
      defs_map.insert(Edn::str(def_name.as_str()), detailed_code_entry_to_edn(def_entry));
    }
    file_record.pairs.push(("defs".into(), Edn::from(defs_map)));

    // Add ns field, make sure "ns" is after "defs"
    file_record.pairs.push(("ns".into(), detailed_ns_entry_to_edn(&v.ns)));

    files_map.insert(Edn::str(k.as_str()), Edn::Record(file_record));
  }
  edn_map.insert_key("files", files_map.into());

  // Add users if present
  if !modified_snapshot.users.is_nil() {
    edn_map.insert_key("users", modified_snapshot.users.clone());
  }

  Edn::from(edn_map)
}

// Helper function to convert DetailedCodeEntry to Edn
fn detailed_code_entry_to_edn(entry: &DetailedCodeEntry) -> Edn {
  let mut record = cirru_edn::EdnRecordView {
    tag: cirru_edn::EdnTag::new("CodeEntry"),
    pairs: Vec::new(),
  };

  // Add code field (must be first based on Calcit's expectation)
  record.pairs.push(("code".into(), detailed_cirru_to_edn(&entry.code)));

  // Add doc field
  record.pairs.push(("doc".into(), Edn::Str(entry.doc.as_str().into())));

  // Add examples field - convert DetailCirru to simple Cirru first (without metadata)
  let examples_list: Vec<Edn> = entry
    .examples
    .iter()
    .map(|e| {
      let simple_cirru: Cirru = e.clone().into();
      simple_cirru.into()
    })
    .collect();
  record.pairs.push(("examples".into(), Edn::List(examples_list.into())));

  Edn::Record(record)
}

fn detailed_ns_entry_to_edn(entry: &DetailedNsEntry) -> Edn {
  let mut record = cirru_edn::EdnRecordView {
    tag: cirru_edn::EdnTag::new("NsEntry"),
    pairs: Vec::new(),
  };

  record.pairs.push(("code".into(), detailed_cirru_to_edn(&entry.code)));
  record.pairs.push(("doc".into(), Edn::Str(entry.doc.as_str().into())));

  Edn::Record(record)
}

// Helper function to convert DetailCirru to Edn
fn detailed_cirru_to_edn(cirru: &DetailCirru) -> Edn {
  match cirru {
    DetailCirru::Leaf { at, by, text } => {
      let mut record = cirru_edn::EdnRecordView {
        tag: cirru_edn::EdnTag::new("Leaf"),
        pairs: Vec::new(),
      };

      record.pairs.push(("at".into(), Edn::Number(*at as f64)));
      record.pairs.push(("by".into(), Edn::Str(by.as_str().into())));

      if let Some(t) = text {
        record.pairs.push(("text".into(), Edn::Str(t.as_str().into())));
      }

      Edn::Record(record)
    }
    DetailCirru::List { data, at, by } => {
      let mut record = cirru_edn::EdnRecordView {
        tag: cirru_edn::EdnTag::new("Expr"), // align with old implementation
        pairs: Vec::new(),
      };

      record.pairs.push(("at".into(), Edn::Number(*at as f64)));
      record.pairs.push(("by".into(), Edn::Str(by.as_str().into())));

      let mut data_map = cirru_edn::EdnMapView::default();
      for (k, v) in data {
        data_map.insert(Edn::str(k.as_str()), detailed_cirru_to_edn(v));
      }
      record.pairs.push(("data".into(), Edn::from(data_map)));

      Edn::Record(record)
    }
  }
}

/// Merge configs and entries from compact snapshot into detailed snapshot
fn merge_configs_and_entries(detailed: &mut DetailedSnapshot, compact: &Snapshot) {
  // Convert SnapshotConfigs to Edn using insert_key for proper keyword handling
  let mut configs_map = cirru_edn::EdnMapView::default();
  configs_map.insert_key("init-fn", Edn::Str(compact.configs.init_fn.as_str().into()));
  configs_map.insert_key("reload-fn", Edn::Str(compact.configs.reload_fn.as_str().into()));
  configs_map.insert_key("version", Edn::Str(compact.configs.version.as_str().into()));
  configs_map.insert_key(
    "modules",
    Edn::List(
      compact
        .configs
        .modules
        .iter()
        .map(|m| Edn::Str(m.as_str().into()))
        .collect::<Vec<_>>()
        .into(),
    ),
  );

  detailed.configs = Edn::from(configs_map);

  // Convert entries HashMap<String, SnapshotConfigs> to Edn
  let mut entries_map = cirru_edn::EdnMapView::default();
  for (key, entry_config) in &compact.entries {
    let mut entry_map = cirru_edn::EdnMapView::default();
    entry_map.insert_key("init-fn", Edn::Str(entry_config.init_fn.as_str().into()));
    entry_map.insert_key("reload-fn", Edn::Str(entry_config.reload_fn.as_str().into()));
    entry_map.insert_key("version", Edn::Str(entry_config.version.as_str().into()));
    entry_map.insert_key(
      "modules",
      Edn::List(
        entry_config
          .modules
          .iter()
          .map(|m| Edn::Str(m.as_str().into()))
          .collect::<Vec<_>>()
          .into(),
      ),
    );
    entries_map.insert_key(key.as_str(), Edn::from(entry_map));
  }
  detailed.entries = Edn::from(entries_map);
}

/// Check if configs changed between compact and detailed snapshots
fn has_configs_changed(compact: &Snapshot, detailed: &DetailedSnapshot) -> bool {
  // Compare each field
  let detailed_map = match detailed.configs.view_map() {
    Ok(m) => m,
    Err(_) => return true, // If can't parse, assume changed
  };

  // Check init-fn
  if let Ok(init_fn) = detailed_map.get_or_nil("init-fn").try_into() {
    let init_fn: Arc<str> = init_fn;
    if init_fn.as_ref() != compact.configs.init_fn.as_str() {
      return true;
    }
  } else {
    return true;
  }

  // Check reload-fn
  if let Ok(reload_fn) = detailed_map.get_or_nil("reload-fn").try_into() {
    let reload_fn: Arc<str> = reload_fn;
    if reload_fn.as_ref() != compact.configs.reload_fn.as_str() {
      return true;
    }
  } else {
    return true;
  }

  // Check version
  if let Ok(version) = detailed_map.get_or_nil("version").try_into() {
    let version: Arc<str> = version;
    if version.as_ref() != compact.configs.version.as_str() {
      return true;
    }
  } else {
    return true;
  }

  // Check modules
  if let Ok(modules_edn) = detailed_map.get_or_nil("modules").view_list() {
    let detailed_modules: Vec<String> = modules_edn
      .iter()
      .filter_map(|e| {
        let s: Result<Arc<str>, _> = e.to_owned().try_into();
        s.ok().map(|a| a.to_string())
      })
      .collect();
    if detailed_modules != compact.configs.modules {
      return true;
    }
  } else if !compact.configs.modules.is_empty() {
    return true;
  }

  false
}

/// Check if entries changed between compact and detailed snapshots
fn has_entries_changed(compact: &Snapshot, detailed: &DetailedSnapshot) -> bool {
  let detailed_entries_map = match detailed.entries.view_map() {
    Ok(m) => m,
    Err(_) => {
      // If detailed doesn't have a valid map, check if compact has entries
      return !compact.entries.is_empty();
    }
  };

  // Check if keys match - EdnMapView is a tuple struct with a HashMap inside
  let cirru_edn::EdnMapView(detailed_map) = &detailed_entries_map;
  let detailed_keys: std::collections::HashSet<String> = detailed_map
    .keys()
    .filter_map(|k| {
      let s: Result<std::sync::Arc<str>, _> = k.to_owned().try_into();
      s.ok().map(|a| a.to_string())
    })
    .collect();
  let compact_keys: std::collections::HashSet<String> = compact.entries.keys().cloned().collect();

  if detailed_keys != compact_keys {
    return true;
  }

  // Compare each entry
  for (key, compact_entry) in &compact.entries {
    if let Ok(detailed_entry_edn) = detailed_entries_map.get_or_nil(key.as_str()).view_map() {
      // Check each field
      if let Ok(init_fn) = detailed_entry_edn.get_or_nil("init-fn").try_into() {
        let init_fn: std::sync::Arc<str> = init_fn;
        if init_fn.as_ref() != compact_entry.init_fn.as_str() {
          return true;
        }
      } else {
        return true;
      }

      if let Ok(reload_fn) = detailed_entry_edn.get_or_nil("reload-fn").try_into() {
        let reload_fn: std::sync::Arc<str> = reload_fn;
        if reload_fn.as_ref() != compact_entry.reload_fn.as_str() {
          return true;
        }
      } else {
        return true;
      }

      if let Ok(version) = detailed_entry_edn.get_or_nil("version").try_into() {
        let version: std::sync::Arc<str> = version;
        if version.as_ref() != compact_entry.version.as_str() {
          return true;
        }
      } else {
        return true;
      }
    } else {
      return true;
    }
  }

  false
}

fn detect_snapshot_changes(compact: &Snapshot, detailed: &DetailedSnapshot) -> Vec<SnapshotChange> {
  let mut changes = Vec::new();

  // Compare files
  for (file_name, compact_file) in &compact.files {
    // Skip $meta namespaces
    if file_name.ends_with(".$meta") {
      continue;
    }

    match detailed.files.get(file_name) {
      Some(detailed_file) => {
        // File exists in both, compare definitions
        compare_file_definitions(file_name, compact_file, detailed_file, &mut changes);
      }
      None => {
        // File added in compact: add namespace first to preserve require rules
        changes.push(SnapshotChange {
          path: ChangePath::NamespaceDefinition {
            file_name: file_name.clone(),
          },
          change_type: ChangeType::Added,
          new_entry: Some(SnapshotEntry::Ns(compact_file.ns.clone())),
        });

        // Then add all definitions in the new file
        for (def_name, code_entry) in &compact_file.defs {
          changes.push(SnapshotChange {
            path: ChangePath::FunctionDefinition {
              file_name: file_name.clone(),
              def_name: def_name.clone(),
            },
            change_type: ChangeType::Added,
            new_entry: Some(SnapshotEntry::Def(code_entry.clone())),
          });
        }
      }
    }
  }

  // Check for removed files
  for (file_name, detailed_file) in &detailed.files {
    if !compact.files.contains_key(file_name) {
      // File removed in compact
      for def_name in detailed_file.defs.keys() {
        changes.push(SnapshotChange {
          path: ChangePath::FunctionDefinition {
            file_name: file_name.clone(),
            def_name: def_name.clone(),
          },
          change_type: ChangeType::Removed,
          new_entry: None,
        });
      }
    }
  }

  changes
}

fn compare_file_definitions(
  file_name: &str,
  compact_file: &FileInSnapShot,
  detailed_file: &DetailedFileInSnapshot,
  changes: &mut Vec<SnapshotChange>,
) {
  // Compare namespace
  let compact_ns_cirru: Cirru = compact_file.ns.code.clone();
  let detailed_ns_cirru: Cirru = detailed_file.ns.code.clone().into();

  let code_changed = compact_ns_cirru != detailed_ns_cirru;
  let doc_changed = compact_file.ns.doc != detailed_file.ns.doc;

  if code_changed || doc_changed {
    let change_type = if code_changed && doc_changed {
      ChangeType::Modified
    } else if code_changed {
      ChangeType::ModifiedCode
    } else {
      ChangeType::ModifiedDoc
    };

    changes.push(SnapshotChange {
      path: ChangePath::NamespaceDefinition {
        file_name: file_name.to_string(),
      },
      change_type,
      new_entry: Some(SnapshotEntry::Ns(compact_file.ns.clone())),
    });
  }

  // Compare definitions
  for (def_name, compact_entry) in &compact_file.defs {
    match detailed_file.defs.get(def_name) {
      Some(detailed_entry) => {
        // Definition exists in both, compare content
        let compact_cirru: Cirru = compact_entry.code.clone();
        let detailed_cirru: Cirru = detailed_entry.code.clone().into();

        let code_changed = compact_cirru != detailed_cirru;
        let doc_changed = compact_entry.doc != detailed_entry.doc;

        // Check if examples changed
        let compact_examples: Vec<Cirru> = compact_entry.examples.clone();
        let detailed_examples: Vec<Cirru> = detailed_entry.examples.iter().map(|e| e.clone().into()).collect();
        let examples_changed = compact_examples != detailed_examples;

        if code_changed || doc_changed || examples_changed {
          let change_type = if code_changed && doc_changed {
            ChangeType::Modified
          } else if code_changed {
            ChangeType::ModifiedCode
          } else {
            ChangeType::ModifiedDoc
          };

          // TODO better detect structural changes of branches and leaves in the future

          changes.push(SnapshotChange {
            path: ChangePath::FunctionDefinition {
              file_name: file_name.to_string(),
              def_name: def_name.clone(),
            },
            change_type,
            new_entry: Some(SnapshotEntry::Def(compact_entry.clone())),
          });
        }
      }
      None => {
        // Definition added in compact
        changes.push(SnapshotChange {
          path: ChangePath::FunctionDefinition {
            file_name: file_name.to_string(),
            def_name: def_name.clone(),
          },
          change_type: ChangeType::Added,
          new_entry: Some(SnapshotEntry::Def(compact_entry.clone())),
        });
      }
    }
  }

  // Check for removed definitions
  for def_name in detailed_file.defs.keys() {
    if !compact_file.defs.contains_key(def_name) {
      changes.push(SnapshotChange {
        path: ChangePath::FunctionDefinition {
          file_name: file_name.to_string(),
          def_name: def_name.clone(),
        },
        change_type: ChangeType::Removed,
        new_entry: None,
      });
    }
  }
}

fn apply_snapshot_changes(detailed: &mut DetailedSnapshot, changes: &[SnapshotChange]) {
  for change in changes {
    match change.change_type {
      ChangeType::Added => {
        if let Some(new_entry) = &change.new_entry {
          apply_add_change(detailed, &change.path, new_entry);
        }
      }
      ChangeType::Modified | ChangeType::ModifiedCode | ChangeType::ModifiedDoc => {
        if let Some(new_entry) = &change.new_entry {
          apply_modify_change(detailed, &change.path, new_entry, &change.change_type);
        }
      }
      ChangeType::Removed => {
        apply_remove_change(detailed, &change.path);
      }
    }
  }
}

fn apply_add_change(detailed: &mut DetailedSnapshot, path: &ChangePath, new_entry: &SnapshotEntry) {
  match path {
    ChangePath::FunctionDefinition { file_name, def_name } => {
      // Create file if it doesn't exist
      if !detailed.files.contains_key(file_name) {
        use calcit::detailed_snapshot::{DetailedFileInSnapshot, DetailedNsEntry};
        use std::collections::HashMap;

        // Create empty namespace entry
        let empty_ns = DetailedNsEntry {
          doc: String::new(),
          code: cirru_parser::Cirru::Leaf("".into()).into(),
        };

        detailed.files.insert(
          file_name.clone(),
          DetailedFileInSnapshot {
            ns: empty_ns,
            defs: HashMap::new(),
          },
        );
      }

      if let (Some(file), SnapshotEntry::Def(new_entry)) = (detailed.files.get_mut(file_name), new_entry) {
        file.defs.insert(def_name.clone(), new_entry.clone().into());
      }
    }
    ChangePath::NamespaceDefinition { file_name } => {
      // Create file if it doesn't exist
      if !detailed.files.contains_key(file_name) {
        use calcit::detailed_snapshot::DetailedFileInSnapshot;
        use std::collections::HashMap;

        detailed.files.insert(
          file_name.clone(),
          DetailedFileInSnapshot {
            ns: match new_entry {
              SnapshotEntry::Ns(entry) => entry.clone().into(),
              SnapshotEntry::Def(_) => DetailedNsEntry {
                doc: String::new(),
                code: cirru_parser::Cirru::Leaf("".into()).into(),
              },
            },
            defs: HashMap::new(),
          },
        );
      } else if let (Some(file), SnapshotEntry::Ns(new_entry)) = (detailed.files.get_mut(file_name), new_entry) {
        file.ns = new_entry.clone().into();
      }
    }
  }
}

fn apply_modify_change(detailed: &mut DetailedSnapshot, path: &ChangePath, new_entry: &SnapshotEntry, change_type: &ChangeType) {
  match path {
    ChangePath::FunctionDefinition { file_name, def_name } => {
      if let (Some(file), SnapshotEntry::Def(new_entry)) = (detailed.files.get_mut(file_name), new_entry) {
        if let Some(existing_def) = file.defs.get_mut(def_name) {
          // Update document part
          existing_def.doc = new_entry.doc.clone();

          // Update examples
          existing_def.examples = new_entry.examples.iter().map(|e| e.clone().into()).collect();

          // If not only document changes, also update code part
          if *change_type != ChangeType::ModifiedDoc {
            existing_def.code = new_entry.code.clone().into();
          }
        }
      }
    }
    ChangePath::NamespaceDefinition { file_name } => {
      if let (Some(file), SnapshotEntry::Ns(new_entry)) = (detailed.files.get_mut(file_name), new_entry) {
        // Update document part
        file.ns.doc = new_entry.doc.clone();

        // If not only document changes, also update code part
        if *change_type != ChangeType::ModifiedDoc {
          file.ns.code = new_entry.code.clone().into();
        }
      }
    }
  }
}

fn apply_remove_change(detailed: &mut DetailedSnapshot, path: &ChangePath) {
  match path {
    ChangePath::FunctionDefinition { file_name, def_name } => {
      if let Some(file) = detailed.files.get_mut(file_name) {
        file.defs.remove(def_name);
      }
    }
    ChangePath::NamespaceDefinition { .. } => {
      // Namespace removal not supported
    }
  }
}