diff --git a/src/backend/lock.rs b/src/backend/lock.rs index 45b2f80..28e5648 100644 --- a/src/backend/lock.rs +++ b/src/backend/lock.rs @@ -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, } @@ -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> { + 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> { + let base = StateLockFile { + dependencies: vec![] + }; + let toml_string = toml::to_string(&base)?; + fs::write(ap.state_lockfile.clone(), toml_string)?; + Ok(()) } \ No newline at end of file