From 02a27b4327ebce23031bd1dea09a3bf0faae86bb Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Wed, 20 Dec 2023 21:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0Index::load()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/index.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/models/index.rs b/src/models/index.rs index 7d4c264..075d60e 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -114,7 +114,18 @@ impl Index { } fn load(&mut self) { - + let path = Index::get_path(); + if path.exists() { + let json = fs::read_to_string(path).expect("无法读取index"); + let relative_index: HashMap = serde_json::from_str(&json).expect("无法解析index"); + self.entries = relative_index.into_iter() + .map(|(path, value)| { + let abs_path = self.working_dir.join(path); + println!("{}", abs_path.display()); + (abs_path, value) + }) + .collect(); + } } pub fn get_path() -> PathBuf { @@ -165,6 +176,13 @@ mod tests { println!("{:?}", metadata.len()); } + #[test] + fn test_load() { + util::setup_test_with_mit(); + let index = Index::new(); + println!("{:?}", index); + } + #[test] fn test_save() { util::setup_test_with_clean_mit();