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
use std::collections::BTreeMap;

use color_eyre::eyre::{Result, WrapErr};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lockfile {
    #[serde(default)]
    pub plugins: BTreeMap<String, LockedPlugin>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockedPlugin {
    pub src: String,
    pub path: String,
    pub rev: String,
}

pub fn load() -> Result<Lockfile> {
    let path = crate::paths::lockfile_path()?;
    let contents = fs_err::read_to_string(&path)?;
    let lockfile: Lockfile = serde_yml::from_str(&contents)
        .wrap_err_with(|| format!("failed to parse lockfile: {}", path.display()))?;
    Ok(lockfile)
}

pub fn save(lockfile: &Lockfile) -> Result<()> {
    let path = crate::paths::lockfile_path()?;
    let contents = serde_yml::to_string(lockfile).wrap_err("failed to serialize lockfile")?;
    fs_err::write(&path, contents)?;
    Ok(())
}