Skip to content

Commit

Permalink
Reorganize cgp-core re-exports (#18)
Browse files Browse the repository at this point in the history
* Reorganize re-exports in cgp-core

* Add DelegateErrorRaiser

* Reformat imports
  • Loading branch information
soareschen authored Jul 2, 2024
1 parent 89293a9 commit b76801f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 51 deletions.
3 changes: 1 addition & 2 deletions crates/cgp-component-macro-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 1 addition & 9 deletions crates/cgp-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
2 changes: 1 addition & 1 deletion crates/cgp-error-eyre/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/cgp-error-std/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<dyn StdError + Send + Sync + 'static>;

Expand Down
17 changes: 17 additions & 0 deletions crates/cgp-error/src/can_raise_error.rs
Original file line number Diff line number Diff line change
@@ -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<ParseIntError>`, 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<Context>)]
pub trait CanRaiseError<E>: HasErrorType {
fn raise_error(e: E) -> Self::Error;
}
19 changes: 19 additions & 0 deletions crates/cgp-error/src/delegate_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use core::marker::PhantomData;

use cgp_component::DelegateComponent;

use crate::{ErrorRaiser, HasErrorType};

pub struct DelegateErrorRaiser<Components>(pub PhantomData<Components>);

impl<Context, Error, Components, Delegate> ErrorRaiser<Context, Error>
for DelegateErrorRaiser<Components>
where
Context: HasErrorType,
Components: DelegateComponent<Error, Delegate = Delegate>,
Delegate: ErrorRaiser<Context, Error>,
{
fn raise_error(e: Error) -> Context::Error {
Delegate::raise_error(e)
}
}
28 changes: 28 additions & 0 deletions crates/cgp-error/src/has_error_type.rs
Original file line number Diff line number Diff line change
@@ -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<Context>)]
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<Context> = <Context as HasErrorType>::Error;
44 changes: 6 additions & 38 deletions crates/cgp-error/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Context>)]
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<ParseIntError>`, 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<Context>)]
pub trait CanRaiseError<E>: HasErrorType {
fn raise_error(e: E) -> Self::Error;
}
pub use can_raise_error::*;
pub use delegate_error::*;
pub use has_error_type::*;

0 comments on commit b76801f

Please sign in to comment.