From f1929f7921b2b3fcd86613c8f4c8a8c0367aad6c Mon Sep 17 00:00:00 2001 From: wzslr321 Date: Thu, 10 Oct 2024 19:44:39 +0200 Subject: [PATCH] feat: use anyhow::Result throughout the codebase; comment unused code --- src/cli.rs | 24 +++++++++++------------- src/config.rs | 47 +++++++++++++++++++++++------------------------ src/git.rs | 1 + src/main.rs | 1 + src/transform.rs | 1 + src/utils/mod.rs | 12 +++++++++++- 6 files changed, 48 insertions(+), 38 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index f9aa3af..81d9746 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,7 +12,9 @@ use serde_json::to_string_pretty; use crate::transform::init_registry; use crate::utils; -use crate::{config::Config, git::clone_repo, git::extract_difference, git::open_repo}; +use crate::git; +use crate::config; +use anyhow::Result; #[derive(Parser, Debug, Clone)] #[command( @@ -75,7 +77,7 @@ struct Args { origin: String, } -// TODO add more credentials variants +// TODO: add more credentials variants pub enum Credentials<'a> { UsernamePassword { username: &'a str, @@ -133,14 +135,10 @@ pub fn run() -> Result<(), CliError> { }, }; - let mock_credentials = Credentials::UsernamePassword { - username: "wzslr321", - password: "TEST", - }; - - crate::git::fetch_remote(&repository, &args.origin, &mock_credentials).unwrap(); + let credentials = utils::get_mock_credentials(); + git::fetch_remote(&repository, &args.origin, &credentials).unwrap(); - let diff = extract_difference( + let diff = git::extract_difference( &repository, &crate::git::DiffOptions::Branches { from: &args.from_branch.unwrap(), @@ -181,7 +179,7 @@ fn check_args_validity(args: Args) -> Result<(), CliError> { } fn try_retrieve_repo_from_path(path: Box) -> Result { - match open_repo(&path) { + match git::open_repo(&path) { Ok(repository) => { info!("sucessfully retrieved repository from path"); Ok(repository) @@ -220,7 +218,7 @@ fn try_retrieve_repo_from_url( username, password: &access_token.unwrap_or_else(|| "OnlyForTesting".to_string()), // ehttps://www.twitch.tv/directory/followingxpect("access_token must be specified, as it is the only supported authentication method for now"), }; - let cloned_repo = match clone_repo(&credentials, url, &clone_into_path) { + let cloned_repo = match git::clone_repo(&credentials, url, &clone_into_path) { Ok(repo) => repo, Err(e) => { error!("Failed to retreive repository from url.\nError: {}", e); @@ -261,9 +259,9 @@ fn setup_logging(tracing_level: u8) { .init(); } -fn load_config(path: &Path) -> anyhow::Result { +fn load_config(path: &Path) -> anyhow::Result { trace!("Starting loading config from {:?}", path); - match Config::load_from_file(path) { + match config::Config::load_from_file(path) { Ok(config) => { info!("Config loaded successfully"); Ok(config) diff --git a/src/config.rs b/src/config.rs index c94c62f..b0d9e57 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,9 @@ -use anyhow::{Context, Result}; +use anyhow::Result; use serde::{Deserialize, Deserializer}; use tracing::error; use std::cmp; use std::fmt; use std::path::Path; -use std::path::PathBuf; use thiserror::Error; use tracing::debug; use url::Url; @@ -35,16 +34,16 @@ pub struct OptionsConfig { pub clone_into: Option>, } -#[derive(Debug, Deserialize)] -pub struct Trigger { - pub path: Box, - - #[serde(default)] - pub pattern: Option, - - #[serde(default)] - pub analyze_dependencies: bool, -} +// #[derive(Debug, Deserialize)] +// pub struct Trigger { +// pub path: Box, +// +// #[serde(default)] +// pub pattern: Option, +// +// #[serde(default)] +// pub analyze_dependencies: bool, +// } #[derive(Debug, Deserialize)] pub struct TransformStep { @@ -55,17 +54,17 @@ pub struct TransformStep { #[derive(Debug, Deserialize)] pub struct Transform { - pub name: String, + // pub name: String, #[serde(default)] pub steps: Vec, } -#[derive(Debug, Deserialize)] -pub struct Matcher { - pub path: PathBuf, - pub pattern: String, -} +// #[derive(Debug, Deserialize)] +// pub struct Matcher { +// pub path: PathBuf, +// pub pattern: String, +// } #[derive(Debug, Deserialize)] pub enum AlertLevel { @@ -76,17 +75,17 @@ pub enum AlertLevel { #[derive(Debug, Deserialize)] pub struct Action { - pub alert_level: AlertLevel, - pub message: String, + // pub alert_level: AlertLevel, + // pub message: String, } #[derive(Debug, Deserialize)] pub struct Rule { - pub name: String, - pub trigger: Trigger, + // pub name: String, + // pub trigger: Trigger, pub transform: Transform, - pub matcher: Matcher, - pub action: Action, + // pub matcher: Matcher, + // pub action: Action, } pub struct CustomStep { diff --git a/src/git.rs b/src/git.rs index 499e20f..4d58492 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,6 +1,7 @@ use std::path::Path; use serde::Serialize; use thiserror::Error; +use anyhow::Result; use git2::{Cred, RemoteCallbacks, Repository}; use std::str; diff --git a/src/main.rs b/src/main.rs index d8d2e36..32ac406 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod utils; mod transform; use cli::CliError; +use anyhow::Result; fn main() -> Result<(), CliError> { cli::run() diff --git a/src/transform.rs b/src/transform.rs index c4f01cf..1f71aa5 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -1,4 +1,5 @@ use crate::config::CustomStep; +use anyhow::Result; use rhai::{Dynamic, Engine, Map, Scope}; use tracing::trace; use std::collections::HashMap; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 7049829..36c25f0 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,7 +1,10 @@ use std::{fs, path::Path}; use tracing::{info, trace}; +use anyhow::Result; -pub fn prepare_directory(path: &Path) -> Result<(), anyhow::Error> { +use crate::cli::Credentials; + +pub fn prepare_directory(path: &Path) -> Result<()> { if path.exists() { if path.read_dir()?.next().is_some() { info!("Directory is not empty, removing existing files..."); @@ -15,3 +18,10 @@ pub fn prepare_directory(path: &Path) -> Result<(), anyhow::Error> { trace!("Successfully prepared directory for cloning"); Ok(()) } + +pub fn get_mock_credentials<'a>() -> &'a Credentials<'a> { + &Credentials::UsernamePassword { + username: "wzslr321", + password: "TEST", + } +}