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
#![allow(unused)]
use std::{
    error::Error,
    fs::{self},
    io::{Read, Write},
};

use toml_edit::{value, DocumentMut, Item, Table};

/// nest: [item.old_key] => [item.new_key.old_key]
pub fn nest(item: &mut Item, old_key: &str, new_key: &str) {
    if let Some((key, universal)) = item
        .as_table_mut()
        .expect("Cannot find the tool.uv")
        .remove_entry(old_key)
    {
        let mut sub_table = item
            .get_mut(new_key)
            .and_then(|x| x.as_table_mut())
            .unwrap_or(&mut Table::new())
            .clone();

        sub_table.insert(&key, universal);
        item[new_key] = Item::Table(sub_table);
    }
}

pub fn rename(table: &mut Table, key: &str, new_key: &str) {
    if let Some(value) = table.remove(key) {
        table[new_key] = value;
    }
}

pub fn convert(document: &mut DocumentMut) -> anyhow::Result<()> {
    if let Some(tool) = document.get_mut("tool") {
        if let Some(tool) = tool.as_table_mut() {
            rename(tool, "rye", "uv");

            nest(&mut tool["uv"], "universal", "pip");
            nest(&mut tool["uv"], "generate-hashes", "pip");

            // (lock-with-sources = false) => (no-sources = true)
            if let Some(lock_with_sources) = tool["uv"]
                .as_table_mut()
                .and_then(|x| x.remove("lock-with-sources"))
            {
                tool["uv"]["no-sources"] = value(!lock_with_sources.as_bool().unwrap());
            }

            tool["uv"].as_table_mut().and_then(|x| x.remove("virtual"));
        }
    }

    Ok(())
}