diff --git a/crates/cgp-component-macro-lib/src/lib.rs b/crates/cgp-component-macro-lib/src/lib.rs index 058935f..2a3d2bb 100644 --- a/crates/cgp-component-macro-lib/src/lib.rs +++ b/crates/cgp-component-macro-lib/src/lib.rs @@ -4,6 +4,5 @@ pub mod derive_component; #[cfg(test)] mod tests; -pub use crate::delegate_components::define_components; -pub use crate::delegate_components::delegate_components; +pub use crate::delegate_components::{define_components, delegate_components}; pub use crate::derive_component::derive_component; diff --git a/crates/cgp-core/src/lib.rs b/crates/cgp-core/src/lib.rs index f106804..ff1478c 100644 --- a/crates/cgp-core/src/lib.rs +++ b/crates/cgp-core/src/lib.rs @@ -3,12 +3,4 @@ pub mod prelude; pub use cgp_async::{async_trait, Async}; -pub use cgp_component::{ - define_components, delegate_components, derive_component, DelegateComponent, HasComponents, -}; -pub use cgp_error::{ - CanRaiseError, ErrorRaiser, ErrorRaiserComponent, ErrorTypeComponent, HasErrorType, - ProvideErrorType, -}; -pub use cgp_inner::{HasInner, InnerComponent, ProvideInner}; -pub use cgp_run::{CanRun, Runner, RunnerComponent}; +pub use {cgp_component as component, cgp_error as error, cgp_inner as inner, cgp_run as run}; diff --git a/crates/cgp-error-eyre/src/lib.rs b/crates/cgp-error-eyre/src/lib.rs index aea208f..844ab00 100644 --- a/crates/cgp-error-eyre/src/lib.rs +++ b/crates/cgp-error-eyre/src/lib.rs @@ -1,8 +1,8 @@ use core::fmt::{Debug, Display}; use std::error::Error as StdError; +use cgp_core::error::{ErrorRaiser, ProvideErrorType}; use cgp_core::prelude::*; -use cgp_core::{ErrorRaiser, ProvideErrorType}; use eyre::{eyre, Report}; pub struct ProvideEyreError; diff --git a/crates/cgp-error-std/src/lib.rs b/crates/cgp-error-std/src/lib.rs index d76d7e8..7b2a5a5 100644 --- a/crates/cgp-error-std/src/lib.rs +++ b/crates/cgp-error-std/src/lib.rs @@ -1,7 +1,7 @@ use std::error::Error as StdError; +use cgp_core::error::{ErrorRaiser, ProvideErrorType}; use cgp_core::prelude::*; -use cgp_core::{ErrorRaiser, ProvideErrorType}; pub type Error = Box; diff --git a/crates/cgp-error/src/can_raise_error.rs b/crates/cgp-error/src/can_raise_error.rs new file mode 100644 index 0000000..9dbba85 --- /dev/null +++ b/crates/cgp-error/src/can_raise_error.rs @@ -0,0 +1,17 @@ +use cgp_async::Async; +use cgp_component::{derive_component, DelegateComponent, HasComponents}; + +use crate::has_error_type::HasErrorType; + +/** + Used for injecting external error types into [`Self::Error`](HasErrorType::Error). + + As an example, if `Context: CanRaiseError`, then we would be + able to call `Context::raise_error(err)` for an error value + [`err: ParseIntError`](core::num::ParseIntError) and get back + a [`Context::Error`](HasErrorType::Error) value. +*/ +#[derive_component(ErrorRaiserComponent, ErrorRaiser)] +pub trait CanRaiseError: HasErrorType { + fn raise_error(e: E) -> Self::Error; +} diff --git a/crates/cgp-error/src/delegate_error.rs b/crates/cgp-error/src/delegate_error.rs new file mode 100644 index 0000000..378a61c --- /dev/null +++ b/crates/cgp-error/src/delegate_error.rs @@ -0,0 +1,19 @@ +use core::marker::PhantomData; + +use cgp_component::DelegateComponent; + +use crate::{ErrorRaiser, HasErrorType}; + +pub struct DelegateErrorRaiser(pub PhantomData); + +impl ErrorRaiser + for DelegateErrorRaiser +where + Context: HasErrorType, + Components: DelegateComponent, + Delegate: ErrorRaiser, +{ + fn raise_error(e: Error) -> Context::Error { + Delegate::raise_error(e) + } +} diff --git a/crates/cgp-error/src/has_error_type.rs b/crates/cgp-error/src/has_error_type.rs new file mode 100644 index 0000000..e446727 --- /dev/null +++ b/crates/cgp-error/src/has_error_type.rs @@ -0,0 +1,28 @@ +use core::fmt::Debug; + +use cgp_async::Async; +use cgp_component::{derive_component, DelegateComponent, HasComponents}; + +/** + This is used for contexts to declare that they have a _unique_ `Self::Error` type. + + Although it is possible for each context to declare their own associated + `Error` type, doing so may result in having multiple ambiguous `Self::Error` types, + if there are multiple associated types with the same name in different traits. + + As a result, it is better for context traits to include `HasError` as their + parent traits, so that multiple traits can all refer to the same abstract + `Self::Error` type. +*/ +#[derive_component(ErrorTypeComponent, ProvideErrorType)] +pub trait HasErrorType: Async { + /** + The `Error` associated type is also required to implement [`Debug`]. + + This is to allow `Self::Error` to be used in calls like `.unwrap()`, + as well as for simpler error logging. + */ + type Error: Async + Debug; +} + +pub type ErrorOf = ::Error; diff --git a/crates/cgp-error/src/lib.rs b/crates/cgp-error/src/lib.rs index 12e76d7..122a245 100644 --- a/crates/cgp-error/src/lib.rs +++ b/crates/cgp-error/src/lib.rs @@ -1,39 +1,7 @@ -use core::fmt::Debug; +mod can_raise_error; +mod delegate_error; +mod has_error_type; -use cgp_async::Async; -use cgp_component::{derive_component, DelegateComponent, HasComponents}; - -/** - This is used for contexts to declare that they have a _unique_ `Self::Error` type. - - Although it is possible for each context to declare their own associated - `Error` type, doing so may result in having multiple ambiguous `Self::Error` types, - if there are multiple associated types with the same name in different traits. - - As a result, it is better for context traits to include `HasError` as their - parent traits, so that multiple traits can all refer to the same abstract - `Self::Error` type. -*/ -#[derive_component(ErrorTypeComponent, ProvideErrorType)] -pub trait HasErrorType: Async { - /** - The `Error` associated type is also required to implement [`Debug`]. - - This is to allow `Self::Error` to be used in calls like `.unwrap()`, - as well as for simpler error logging. - */ - type Error: Async + Debug; -} - -/** - Used for injecting external error types into [`Self::Error`](HasErrorType::Error). - - As an example, if `Context: CanRaiseError`, then we would be - able to call `Context::raise_error(err)` for an error value - [`err: ParseIntError`](core::num::ParseIntError) and get back - a [`Context::Error`](HasErrorType::Error) value. -*/ -#[derive_component(ErrorRaiserComponent, ErrorRaiser)] -pub trait CanRaiseError: HasErrorType { - fn raise_error(e: E) -> Self::Error; -} +pub use can_raise_error::*; +pub use delegate_error::*; +pub use has_error_type::*;