From 52b0d3db1289a74aa4817b211c3fcbf0bba84a99 Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Tue, 18 Mar 2025 11:31:19 +0700 Subject: upd: replace HashMap to IndexMap to preserve header's order --- Cargo.lock | 23 +++++++++++++++++++++++ Cargo.toml | 1 + src/object.rs | 16 ++++++++-------- src/utilities.rs | 11 +++++++++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c98c234..4fcb9ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -180,6 +180,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "flate2" version = "1.1.0" @@ -208,10 +214,17 @@ dependencies = [ "configparser 3.1.0", "derive_is_enum_variant", "flate2", + "indexmap", "ini", "sha1", ] +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + [[package]] name = "heck" version = "0.3.3" @@ -227,6 +240,16 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "indexmap" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3954d50fe15b02142bf25d3b8bdadb634ec3948f103d04ffe3031bc8fe9d7058" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "ini" version = "1.3.0" diff --git a/Cargo.toml b/Cargo.toml index 9399511..db656f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,6 @@ clap = { version = "4.5.32", features = ["derive"] } configparser = "3.1.0" derive_is_enum_variant = "0.1.1" flate2 = "1.1.0" +indexmap = "2.8.0" ini = "1.3.0" sha1 = "0.10.6" diff --git a/src/object.rs b/src/object.rs index aa5f434..90a06f4 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,14 +1,14 @@ -use std::collections::HashMap; -use std::fs; +use crate::utilities::deserialize_kv_with_message; use crate::{create_path_or_die, die}; +use derive_is_enum_variant::is_enum_variant; use flate2::read::{ZlibDecoder, ZlibEncoder}; +use flate2::Compression; +use indexmap::IndexMap; +use sha1::{Digest, Sha1}; +use std::fs; use std::fs::File; use std::io::{BufReader, Read, Write}; use std::path::PathBuf; -use derive_is_enum_variant::is_enum_variant; -use flate2::Compression; -use sha1::{Digest, Sha1}; -use crate::utilities::deserialize_kv_with_message; #[derive(is_enum_variant)] pub enum GitObjectType { @@ -120,14 +120,14 @@ impl GitObject { } pub struct GitCommit { - header: HashMap, + header: IndexMap, message: String, } impl GitCommit { pub fn new() -> Self { Self { - header: HashMap::new(), + header: IndexMap::new(), message: String::new(), } } diff --git a/src/utilities.rs b/src/utilities.rs index 8f80ecd..1a19f20 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -1,3 +1,4 @@ +use indexmap::IndexMap; use std::collections::HashMap; use std::path::PathBuf; @@ -50,14 +51,14 @@ pub fn path_should_not_exist(path: &PathBuf, message: &str) { } } -pub fn deserialize_kv_with_message(data: &Vec) -> (HashMap, String) { +pub fn deserialize_kv_with_message(data: &Vec) -> (IndexMap, String) { let string_rep = String::from_utf8(data.clone()); if string_rep.is_err() { die!("Failed to parse commit data"); } let string_rep = string_rep.unwrap(); let mut is_message = false; - let mut header: HashMap = HashMap::new(); + let mut header: IndexMap = IndexMap::new(); let mut message = String::new(); let mut last_key: Option = None; for line in string_rep.lines() { @@ -95,3 +96,9 @@ pub fn deserialize_kv_with_message(data: &Vec) -> (HashMap, } (header, message) } + +pub fn serialize_kv_with_message(header: &HashMap, message: &str) { + for (key, val) in header.into_iter() { + + } +} -- cgit v1.2.3-70-g09d2