diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-18 11:31:19 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-18 11:31:19 +0700 |
commit | 52b0d3db1289a74aa4817b211c3fcbf0bba84a99 (patch) | |
tree | 3eeccc3b497d0e49f39a37c91073fec39d8bd9e7 | |
parent | 2766f230eff64b63cf4c88e8c26019762a92494d (diff) |
upd: replace HashMap to IndexMap to preserve header's order
-rw-r--r-- | Cargo.lock | 23 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/object.rs | 16 | ||||
-rw-r--r-- | src/utilities.rs | 11 |
4 files changed, 41 insertions, 10 deletions
@@ -181,6 +181,12 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -208,11 +214,18 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -228,6 +241,16 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -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<String, String>, + header: IndexMap<String, String>, 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<u8>) -> (HashMap<String, String>, String) { +pub fn deserialize_kv_with_message(data: &Vec<u8>) -> (IndexMap<String, String>, 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<String, String> = HashMap::new(); + let mut header: IndexMap<String, String> = IndexMap::new(); let mut message = String::new(); let mut last_key: Option<String> = None; for line in string_rep.lines() { @@ -95,3 +96,9 @@ pub fn deserialize_kv_with_message(data: &Vec<u8>) -> (HashMap<String, String>, } (header, message) } + +pub fn serialize_kv_with_message(header: &HashMap<String, String>, message: &str) { + for (key, val) in header.into_iter() { + + } +} |