summaryrefslogtreecommitdiff
path: root/src/utilities.rs
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-18 10:55:30 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-18 10:55:30 +0700
commit66aa0f0d9d02800b4d5c12e9f566b8808b67c8ea (patch)
tree17b06b4b625d9a1273baa21e21f0bfe0ebbc272a /src/utilities.rs
parentba1d9d094fa9c9930b3d3919b175f12e9eeeb3f3 (diff)
refactor: deserialization out of commit obj because it will be used for tag as well
Diffstat (limited to 'src/utilities.rs')
-rw-r--r--src/utilities.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/utilities.rs b/src/utilities.rs
index 2c7a43f..11d0e23 100644
--- a/src/utilities.rs
+++ b/src/utilities.rs
@@ -49,3 +49,47 @@ pub fn path_should_not_exist(path: &PathBuf, message: &str) {
die!(message);
}
}
+
+pub fn deserialize_kv_with_message(data: &Vec<u8>) -> (HashMap<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 message = String::new();
+ let mut last_key: Option<String> = None;
+ for line in string_rep.lines() {
+ if is_message {
+ message.push_str(format!("\n{}", line).as_str());
+ continue;
+ }
+
+ if line == "" {
+ is_message = true;
+ continue;
+ }
+
+ if line.starts_with(" ") && last_key.is_some() {
+ let key = last_key.clone().unwrap();
+ match header.get(&key) {
+ Some(val) => {
+ let mut new_val = val.clone();
+ new_val.push_str(format!("\n{}", line).as_str());
+ header.insert(key, new_val);
+ }
+ None => {
+ header.insert(key, line.to_string());
+ }
+ }
+ } else {
+ let mut line_iter = line.splitn(2, ' ');
+ let key = line_iter.next().unwrap();
+ let val = line_iter.next().unwrap();
+ header.insert(key.to_string(), val.to_string());
+ last_key = Some(key.to_string());
+ }
+ }
+ (header, message)
+}