Skip to content

Commit

Permalink
#4 working on state lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Mar 13, 2024
1 parent b7b3b10 commit dcadae6
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/backend/lock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::{error, fs, result};

use serde::{Deserialize, Serialize};

use super::context::{AbsoltuePaths, DynamicAbsolutePaths};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LockFile {
pub struct StateLockFile {
pub dependencies: Vec<Dependency>,
}

Expand All @@ -15,9 +19,26 @@ pub struct Dependency {
pub checksum: String,
}

/**
* Get '.espresso'
*/
pub fn get_lock_file_from_fs() -> LockFile {
/// Get the `.espresso/state.lock` file from the filesystem.
///
/// # Arguments
/// * `ap`: Reference to an `AbsolutePaths` struct
///
/// # Returns
/// Returns a `LockFile` struct on success, propagating any errors in the process
pub fn get_state_lockfile_from_fs(ap: &AbsoltuePaths) -> result::Result<StateLockFile, Box<dyn error::Error>> {
let contents = fs::read_to_string(&ap.state_lockfile)?;
let x: StateLockFile = toml::from_str(&contents)?;
Ok(x)
}

/// Initialize a new state lockfile
pub fn initialize_state_lockfile(ap: &AbsoltuePaths) -> result::Result<(), Box<dyn error::Error>> {
let base = StateLockFile {
dependencies: vec![]
};

let toml_string = toml::to_string(&base)?;
fs::write(ap.state_lockfile.clone(), toml_string)?;
Ok(())
}

0 comments on commit dcadae6

Please sign in to comment.