diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-15 17:01:27 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-15 17:01:27 +0700 |
commit | ef420db3ab72b1a49a7706fdbabce1a179b41bf9 (patch) | |
tree | 3c894d7e5b9f6414d6404a91c237f08c76f577e3 | |
parent | 3390e88aa0f1ec785b883663c2159ededab45c3c (diff) |
trying macro
-rw-r--r-- | src/core.rs | 55 | ||||
-rw-r--r-- | src/utilities.rs | 30 |
2 files changed, 45 insertions, 40 deletions
diff --git a/src/core.rs b/src/core.rs index 4f9302e..aa1d04d 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,10 +1,10 @@ -use crate::utilities::{die, ConfigHashMap}; +use crate::utilities::ConfigHashMap; +use crate::{create_path_or_die, die}; +use configparser::ini::Ini; use ini::ini; use std::collections::HashMap; -use std::fs::{create_dir, File}; use std::io::Write; use std::path::PathBuf; -use configparser::ini::Ini; pub struct GitRepository { pub worktree: PathBuf, @@ -21,24 +21,24 @@ impl GitRepository { pub fn from_dir(worktree: PathBuf) -> Self { if !worktree.exists() { - die("Worktree does not exist"); + die!("Worktree does not exist"); } let gitdir = worktree.join(".git"); if !gitdir.exists() { - die("Not a valid git repository"); + die!("Not a valid git repository"); } let conf_file = gitdir.join("config"); if !conf_file.exists() { - die("Config file does not exist"); + die!("Config file does not exist"); } let conf = ini!(conf_file.to_str().unwrap()); let repo_format_version = conf["core"]["repositoryformatversion"].clone(); if let Some(version) = repo_format_version { if version != "0" { - die("Unsupported format version"); + die!("Unsupported format version"); } } else { - die("Could not determine repository format version"); + die!("Could not determine repository format version"); } Self { @@ -52,34 +52,19 @@ impl GitRepository { let repo = GitRepository::new(worktree); if repo.worktree.exists() { if repo.gitdir.exists() { - die("Repo already exists"); + die!("Repo already exists"); } } - let create_gitdir = create_dir(repo.gitdir.clone()); - if let Err(_) = create_gitdir { - die("Failed to initialize repository"); - } + create_path_or_die!(dir: repo.gitdir.clone(), "Failed to initialize repository"); - if let Err(_) = create_dir(repo.gitdir.clone().join("objects")) { - die("Failed to create objects directory"); - } + create_path_or_die!(dir: repo.gitdir.clone().join("objects"), "Failed to create objects directory"); - if let Err(_) = create_dir(repo.gitdir.clone().join("refs").join("heads")) { - die("Failed to create git refs/head directory"); - } + create_path_or_die!(dir: repo.gitdir.clone().join("refs").join("heads"), "Failed to create git refs/head directory"); - if let Err(_) = create_dir(repo.gitdir.clone().join("refs").join("tags")) { - die("Failed to create git refs/tags directory"); - } + create_path_or_die!(dir: repo.gitdir.clone().join("refs").join("tags"), "Failed to create git refs/tags directory"); - if let Ok(mut head_file) = File::create(repo.gitdir.clone().join("HEAD")) { - if let Err(_) = head_file.write_all(&"ref: refs/heads/master\n".as_ref()) { - die("Failed to write HEAD file"); - } - } else { - die("Failed to create HEAD file"); - } + create_path_or_die!(file: repo.gitdir.clone().join("HEAD"), &"ref: refs/heads/master\n", "Failed to write HEAD file"); let mut config = Ini::new(); config.set("core", "repositoryformatversion", Some("0".to_owned())); @@ -87,14 +72,12 @@ impl GitRepository { config.set("core", "bare", Some("false".to_owned())); if let Err(_) = config.write(repo.gitdir.join("config")) { - die("Failed to write config file"); + die!("Failed to write config file"); } - - if let Ok(mut description_file) = File::create(repo.gitdir.clone().join("config")) { - if let Err(_) = description_file.write("Unnamed repository; edit this file 'description' to name the repository.\n".as_ref()) { - die("Failed to write description file"); - } - } + create_path_or_die!( + file: repo.gitdir.clone().join("HEAD"), + "Unnamed repository; edit this file 'description' to name the repository.\n", + "Failed to write description file"); } }
\ No newline at end of file diff --git a/src/utilities.rs b/src/utilities.rs index 54f1292..aa7a44e 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -2,7 +2,29 @@ use std::collections::HashMap; pub type ConfigHashMap = HashMap<String, HashMap<String, Option<String>>>; -pub fn die(message: &str) { - println!("{}", message); - std::process::exit(1); -}
\ No newline at end of file +#[macro_export] +macro_rules! die { + ($message:expr) => { + println!("{}", $message); + std::process::exit(1); + } +} + +#[macro_export] +macro_rules! create_path_or_die { + (dir: $path:expr, $message:expr) => { + if let Err(_) = std::fs::create_dir($path) { + die!($message); + } + }; + + (file: $path:expr, $content:expr, $message:expr) => { + if let Ok(mut file) = std::fs::File::create($path) { + if let Err(_) = file.write_all($content.as_ref()) { + die!($message); + } + } else { + die!($message); + } + }; +} |