Skip to content

Commit

Permalink
Improve generate error
Browse files Browse the repository at this point in the history
  • Loading branch information
andriygm committed Oct 16, 2024
1 parent 22d08e0 commit dd7a3af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spackle-cli"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
repository = "https://github.com/a2-ai/spackle"

Expand Down
50 changes: 19 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use template::RenderedFile;
use thiserror::Error;
use tokio_stream::Stream;
use users::User;

Expand All @@ -15,36 +16,18 @@ mod needs;
pub mod slot;
pub mod template;

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum GenerateError {
#[error("The output directory already exists: {0}")]
AlreadyExists(PathBuf),
#[error("Error loading config: {0}")]
BadConfig(config::Error),
#[error("Error copying files: {0}")]
CopyError(copy::Error),
TemplateError,
}

impl Display for GenerateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GenerateError::AlreadyExists(dir) => {
write!(f, "Directory already exists: {}", dir.display())
}
GenerateError::BadConfig(e) => write!(f, "Error loading config: {}", e),
GenerateError::TemplateError => write!(f, "Error rendering template"),
GenerateError::CopyError(e) => write!(f, "Error copying files: {}", e),
}
}
}

impl std::error::Error for GenerateError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
GenerateError::AlreadyExists(_) => None,
GenerateError::BadConfig(_) => None,
GenerateError::TemplateError => None,
GenerateError::CopyError(e) => Some(e),
}
}
#[error("Error rendering templates: {0}")]
TemplateError(#[from] tera::Error),
#[error("Error rendering file: {0}")]
FileError(#[from] template::FileError),
}

// Gets the output name as the canonicalized path's file stem
Expand Down Expand Up @@ -136,15 +119,20 @@ impl Project {
.map_err(GenerateError::CopyError)?;

// Render template files to the output directory
// TODO improve returned error type here
let results = template::fill(project_dir, out_dir, &slot_data)
.map_err(|_| GenerateError::TemplateError)?;
.map_err(GenerateError::TemplateError)?;

// Split vector into vector of rendered files and vector of errors
let mut okay_results = Vec::new();

if results.iter().any(|r| r.is_err()) {
return Err(GenerateError::TemplateError);
for result in results {
match result {
Ok(rendered_file) => okay_results.push(rendered_file),
Err(error) => return Err(GenerateError::FileError(error)),
}
}

Ok(results.into_iter().filter_map(|r| r.ok()).collect())
Ok(okay_results)
}

pub fn validate(&self) -> Result<(), template::ValidateError> {
Expand Down
3 changes: 2 additions & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use std::{
time::Duration,
};
use tera::{Context, Tera};
use thiserror::Error;

use super::slot::Slot;

pub const TEMPLATE_EXT: &str = ".j2";

#[derive(Debug)]
#[derive(Error, Debug)]
pub struct FileError {
pub kind: FileErrorKind,
pub file: String,
Expand Down

0 comments on commit dd7a3af

Please sign in to comment.