Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Overhaul error system
Browse files Browse the repository at this point in the history
Separate diagnostics reporting from high-level engine results.

(lost the much longer commit message I wrote, sorry)
  • Loading branch information
cassaundra committed Apr 9, 2024
1 parent bc36c06 commit 4295683
Show file tree
Hide file tree
Showing 19 changed files with 513 additions and 402 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ edition.workspace = true
quake_core = { path = "crates/quake_core" }
quake_engine = { path = "crates/quake_engine" }

anyhow = "1.0.81"
clap = { version = "4.4.18", features = ["cargo", "wrap_help"] }
serde_json = "1.0.113"

Expand Down
10 changes: 1 addition & 9 deletions crates/quake_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ pub mod utils;
/// descending precedence.
pub const BUILD_SCRIPT_NAMES: &[&str] = &["build.quake", "build.quake.nu"];

pub mod exit_codes {
pub const TASK_RUN_FAIL: i32 = 1;
pub const TASK_DECL_FAIL: i32 = 2;
pub const LOAD_FAIL: i32 = 3;

pub const INTERNAL_ERROR: i32 = 255;
}

pub mod prelude {
pub use quake_errors::*;
pub use quake_log::{log_error, log_fatal, log_info, log_warning, panic_bug};

pub use crate::match_expr;
pub use crate::project::*;
pub use crate::{exit_codes, match_expr};
}
6 changes: 3 additions & 3 deletions crates/quake_core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Metadata {
self.tasks.get(task_id)
}

pub fn find_task(&self, name: &str, span: Option<Span>) -> Result<&Arc<Task>> {
pub fn find_task(&self, name: &str, span: Option<Span>) -> DiagResult<&Arc<Task>> {
self.tasks
.iter()
.find(|t| t.name.item == name)
Expand All @@ -48,7 +48,7 @@ impl Metadata {
})
}

pub fn find_task_id(&self, name: &str, span: Option<Span>) -> Result<TaskId> {
pub fn find_task_id(&self, name: &str, span: Option<Span>) -> DiagResult<TaskId> {
self.tasks
.iter()
.position(|t| t.name.item == name)
Expand All @@ -66,7 +66,7 @@ impl Metadata {
name: String,
task: impl Into<Arc<Task>>,
span: Span,
) -> Result<TaskId> {
) -> DiagResult<TaskId> {
if let Ok(existing) = self.find_task(&name, None) {
return Err(errors::TaskDuplicateDefinition {
name,
Expand Down
18 changes: 10 additions & 8 deletions crates/quake_core/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ impl Project {
/// ## Errors
///
/// If the project root is not a directory, this will return an `Err` with
/// [`errors::ProjectNotFound`].
/// If no build script file is found in the
/// [`errors::ProjectNotFound`]. If no build script file is found in the
/// project root, this will return an `Err` with
/// [`errors::BuildScriptNotFound`].
pub fn new(project_root: PathBuf) -> Result<Self> {
pub fn new(project_root: PathBuf) -> EngineResult<Self> {
if !project_root.is_dir() {
return Err(errors::ProjectNotFound.into());
bail!("{}", errors::ProjectNotFound);
}

let build_script = find_build_script(&project_root).ok_or(errors::BuildScriptNotFound)?;
let build_script = find_build_script(&project_root)
.ok_or_else(|| error!("{}", errors::BuildScriptNotFound))?;

Ok(Self {
project_root,
Expand All @@ -43,9 +43,10 @@ impl Project {
///
/// If the project root is not a directory or no project can be found, this
/// will return an `Err` with [`errors::ProjectNotFound`].
pub fn locate(current_dir: impl AsRef<Path>) -> Result<Self> {
pub fn locate(current_dir: impl AsRef<Path>) -> EngineResult<Self> {
if !current_dir.as_ref().is_dir() {
return Err(errors::ProjectNotFound.into());
eprintln!("{}", errors::ProjectNotFound);
bail!(EngineError::LoadFailed);
}

if let Some(build_script) = find_build_script(&current_dir) {
Expand All @@ -56,7 +57,8 @@ impl Project {
} else if let Some(parent) = current_dir.as_ref().parent() {
Self::locate(parent)
} else {
Err(errors::ProjectNotFound.into())
eprintln!("{}", errors::ProjectNotFound);
bail!("{}", EngineError::LoadFailed);
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/quake_core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ pub fn get_init_cwd() -> Option<PathBuf> {
.or_else(|| std::env::var(PWD_ENV).ok().map(Into::into))
}

pub fn latest_timestamp(paths: &[impl AsRef<Path>]) -> Result<Option<SystemTime>> {
pub fn latest_timestamp(paths: &[impl AsRef<Path>]) -> DiagResult<Option<SystemTime>> {
Ok(paths
.iter()
.filter(|p| p.as_ref().exists())
.map(|s| fs::metadata(s).and_then(|m| m.modified()).into_diagnostic())
.collect::<Result<Vec<_>>>()?
.collect::<DiagResult<Vec<_>>>()?
.into_iter()
.max())
}

pub fn is_dirty(task: &TaskCallMetadata) -> Result<bool> {
pub fn is_dirty(task: &TaskCallMetadata) -> DiagResult<bool> {
// if either is undefined, assume dirty
if task.sources.is_empty() || task.artifacts.is_empty() {
return Ok(true);
Expand Down
Loading

0 comments on commit 4295683

Please sign in to comment.