Skip to content

Commit

Permalink
Improve error handling, hook enabled logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andriygm committed Oct 16, 2024
1 parent dd7a3af commit e5a8202
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 deletions.
37 changes: 10 additions & 27 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Display, path::Path};
use std::{io, process};
use tera::{Context, Tera};
use thiserror::Error;
use tokio::pin;
use tokio_stream::{Stream, StreamExt};
use users::User;
Expand Down Expand Up @@ -69,14 +70,11 @@ impl Needy for Hook {
}

fn is_enabled(&self, data: &HashMap<String, String>) -> bool {
// TODO verify that this is still the correct implementation
match &self.default {
Some(default) => data
.get(&self.key)
.map(|s: &String| s.parse::<bool>().unwrap_or(false))
.unwrap_or(*default),
None => true,
if data.contains_key(&self.key) {
return data[&self.key] == "true";
}

self.default.unwrap_or(true)
}

fn is_satisfied(&self, items: &Vec<&dyn Needy>, data: &HashMap<String, String>) -> bool {
Expand Down Expand Up @@ -189,33 +187,18 @@ impl Display for SkipReason {
}
}

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum Error {
#[error("Error initializing runtime: {0}")]
ErrorInitializingRuntime(io::Error),
#[error("Error rendering template: {0}")]
ErrorRenderingTemplate(Hook, tera::Error),
#[error("Invalid conditional: {0}")]
InvalidConditional(Hook, ConditionalError),
#[error("Setup failed: {0}")]
SetupFailed(Hook, io::Error),
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::ErrorInitializingRuntime(e) => {
write!(f, "error initializing runtime: {}", e)
}
Error::ErrorRenderingTemplate(hook, e) => {
write!(f, "error rendering template for hook {}: {}", hook.key, e)
}
Error::InvalidConditional(hook, e) => {
write!(f, "invalid conditional for hook {}: {}", hook.key, e)
}
Error::SetupFailed(hook, e) => {
write!(f, "setup failed for hook {}: {}", hook.key, e)
}
}
}
}

#[derive(Serialize, Debug)]
pub enum HookStreamResult {
HookStarted(String),
Expand Down
21 changes: 5 additions & 16 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,18 @@ impl Display for FileError {
}
}

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum FileErrorKind {
#[error("Error rendering contents: {0}")]
ErrorRenderingContents(tera::Error),
#[error("Error rendering name: {0}")]
ErrorRenderingName(tera::Error),
#[error("Error creating destination: {0}")]
ErrorCreatingDest(io::ErrorKind),
#[error("Error writing to destination: {0}")]
ErrorWritingToDest(io::Error),
}

impl Display for FileErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FileErrorKind::ErrorRenderingContents(e) => {
write!(f, "error rendering template contents: {}", e)
}
FileErrorKind::ErrorRenderingName(e) => {
write!(f, "error rendering template name: {}", e)
}
FileErrorKind::ErrorCreatingDest(e) => write!(f, "error creating directory: {}", e),
FileErrorKind::ErrorWritingToDest(e) => write!(f, "error writing file: {}", e),
}
}
}

#[derive(Debug, Clone)]
pub struct RenderedFile {
pub path: PathBuf,
Expand Down

0 comments on commit e5a8202

Please sign in to comment.