redo/
dependency.rs

1use md5::{Digest, Md5};
2use serde::{Deserialize, Serialize};
3
4/// Dependency of Target that determine if Target should be rebuilded
5#[derive(Debug, Serialize, Deserialize, Clone)]
6pub struct Dependency {
7    pub name: std::path::PathBuf,
8    pub hash: String,
9}
10
11impl Dependency {
12    /// Computes `hash` from file pointed by `name`
13    pub fn compute_hash(&self) -> String {
14        let mut hasher = Md5::new();
15        std::fs::File::open(&self.name)
16            .and_then(|mut file| std::io::copy(&mut file, &mut hasher))
17            .map(|_| base16ct::lower::encode_string(&hasher.finalize()))
18            .unwrap_or(String::new())
19    }
20
21    /// Sets `hash` as hash computed from file pointed by `name`
22    pub fn update_hash(&mut self) {
23        self.hash = self.compute_hash();
24    }
25
26    /// Dependency needs an update when cached `hash` is different then hash
27    /// of file pointed by `name`
28    pub fn needs_update(&self) -> bool {
29        self.compute_hash() != self.hash
30    }
31}