simple store

This commit is contained in:
HouXiaoxuan
2023-12-18 01:07:03 +08:00
parent a0059d1c9c
commit 3215eab199
2 changed files with 59 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
// 不使用lib.rs的话就无法在tests里引用到src中的模块
pub mod objects;
pub mod utils;
pub mod commands;
pub mod commands;
mod store;

57
src/store.rs Normal file
View File

@@ -0,0 +1,57 @@
use super::utils::util;
pub struct Store {
store_path: String,
}
impl Store {
pub fn new() -> Store {
if !util::storage_exist() {
panic!("不是合法的mit仓库");
}
let store_path = util::get_storage_path().unwrap();
Store {
store_path: store_path,
}
}
pub fn load(&self, hash: &String) -> String {
/* 读取文件内容 */
let mut path = self.store_path.clone();
path.push_str("/objects/");
path.push_str(hash);
match std::fs::read_to_string(path) {
Ok(content) => content,
Err(_) => panic!("储存库疑似损坏,无法读取文件"),
}
}
pub fn save(&self, content: &String) -> String {
/* 保存文件内容 */
let hash = util::calc_hash(content);
let mut path = self.store_path.clone();
path.push_str("/objects/");
path.push_str(&hash);
match std::fs::write(path, content) {
Ok(_) => hash,
Err(_) => panic!("储存库疑似损坏,无法写入文件"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let _ = Store::new();
}
#[test]
fn test_save_and_load() {
let store = Store::new();
let content = "hello world".to_string();
let hash = store.save(&content);
let content2 = store.load(&hash);
assert_eq!(content, content2, "内容不一致");
}
}