将序列化中[to_string]改为[to_string_pretty]进行格式化,改善显示效果

This commit is contained in:
mrbeanc
2023-12-20 13:13:12 +08:00
parent 642f8b2cff
commit 2255cdaf09
5 changed files with 12 additions and 12 deletions

View File

@@ -33,6 +33,7 @@ impl Blob {
}
}
/// 写入文件;优化:文件已存在时不做操作
pub fn save(&self) {
let s = Store::new();
if !s.contains(&self.hash) {

View File

@@ -44,7 +44,7 @@ impl Commit {
pub fn save(&mut self) -> String {
// unimplemented!()
let s = store::Store::new();
let commit_data = serde_json::to_string(&self).unwrap();
let commit_data = serde_json::to_string_pretty(&self).unwrap();
let hash = s.save(&commit_data);
self.hash = hash.clone();
hash

View File

@@ -1,6 +1,6 @@
use crate::models::blob::Blob;
use crate::models::object::Hash;
use crate::utils::util::{get_file_mode, get_working_dir};
use crate::utils::util;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
@@ -25,7 +25,7 @@ impl FileMetaData {
size: meta.len(),
created_time: meta.created().unwrap(),
modified_time: meta.modified().unwrap(),
mode: get_file_mode(file),
mode: util::get_file_mode(file),
}
}
}
@@ -42,7 +42,7 @@ impl Index {
pub(crate) fn new() -> Index {
let mut index = Index {
entries: HashMap::new(),
working_dir: get_working_dir().unwrap(),
working_dir: util::get_working_dir().unwrap(),
};
index.load();
return index;
@@ -112,12 +112,13 @@ impl Index {
self.entries.insert(path, data);
}
fn load(&mut self) {}
fn load(&mut self) {
}
/// 二进制序列化
pub fn save(&self) {
//要先转化为相对路径
let ser = serde_json::to_string(&self).unwrap();
let ser = serde_json::to_string_pretty(&self).unwrap();
println!("{}", ser);
}
@@ -143,7 +144,7 @@ mod tests {
#[test]
fn test_index() {
// 示例:获取文件的元数据
let metadata = fs::metadata("lines.txt").unwrap();
let metadata = fs::metadata(".gitignore").unwrap();
println!("{:?}", util::format_time(&metadata.created().unwrap()));
println!("{:?}", util::format_time(&metadata.modified().unwrap()));
println!("{:?}", metadata.len());
@@ -151,7 +152,7 @@ mod tests {
#[test]
fn test_save() {
util::setup_test_with_mit();
util::setup_test_with_clean_mit();
let mut index = Index::new();
let metadata = fs::metadata("../.gitignore").unwrap();
let file_meta_data = FileMetaData {

View File

@@ -41,7 +41,7 @@ impl Tree {
pub fn save(&self) -> String {
let s = store::Store::new();
let tree_data = serde_json::to_string(&self).unwrap();
let tree_data = serde_json::to_string_pretty(&self).unwrap();
let hash = s.save(&tree_data);
hash
}

View File

@@ -34,13 +34,11 @@ impl Store {
pub fn save(&self, content: &String) -> String {
/* 保存文件内容 */
println!("store_path: {:?}", self.store_path);
let hash = util::calc_hash(content);
let mut path = self.store_path.clone();
println!("path: {:?}", path);
path.push("objects");
path.push(&hash);
println!("path: {:?}", path);
println!("Saved to: [{}]", path.display());
match std::fs::write(path, content) {
Ok(_) => hash,
Err(_) => panic!("储存库疑似损坏,无法写入文件"),