ragit 0.4.5

git-like rag pipeline
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
use super::Index;
use crate::error::Error;
use crate::index::{INDEX_DIR_NAME, INDEX_FILE_NAME};
use crate::prompts::PROMPTS;
use flate2::read::GzDecoder;
use lazy_static::lazy_static;
use ragit_fs::{
    WriteMode,
    copy_dir,
    copy_file,
    create_dir,
    create_dir_all,
    exists,
    extension,
    file_name,
    file_size,
    join,
    join3,
    join4,
    read_bytes,
    read_dir,
    read_string,
    remove_dir_all,
    remove_file,
    rename,
    set_extension,
    try_create_dir,
    write_bytes,
    write_string,
};
use ragit_pdl::JsonType;
use regex::Regex;
use serde_json::{Number, Value};
use std::{cmp, fmt};
use std::collections::hash_map::{Entry, HashMap};
use std::io::Read;
use std::str::FromStr;

pub type Path = String;

lazy_static! {
    static ref FILE_UID_RE: Regex = Regex::new(r"^(\d{8})_([0-9a-f]{48})[0-9a-f]{16}$").unwrap();
    static ref UID_RE: Regex = Regex::new(r"[0-9a-z]{64}").unwrap();
    static ref VERSION_RE: Regex = Regex::new(r"(\d{0,4})\.(\d{0,4})\.(\d{0,4})(?:-([a-zA-Z0-9]+))?").unwrap();
}

impl Index {
    /// It reads version info at `root/.ragit/index.json`. Make sure that the
    /// file exists and `index.json` has `"ragit_version" field.`
    pub fn check_ragit_version(root_dir: &Path) -> Result<VersionInfo, Error> {
        let index_at = join3(
            root_dir,
            INDEX_DIR_NAME,
            INDEX_FILE_NAME,
        )?;
        let j = read_string(&index_at)?;

        match serde_json::from_str::<Value>(&j)? {
            Value::Object(obj) => match obj.get("ragit_version") {
                Some(v) => match v.as_str() {
                    Some(v) => Ok(v.parse::<VersionInfo>()?),
                    None => Err(Error::JsonTypeError {
                        expected: JsonType::String,
                        got: v.into(),
                    }),
                },
                None => Err(Error::BrokenIndex(String::from("`ragit_version` is not found in `index.json`."))),
            },
            v => Err(Error::JsonTypeError {
                expected: JsonType::Object,
                got: (&v).into(),
            }),
        }
    }

    /// You can auto-migrate knowledge-bases built by older versions of
    /// ragit. It doesn't modify the contents of the knowledge-bases, but
    /// may change structures or formats of the files.
    ///
    /// If it returns `Ok(())`, you can access the migrated knowledge-base
    /// by `Index::load(root_dir)`.
    ///
    /// The result of this function doesn't always mean whether the knowledge-base
    /// is corrupted or not. For example, if the original knowledge-base is corrupted,
    /// it may successfully migrate, but still is corrupted.
    /// If the original knowledge-base perfect and there's no compatibility issue but
    /// the client doesn't know that, this function may fail but there's no problem
    /// using the knowledge-base.
    ///
    /// It assumes that `recover` is run after `migrate`. It doesn't do any operation
    /// that can be handled by `recover`.
    pub fn migrate(root_dir: &Path) -> Result<Option<(VersionInfo, VersionInfo)>, Error> {
        let base_version = Index::check_ragit_version(root_dir)?;
        let client_version_str = crate::VERSION.to_string();
        let client_version = client_version_str.parse::<VersionInfo>()?;

        // It's still a problem.
        // Even though the client is outdated, compatibility issue is very unlikely.
        // But the client can never tell that.
        //
        // The easiest fix is to implement migration for all the versions, and tell users to always keep their ragit version up-to-date.
        // But those are not always possible.
        if base_version > client_version {
            Err(Error::CannotMigrate {
                from: base_version.to_string(),
                to: client_version_str,
            })
        }

        else if base_version == client_version {
            Ok(None)
        }

        else {
            if base_version.major == 0 && base_version.minor < 2 {
                let index_dir = join(root_dir, INDEX_DIR_NAME)?;
                let tmp_dir = create_tmp_dir()?;
                let tmp_index_dir = join(&tmp_dir, INDEX_DIR_NAME)?;
                copy_dir(&index_dir, &tmp_index_dir)?;

                match migrate_0_1_1_to_0_2_x(&tmp_dir) {
                    Ok(()) => {
                        remove_dir_all(&index_dir)?;
                        rename(&tmp_index_dir, &index_dir)?;
                        remove_dir_all(&tmp_dir)?;
                        Ok(Some((base_version, client_version)))
                    },
                    Err(e) => {
                        remove_dir_all(&tmp_dir)?;
                        Err(e)
                    },
                }
            }

            // as of v0.2.1, there's no compatibility issue in v0.2.x
            else {
                let index_dir = join(root_dir, INDEX_DIR_NAME)?;
                let tmp_dir = create_tmp_dir()?;
                let tmp_index_dir = join(&tmp_dir, INDEX_DIR_NAME)?;
                copy_dir(&index_dir, &tmp_index_dir)?;

                match update_version_string(&tmp_dir, crate::VERSION) {
                    Ok(()) => {
                        remove_dir_all(&index_dir)?;
                        rename(&tmp_index_dir, &index_dir)?;
                        remove_dir_all(&tmp_dir)?;
                        Ok(Some((base_version, client_version)))
                    },
                    Err(e) => {
                        remove_dir_all(&tmp_dir)?;
                        Err(e)
                    },
                }
            }
        }
    }
}

fn create_tmp_dir() -> Result<Path, Error> {
    let mut dir_name = String::new();

    for i in 0..1000 {
        dir_name = format!("__tmp_{i:03}");

        if !exists(&dir_name) {
            break;
        }
    }

    create_dir_all(&dir_name)?;
    Ok(dir_name)
}

fn update_version_string(root_dir: &Path, new_version: &str) -> Result<(), Error> {
    let index_at = join3(
        root_dir,
        ".ragit",
        "index.json",
    )?;
    let j = read_string(&index_at)?;
    let mut j = serde_json::from_str::<Value>(&j)?;

    match &mut j {
        Value::Object(index) => {
            index.insert(String::from("ragit_version"), new_version.into());
        },
        _ => {
            return Err(Error::JsonTypeError {
                expected: JsonType::Object,
                got: (&j).into(),
            });
        },
    }

    write_bytes(
        &index_at,
        &serde_json::to_vec_pretty(&j)?,
        WriteMode::Atomic,
    )?;

    Ok(())
}

fn migrate_0_1_1_to_0_2_x(root_dir: &Path) -> Result<(), Error> {
    let index_at = join3(
        root_dir,
        ".ragit",
        "index.json",
    )?;
    let j = read_string(&index_at)?;
    let mut j = serde_json::from_str::<Value>(&j)?;
    let file_uid_re = &FILE_UID_RE;
    let uid_re = &UID_RE;
    let mut processed_files_cache;
    let mut image_uid_map = HashMap::new();

    match &mut j {
        Value::Object(index) => {
            index.insert(String::from("ragit_version"), "0.2.0".into());
            index.insert(String::from("ii_status"), Value::Object(vec![(String::from("type"), String::from("None").into())].into_iter().collect()));

            if index.remove("chunk_files").is_none() {
                return Err(Error::BrokenIndex(String::from("`index.json` is missing `chunk_files` field.")));
            }

            match index.get_mut("processed_files") {
                Some(Value::Object(processed_files)) => {
                    processed_files_cache = HashMap::with_capacity(processed_files.len());

                    for (file_name, file_uid) in processed_files.clone().iter() {
                        match file_uid.as_str() {
                            Some(file_uid) => match file_uid_re.captures(file_uid) {
                                Some(file_uid_cap) => {
                                    let file_uid = format!("{}00000003{:08x}", &file_uid_cap[2], file_uid_cap[1].parse::<usize>().unwrap());
                                    processed_files.insert(
                                        file_name.to_string(),
                                        vec![
                                            (String::from("high"), Value::Number(Number::from_u128(u128::from_str_radix(file_uid.get(0..32).unwrap(), 16).unwrap()).unwrap())),
                                            (String::from("low"), Value::Number(Number::from_u128(u128::from_str_radix(file_uid.get(32..).unwrap(), 16).unwrap()).unwrap())),
                                        ].into_iter().collect(),
                                    );
                                    processed_files_cache.insert(file_name.to_string(), file_uid);
                                },
                                None => {
                                    return Err(Error::BrokenIndex(format!("`index.json` has a corrupted file uid: `{file_uid}`.")));
                                },
                            },
                            None => {
                                return Err(Error::JsonTypeError {
                                    expected: JsonType::String,
                                    got: file_uid.into(),
                                });
                            },
                        }
                    }
                },
                Some(v) => {
                    return Err(Error::JsonTypeError {
                        expected: JsonType::Object,
                        got: (&*v).into(),
                    });
                },
                None => {
                    return Err(Error::BrokenIndex(String::from("`index.json` is missing `processed_files` field.")));
                },
            }
        },
        _ => {
            return Err(Error::JsonTypeError {
                expected: JsonType::Object,
                got: (&j).into(),
            });
        },
    }

    write_bytes(
        &index_at,
        &serde_json::to_vec_pretty(&j)?,
        WriteMode::Atomic,
    )?;
    remove_dir_all(
        &join3(
            root_dir,
            ".ragit",
            "chunk_index",
        )?,
    )?;

    let image_dir = join3(
        root_dir,
        ".ragit",
        "images",
    )?;
    let mut image_size_map = HashMap::new();

    if !exists(&image_dir) {
        create_dir(&image_dir)?;
    }

    for image_file in read_dir(&image_dir, false)? {
        let curr_ext = extension(&image_file)?.unwrap_or(String::new());

        if curr_ext != "png" && curr_ext != "json" {
            continue;
        }

        let uid = file_name(&image_file)?;
        let image_size = match image_size_map.get(&uid) {
            Some(n) => *n,
            None => {
                let s = file_size(&set_extension(&image_file, "png")?)?;
                image_size_map.insert(uid.to_string(), s);
                s
            },
        };
        let new_uid = update_uid_schema(&uid, 2, image_size);
        image_uid_map.insert(uid.clone(), new_uid.clone());

        let image_at = join(
            &image_dir,
            &new_uid.get(0..2).unwrap(),
        )?;

        if !exists(&image_at) {
            try_create_dir(&image_at)?;
        }

        copy_file(
            &image_file,
            &join(
                &image_at,
                &set_extension(
                    &new_uid.get(2..).unwrap(),
                    &curr_ext,
                )?,
            )?,
        )?;
        remove_file(&image_file)?;
    }

    let tmp_chunk_dir = join3(
        root_dir,
        ".ragit",
        "chunks-tmp",
    )?;
    let mut file_to_chunks_map: HashMap<String, Vec<(String, usize)>> = HashMap::new();

    if !exists(&tmp_chunk_dir) {
        create_dir(&tmp_chunk_dir)?;
    }

    for chunk_file in read_dir(&join3(
        root_dir,
        ".ragit",
        "chunks",
    )?, false)? {
        if extension(&chunk_file)?.unwrap_or(String::new()) != "chunks" {
            continue;
        }

        let chunks = load_chunks_0_1_1(&chunk_file)?;

        match chunks {
            Value::Array(mut chunks) => {
                for chunk in chunks.iter_mut() {
                    match chunk {
                        Value::Object(obj) => {
                            match obj.get("images") {
                                Some(Value::Array(images)) if images.len() > 0 => {
                                    let mut new_images: Vec<Value> = Vec::with_capacity(images.len());

                                    for image in images.iter() {
                                        match image {
                                            Value::String(uid) => {
                                                let new_uid = match image_uid_map.get(uid) {
                                                    Some(new_uid) => new_uid.to_string(),
                                                    _ => {
                                                        return Err(Error::BrokenIndex(format!(
                                                            "chunk `{}` is pointing to an image `{}`, which does not exist",
                                                            obj.get("uid").map(|s| s.as_str().map(|s| s.to_string()).unwrap_or(String::from("Unknown"))).unwrap_or(String::from("Unknown")),
                                                            uid,
                                                        )));
                                                    },
                                                };

                                                new_images.push(vec![
                                                    (String::from("high"), Value::Number(Number::from_u128(u128::from_str_radix(new_uid.get(0..32).unwrap(), 16).unwrap()).unwrap())),
                                                    (String::from("low"), Value::Number(Number::from_u128(u128::from_str_radix(new_uid.get(32..).unwrap(), 16).unwrap()).unwrap())),
                                                ].into_iter().collect());
                                            },
                                            v => {
                                                return Err(Error::JsonTypeError {
                                                    expected: JsonType::String,
                                                    got: v.into(),
                                                });
                                            },
                                        }
                                    }

                                    obj.insert(String::from("images"), new_images.into());
                                },
                                _ => {},
                            }

                            let file_name = match obj.get("file") {
                                Some(file_name) => match file_name.as_str() {
                                    Some(file_name) => file_name.to_string(),
                                    None => {
                                        return Err(Error::JsonTypeError {
                                            expected: JsonType::String,
                                            got: file_name.into(),
                                        });
                                    },
                                },
                                None => {
                                    return Err(Error::BrokenIndex(String::from("A corrupted chunk.")));
                                },
                            };
                            let file_index = match obj.get("index") {
                                Some(index) => match index.as_u64() {
                                    Some(index) => index as usize,
                                    None => {
                                        return Err(Error::JsonTypeError {
                                            expected: JsonType::Usize,
                                            got: index.into(),
                                        });
                                    },
                                },
                                None => {
                                    return Err(Error::BrokenIndex(String::from("A corrupted chunk.")));
                                },
                            };
                            obj.remove("file");
                            obj.remove("index");
                            obj.insert(String::from("source"), Value::Object(vec![
                                (String::from("type"), String::from("File").into()),
                                (String::from("path"), file_name.clone().into()),

                                // ragit 0.1.1 uses 1-base index
                                (String::from("index"), (file_index - 1).into()),
                            ].into_iter().collect()));
                            obj.insert(String::from("searchable"), true.into());
                            let data_len = match obj.get("data") {
                                Some(Value::String(d)) => d.len(),
                                Some(d) => {
                                    return Err(Error::JsonTypeError {
                                        expected: JsonType::String,
                                        got: d.into(),
                                    });
                                },
                                None => {
                                    return Err(Error::BrokenIndex(format!(
                                        "chunk `{}` does not have `data` field.",
                                        obj.get("uid").map(|s| s.as_str().map(|s| s.to_string()).unwrap_or(String::from("Unknown"))).unwrap_or(String::from("Unknown")),
                                    )));
                                },
                            };

                            match obj.get("uid") {
                                Some(uid) => match uid.as_str() {
                                    Some(uid) if uid_re.is_match(uid) => {
                                        let uid = uid.to_string();
                                        let new_uid = update_uid_schema(&uid, 1, data_len as u64);
                                        obj.insert(
                                            String::from("uid"),
                                            vec![
                                                (String::from("high"), Value::Number(Number::from_u128(u128::from_str_radix(new_uid.get(0..32).unwrap(), 16).unwrap()).unwrap())),
                                                (String::from("low"), Value::Number(Number::from_u128(u128::from_str_radix(new_uid.get(32..).unwrap(), 16).unwrap()).unwrap())),
                                            ].into_iter().collect(),
                                        );
                                        let chunk_at = join(
                                            &tmp_chunk_dir,
                                            new_uid.get(0..2).unwrap(),
                                        )?;

                                        if !exists(&chunk_at) {
                                            try_create_dir(&chunk_at)?;
                                        }

                                        match file_to_chunks_map.entry(file_name) {
                                            Entry::Occupied(mut uids) => {
                                                uids.get_mut().push((new_uid.to_string(), file_index));
                                            },
                                            Entry::Vacant(e) => {
                                                e.insert(vec![(new_uid.to_string(), file_index)]);
                                            },
                                        }

                                        write_bytes(
                                            &join(&chunk_at, &format!("{}.chunk", new_uid.get(2..).unwrap()))?,
                                            &vec![
                                                vec![b'\n'],  // chunk prefix for an un-compressed chunk
                                                serde_json::to_vec_pretty(&chunk)?,
                                            ].concat(),
                                            WriteMode::AlwaysCreate,
                                        )?;
                                    },
                                    Some(uid) => {
                                        return Err(Error::BrokenIndex(format!("There's a malformed uid: `{uid}`.")));
                                    },
                                    None => {
                                        return Err(Error::JsonTypeError {
                                            expected: JsonType::String,
                                            got: uid.into(),
                                        });
                                    },
                                },
                                None => {
                                    return Err(Error::BrokenIndex(String::from("There's a chunk without uid.")));
                                },
                            }
                        },
                        _ => {
                            return Err(Error::JsonTypeError {
                                expected: JsonType::Object,
                                got: (&*chunk).into(),
                            });
                        },
                    }
                }
            },
            _ => {
                return Err(Error::JsonTypeError {
                    expected: JsonType::Array,
                    got: (&chunks).into(),
                });
            },
        }
    }

    remove_dir_all(&join3(
        root_dir,
        ".ragit",
        "chunks",
    )?)?;
    rename(
        &join3(
            root_dir,
            ".ragit",
            "chunks-tmp",
        )?, &join3(
            root_dir,
            ".ragit",
            "chunks",
        )?,
    )?;

    let file_index_at = join3(
        root_dir,
        ".ragit",
        "files",
    )?;

    if !exists(&file_index_at) {
        create_dir(&file_index_at)?;
    }

    for (file_name, mut chunks) in file_to_chunks_map.into_iter() {
        chunks.sort_by_key(|(_, index)| *index);
        let file_uid = match processed_files_cache.get(&file_name) {
            Some(file_uid) => file_uid.to_string(),
            None => {
                return Err(Error::BrokenIndex(format!("File hash not found: `{file_name}`")));
            },
        };
        let index_path = join(&file_index_at, file_uid.get(0..2).unwrap())?;

        if !exists(&index_path) {
            try_create_dir(&index_path)?;
        }

        write_string(
            &join(&index_path, file_uid.get(2..).unwrap())?,
            &chunks.into_iter().map(|(uid, _)| uid).collect::<Vec<_>>().join("\n"),
            WriteMode::AlwaysCreate,
        )?;
    }

    update_configs(
        &root_dir,
        vec![
            ConfigUpdate::add("query", "enable_ii", true),
            ConfigUpdate::remove("build", "chunks_per_json"),
            ConfigUpdate::update_if("api", "model", "llama3.1-70b-groq", "llama3.3-70b-groq"),
        ],
    )?;

    let prompt_path = join4(
        root_dir,
        ".ragit",
        "prompts",
        "summarize_chunks.pdl",
    )?;
    write_string(
        &prompt_path,
        PROMPTS.get("summarize_chunks").unwrap(),
        WriteMode::AlwaysCreate,
    )?;

    Ok(())
}

fn load_chunks_0_1_1(path: &str) -> Result<Value, Error> {
    let content = read_bytes(path)?;

    match content.get(0) {
        Some(b) if *b == b'c' => {
            let mut decompressed = vec![];
            let mut gz = GzDecoder::new(&content[1..]);
            gz.read_to_end(&mut decompressed)?;

            Ok(serde_json::from_slice::<Value>(&decompressed)?)
        },
        Some(b) if *b == b'\n' => Ok(serde_json::from_slice::<Value>(&content[1..])?),
        Some(b) => Err(Error::BrokenIndex(format!("Unknown chunk prefix: {}", *b as char))),
        None => Err(Error::BrokenIndex(format!("An empty chunk file."))),
    }
}

pub fn get_compatibility_warning(
    index_version: &str,
    ragit_version: &str,
) -> Option<String> {
    let index_version = match index_version.parse::<VersionInfo>() {
        Ok(v) => v,
        Err(_) => {
            return Some(format!("Unable to parse the version of the knowledge-base. It's {index_version}"));
        },
    };
    let ragit_version = match ragit_version.parse::<VersionInfo>() {
        Ok(v) => v,
        Err(_) => {
            return Some(format!("Unable to parse the current version of ragit_version. It's {ragit_version}"));
        },
    };

    if ragit_version < index_version {
        Some(format!("Ragit's version is older than the knowledge-base. Please update ragit_version."))
    }

    else if (ragit_version.major > 0 || ragit_version.minor > 1) && index_version.major == 0 && index_version.minor == 1 {
        Some(format!("The knowledge-base is outdated. Please run `rag migrate`."))
    }

    else {
        None
    }
}

// This is an internal representation of versions. I don't think it's the best
// way to manage versions. There must be better ways and I need more research on those.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct VersionInfo {
    major: u16,
    minor: u16,
    patch: u16,
    dev: bool,
}

impl FromStr for VersionInfo {
    type Err = Error;

    fn from_str(s: &str) -> Result<VersionInfo, Error> {
        let version_re = &VERSION_RE;

        match version_re.captures(s) {
            Some(cap) => {
                if let Some(m) = cap.get(4) {
                    if m.as_str() != "dev" {
                        return Err(Error::InvalidVersionString(s.to_string()));
                    }
                }

                Ok(VersionInfo {
                    major: cap[1].parse::<u16>().unwrap(),
                    minor: cap[2].parse::<u16>().unwrap(),
                    patch: cap[3].parse::<u16>().unwrap(),
                    dev: cap.get(4).map(|c| c.as_str().to_string()).unwrap_or(String::new()) == "dev",
                })
            },
            // TODO: handle custom version numbers
            None => Err(Error::InvalidVersionString(s.to_string())),
        }
    }
}

impl fmt::Display for VersionInfo {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(
            fmt,
            "{}.{}.{}{}",
            self.major,
            self.minor,
            self.patch,
            if self.dev { "-dev" } else { "" },
        )
    }
}

impl PartialOrd for VersionInfo {
    fn partial_cmp(&self, other: &VersionInfo) -> Option<cmp::Ordering> {
        if self == other {
            Some(cmp::Ordering::Equal)
        }

        else {
            (
                self.major,
                self.minor,
                self.patch,
                !(self.dev as u16),  // 0.2.2-dev is lower than 0.2.2
            ).partial_cmp(&(
                other.major,
                other.minor,
                other.patch,
                !(other.dev as u16),
            ))
        }
    }
}

impl Ord for VersionInfo {
    fn cmp(&self, other: &VersionInfo) -> cmp::Ordering {
        self.partial_cmp(other).unwrap()
    }
}

enum ConfigUpdate {
    Add {
        file: String,
        key: String,
        value: Value,
    },
    Remove {
        file: String,
        key: String,
    },
    UpdateIf {
        file: String,
        key: String,
        pre: Value,
        post: Value,
    },
}

impl ConfigUpdate {
    pub fn add<T: Into<Value>>(file: &str, key: &str, value: T) -> Self {
        ConfigUpdate::Add {
            file: file.to_string(),
            key: key.to_string(),
            value: value.into(),
        }
    }

    pub fn remove(file: &str, key: &str) -> Self {
        ConfigUpdate::Remove {
            file: file.to_string(),
            key: key.to_string(),
        }
    }

    pub fn update_if<T: Into<Value>, U: Into<Value>>(file: &str, key: &str, pre: T, post: U) -> Self {
        ConfigUpdate::UpdateIf {
            file: file.to_string(),
            key: key.to_string(),
            pre: pre.into(),
            post: post.into(),
        }
    }

    pub fn get_file(&self) -> String {
        match self {
            ConfigUpdate::Add { file, .. } => file.to_string(),
            ConfigUpdate::Remove { file, .. } => file.to_string(),
            ConfigUpdate::UpdateIf { file, .. } => file.to_string(),
        }
    }
}

fn update_configs(root_dir: &str, updates: Vec<ConfigUpdate>) -> Result<(), Error> {
    let configs_at = join3(
        root_dir,
        ".ragit",
        "configs",
    )?;

    for update in updates.into_iter() {
        let json_at = join(
            &configs_at,
            &set_extension(
                &update.get_file(),
                "json",
            )?,
        )?;
        let j = read_string(&json_at)?;
        let mut v = serde_json::from_str::<Value>(&j)?;

        match &mut v {
            Value::Object(obj) => match update {
                ConfigUpdate::Add { key, value, .. } => {
                    obj.insert(key, value);
                },
                ConfigUpdate::Remove { key, .. } => {
                    obj.remove(&key);
                },
                ConfigUpdate::UpdateIf { key, pre, post, .. } => {
                    match obj.get(&key) {
                        Some(v) if v == &pre => {
                            obj.insert(key, post);
                        },
                        _ => {},
                    }
                },
            },
            _ => {
                return Err(Error::JsonTypeError {
                    expected: JsonType::Object,
                    got: (&v).into(),
                });
            },
        }

        write_bytes(
            &json_at,
            &serde_json::to_vec_pretty(&v)?,
            WriteMode::Atomic,
        )?;
    }

    Ok(())
}

fn update_uid_schema(old_uid: &str, uid_type: u32, data_len: u64) -> String {
    let prefix = old_uid.get(0..48).unwrap();
    let suffix = format!("{uid_type:08x}{data_len:08x}");
    format!("{prefix}{suffix}")
}