更名文件夹objects->models,并增添index.rs结构

This commit is contained in:
mrbeanc
2023-12-18 13:42:29 +08:00
parent f55ab0e7a6
commit af5e36c24c
10 changed files with 53 additions and 5 deletions

View File

@@ -9,3 +9,4 @@ edition = "2021"
sha1 = "0.10.6"
hex = "0.4.3"
clap = { version = "4.4.11", features = ["derive"] }
chrono = "0.4.31"

View File

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

View File

@@ -1,5 +1,3 @@
use std::collections::HashMap;
type Index = HashMap<String, bool>;
mod cli;
fn main() {
cli::handle_command();

View File

@@ -1,4 +1,4 @@
use crate::objects::object::Hash;
use crate::models::object::Hash;
/*Blob
* Blob是git中最基本的对象使hash作为标识符
*/

36
src/models/index.rs Normal file
View File

@@ -0,0 +1,36 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::SystemTime;
use crate::models::object::Hash;
// 文件元数据结构
#[derive(Debug, Clone)]
struct FileMetaData {
hash: Hash, // SHA-1 哈希值
size: u64, // 文件大小
created_time: SystemTime, // 创建时间
modified_time: SystemTime, // 修改时间
mode: String, // 文件模式
}
// 索引数据结构
#[derive(Debug, Default)]
struct Index {
entries: HashMap<PathBuf, FileMetaData>,
}
#[cfg(test)]
mod tests {
use std::fs;
use crate::utils::util;
use super::*;
#[test]
fn test_index() {
// 示例:获取文件的元数据
let metadata = fs::metadata("lines.txt").unwrap();
println!("{:?}", util::format_time(&metadata.created().unwrap()));
println!("{:?}", util::format_time(&metadata.modified().unwrap()));
println!("{:?}", metadata.len());
}
}

View File

@@ -1,4 +1,5 @@
pub mod commit;
pub mod blob;
pub mod tree;
mod object;
pub mod object;
pub mod index;

View File

@@ -34,6 +34,11 @@ pub fn get_storage_path() -> Result<String, std::io::Error> {
}
}
pub fn format_time(time: &std::time::SystemTime) -> String {
let datetime: chrono::DateTime<chrono::Utc> = time.clone().into();
datetime.format("%Y-%m-%d %H:%M:%S.%3f").to_string()
}
#[cfg(test)]
mod tests {
use super::*;
@@ -49,4 +54,11 @@ mod tests {
},
}
}
#[test]
fn test_format_time() {
let time = std::time::SystemTime::now();
let formatted_time = format_time(&time);
println!("{}", formatted_time);
}
}