mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-02-03 02:04:09 +08:00
初步搭建代码框架 包括objects, tests, utils
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -17,3 +17,9 @@ Cargo.lock
|
||||
# Added by cargo
|
||||
|
||||
/target
|
||||
|
||||
## IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
@@ -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
3
src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
// 不使用lib.rs的话,就无法在tests里引用到src中的模块
|
||||
pub mod objects;
|
||||
pub mod utils;
|
||||
@@ -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
6
src/objects/blob.rs
Normal 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
0
src/objects/commit.rs
Normal file
4
src/objects/mod.rs
Normal file
4
src/objects/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod commit;
|
||||
pub mod blob;
|
||||
pub mod tree;
|
||||
mod object;
|
||||
1
src/objects/object.rs
Normal file
1
src/objects/object.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub type Hash = String;
|
||||
0
src/objects/tree.rs
Normal file
0
src/objects/tree.rs
Normal file
1
src/utils/mod.rs
Normal file
1
src/utils/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod util;
|
||||
11
src/utils/util.rs
Normal file
11
src/utils/util.rs
Normal 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
42
tests/test.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user