mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-04-03 18:57:41 +08:00
更名文件夹objects->models,并增添index.rs结构
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// 不使用lib.rs的话,就无法在tests里引用到src中的模块
|
||||
pub mod objects;
|
||||
pub mod models;
|
||||
pub mod utils;
|
||||
pub mod commands;
|
||||
mod store;
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::collections::HashMap;
|
||||
type Index = HashMap<String, bool>;
|
||||
mod cli;
|
||||
fn main() {
|
||||
cli::handle_command();
|
||||
|
||||
@@ -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
36
src/models/index.rs
Normal 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());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod commit;
|
||||
pub mod blob;
|
||||
pub mod tree;
|
||||
mod object;
|
||||
pub mod object;
|
||||
pub mod index;
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user