初步搭建代码框架 包括objects, tests, utils

This commit is contained in:
mrbeanc
2023-12-16 18:37:29 +08:00
parent 42e80a53cf
commit e5141c159b
12 changed files with 78 additions and 0 deletions

6
.gitignore vendored
View File

@@ -17,3 +17,9 @@ Cargo.lock
# Added by cargo
/target
## IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

View File

@@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sha1 = "0.10.6"
hex = "0.4.3"

3
src/lib.rs Normal file
View File

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

View File

@@ -1,3 +1,5 @@
use std::collections::HashMap;
type Index = HashMap<String, bool>;
fn main() {
println!("Hello, world!");
}

6
src/objects/blob.rs Normal file
View File

@@ -0,0 +1,6 @@
use crate::objects::object::Hash;
#[derive(Debug, Clone)]
pub struct Blob {
hash: Hash,
data: String,
}

0
src/objects/commit.rs Normal file
View File

4
src/objects/mod.rs Normal file
View File

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

1
src/objects/object.rs Normal file
View File

@@ -0,0 +1 @@
pub type Hash = String;

0
src/objects/tree.rs Normal file
View File

1
src/utils/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod util;

11
src/utils/util.rs Normal file
View File

@@ -0,0 +1,11 @@
use sha1::{
Digest,
Sha1
};
pub fn calc_hash(data: &String) -> String {
let mut hasher = Sha1::new();
hasher.update(data);
let hash = hasher.finalize();
hex::encode(hash)
}

42
tests/test.rs Normal file
View File

@@ -0,0 +1,42 @@
use sha1::{Sha1, Digest};
use std::fs::File;
use std::io::{Write, BufReader, BufRead, Error};
#[test]
fn test_hash() {
let mut hasher = Sha1::new();
hasher.update(String::from("hello world"));
let result = format!("{:x}", hasher.finalize());
println!("{}", result);
println!("{}", mini_git::utils::util::calc_hash(&String::from("hello world")));
}
#[test]
fn test_write() -> Result<(), Error> {
let path = "lines.txt";
//create会截断文件
let mut output = File::create(path)?; // ? 用于传播错误
write!(output, "Rust\nWrite\nRead4")?;
Ok(())
}
#[test]
fn test_read() -> Result<(), Error> {
let path = "lines.txt";
let input = File::open(path)?;
let buffered = BufReader::new(input);
for line in buffered.lines() {
println!("{}", line?);
}
Ok(())
}
#[test]
fn test_string() {
let mut s = String::from("Hello");
s.push_str(", world!");
s += "2";
s.push('!');
println!("{}", s);
}