diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cc9d8d..b3e8d84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## v0.3.0 (pre-release) +- Error crates refactoring - [#48](https://github.com/contextgeneric/cgp/pull/48) + - Remove `Async` trait bound from `HasErrorType::Error`. + - Introduce `HasAsyncErrorType` trait used for adding `Async` constraint to `HasErrorType::Error`. + - Introduce `CanWrapError` trait. + - Introduce generic `ErrorRaiser` providers in `cgp-error`. + - Rename and reoganize constructs in `cgp-error-eyre` and `cgp-error-std`. + - Introduce `cgp-error-anyhow` crate. + - Decouple component and field macro crates from the library crates - [#47](https://github.com/contextgeneric/cgp/pull/47) - Remove `cgp-component-macro` crate from being a dependency of `cgp-component`. - Remove `cgp-field-macro` crate from being a dependency of `cgp-field`. diff --git a/Cargo.lock b/Cargo.lock index 808fcb2..326e6e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" + [[package]] name = "cgp" version = "0.2.0" @@ -74,6 +80,14 @@ dependencies = [ "cgp-type", ] +[[package]] +name = "cgp-error-anyhow" +version = "0.2.0" +dependencies = [ + "anyhow", + "cgp-core", +] + [[package]] name = "cgp-error-eyre" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 5cef490..c40d524 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ members = [ "crates/cgp-field-macro", "crates/cgp-field-macro-lib", "crates/cgp-error", + "crates/cgp-error-anyhow", "crates/cgp-error-eyre", "crates/cgp-error-std", "crates/cgp-run", diff --git a/crates/cgp-async-macro/src/lib.rs b/crates/cgp-async-macro/src/lib.rs index 66d49f8..956ee70 100644 --- a/crates/cgp-async-macro/src/lib.rs +++ b/crates/cgp-async-macro/src/lib.rs @@ -1,7 +1,10 @@ +#![no_std] + /*! This library provides helper macros for using async functions in traits. */ +extern crate alloc; extern crate proc_macro; use proc_macro::TokenStream; diff --git a/crates/cgp-async-macro/src/strip_async.rs b/crates/cgp-async-macro/src/strip_async.rs index a0250c3..d0e61ee 100644 --- a/crates/cgp-async-macro/src/strip_async.rs +++ b/crates/cgp-async-macro/src/strip_async.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use proc_macro2::{Group, TokenStream, TokenTree}; use syn::parse::{Parse, ParseStream}; use syn::token::{Async, Await, Dot, Fn}; diff --git a/crates/cgp-async/src/lib.rs b/crates/cgp-async/src/lib.rs index 16718dd..3e05e85 100644 --- a/crates/cgp-async/src/lib.rs +++ b/crates/cgp-async/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + pub mod traits; pub use traits::{Async, MaybeSend, MaybeStatic, MaybeSync}; diff --git a/crates/cgp-async/src/traits/async.rs b/crates/cgp-async/src/traits/async.rs index 695f7d2..31e68fe 100644 --- a/crates/cgp-async/src/traits/async.rs +++ b/crates/cgp-async/src/traits/async.rs @@ -4,27 +4,7 @@ use crate::traits::sync::MaybeSync; /** This is defined as a convenient constraint alias to - `Sized + Send + Sync + 'static`. - - This constraint is commonly required to be present in almost all associated - types. The `Sized` constraint is commonly required for associated types to be - used as generic parameters. The `Send + Sync + 'static` constraints are - important for the use of async functions inside traits. - - Because Rust do not currently natively support the use of async functions - in traits, we use the [`async_trait`] crate to desugar async functions - inside traits into functions returning - `Pin>`. Due to the additional `Send` and lifetime - trait bounds inside the returned boxed future, almost all values that are - used inside the async functions are required to have types that implement - `Send` and `Sync`. - - It is also common to require the associated types to have the `'static` - lifetime for them to be used inside async functions, because Rust would - otherwise infer a more restrictive lifetime that does not outlive the - async functions. The `'static` lifetime constraint here really means - that the types implementing `Async` must not contain any lifetime - parameter. + `Send + Sync + 'static`. */ pub trait Async: MaybeSend + MaybeSync + MaybeStatic {} diff --git a/crates/cgp-component-macro-lib/src/delegate_components/impl_delegate.rs b/crates/cgp-component-macro-lib/src/delegate_components/impl_delegate.rs index b53a8e5..f61809b 100644 --- a/crates/cgp-component-macro-lib/src/delegate_components/impl_delegate.rs +++ b/crates/cgp-component-macro-lib/src/delegate_components/impl_delegate.rs @@ -1,3 +1,6 @@ +use alloc::boxed::Box; +use alloc::vec; +use alloc::vec::Vec; use syn::{parse_quote, Generics, ImplItem, ImplItemType, ItemImpl, Path, Type}; use crate::delegate_components::ast::{ComponentAst, DelegateEntriesAst}; diff --git a/crates/cgp-component-macro-lib/src/derive_component/component_spec.rs b/crates/cgp-component-macro-lib/src/derive_component/component_spec.rs index d0c9a10..6769568 100644 --- a/crates/cgp-component-macro-lib/src/derive_component/component_spec.rs +++ b/crates/cgp-component-macro-lib/src/derive_component/component_spec.rs @@ -1,3 +1,4 @@ +use alloc::format; use proc_macro2::Span; use quote::ToTokens; use syn::parse::{Parse, ParseStream}; diff --git a/crates/cgp-component-macro-lib/src/derive_component/consumer_impl.rs b/crates/cgp-component-macro-lib/src/derive_component/consumer_impl.rs index ed7b393..0221d8a 100644 --- a/crates/cgp-component-macro-lib/src/derive_component/consumer_impl.rs +++ b/crates/cgp-component-macro-lib/src/derive_component/consumer_impl.rs @@ -1,3 +1,5 @@ +use alloc::boxed::Box; +use alloc::vec::Vec; use syn::punctuated::Punctuated; use syn::token::{Brace, Comma, For, Impl, Plus}; use syn::{ diff --git a/crates/cgp-component-macro-lib/src/derive_component/delegate_fn.rs b/crates/cgp-component-macro-lib/src/derive_component/delegate_fn.rs index bc71cea..28e3c16 100644 --- a/crates/cgp-component-macro-lib/src/derive_component/delegate_fn.rs +++ b/crates/cgp-component-macro-lib/src/derive_component/delegate_fn.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use proc_macro2::TokenStream; use quote::quote; use syn::{parse_quote, ImplItemFn, Signature, TypePath, Visibility}; diff --git a/crates/cgp-component-macro-lib/src/derive_component/entry.rs b/crates/cgp-component-macro-lib/src/derive_component/entry.rs index ad38eed..9d76c09 100644 --- a/crates/cgp-component-macro-lib/src/derive_component/entry.rs +++ b/crates/cgp-component-macro-lib/src/derive_component/entry.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeMap; +use alloc::collections::BTreeMap; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; diff --git a/crates/cgp-component-macro-lib/src/derive_component/provider_impl.rs b/crates/cgp-component-macro-lib/src/derive_component/provider_impl.rs index 658122c..d0631f3 100644 --- a/crates/cgp-component-macro-lib/src/derive_component/provider_impl.rs +++ b/crates/cgp-component-macro-lib/src/derive_component/provider_impl.rs @@ -1,3 +1,5 @@ +use alloc::boxed::Box; +use alloc::vec::Vec; use proc_macro2::Span; use syn::punctuated::Punctuated; use syn::token::{Brace, Comma, For, Impl, Plus}; diff --git a/crates/cgp-component-macro-lib/src/derive_component/provider_trait.rs b/crates/cgp-component-macro-lib/src/derive_component/provider_trait.rs index 8a3addb..aed9446 100644 --- a/crates/cgp-component-macro-lib/src/derive_component/provider_trait.rs +++ b/crates/cgp-component-macro-lib/src/derive_component/provider_trait.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use syn::punctuated::Punctuated; use syn::{parse_quote, Ident, ItemTrait, TraitItem}; diff --git a/crates/cgp-component-macro-lib/src/derive_component/replace_self_type.rs b/crates/cgp-component-macro-lib/src/derive_component/replace_self_type.rs index 6ee1006..16711fe 100644 --- a/crates/cgp-component-macro-lib/src/derive_component/replace_self_type.rs +++ b/crates/cgp-component-macro-lib/src/derive_component/replace_self_type.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use itertools::Itertools; use proc_macro2::{Group, Ident, TokenStream, TokenTree}; use quote::{format_ident, ToTokens}; diff --git a/crates/cgp-component-macro-lib/src/derive_component/snake_case.rs b/crates/cgp-component-macro-lib/src/derive_component/snake_case.rs index 6e82abc..10d0298 100644 --- a/crates/cgp-component-macro-lib/src/derive_component/snake_case.rs +++ b/crates/cgp-component-macro-lib/src/derive_component/snake_case.rs @@ -1,3 +1,4 @@ +use alloc::string::{String, ToString}; use proc_macro2::Span; use syn::Ident; diff --git a/crates/cgp-component-macro-lib/src/for_each_replace.rs b/crates/cgp-component-macro-lib/src/for_each_replace.rs index b385379..80c8349 100644 --- a/crates/cgp-component-macro-lib/src/for_each_replace.rs +++ b/crates/cgp-component-macro-lib/src/for_each_replace.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use proc_macro2::{Group, TokenStream, TokenTree}; use quote::{quote, ToTokens}; use syn::__private::parse_brackets; diff --git a/crates/cgp-component-macro-lib/src/lib.rs b/crates/cgp-component-macro-lib/src/lib.rs index 4267725..bef098d 100644 --- a/crates/cgp-component-macro-lib/src/lib.rs +++ b/crates/cgp-component-macro-lib/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + /*! This is an internal crate used by the `cgp-component-macro` crate. We implement the proc macros for `cgp-component` as a library, so that it can be more easily tested. @@ -5,6 +7,8 @@ which is defined as a proc macro crate. */ +extern crate alloc; + pub mod delegate_components; pub mod derive_component; pub mod for_each_replace; diff --git a/crates/cgp-component-macro-lib/src/preset/define_preset.rs b/crates/cgp-component-macro-lib/src/preset/define_preset.rs index a12bb50..4af3ae7 100644 --- a/crates/cgp-component-macro-lib/src/preset/define_preset.rs +++ b/crates/cgp-component-macro-lib/src/preset/define_preset.rs @@ -1,3 +1,5 @@ +use alloc::format; +use alloc::string::ToString; use proc_macro2::{Span, TokenStream}; use quote::ToTokens; use syn::{parse_quote, Ident, ItemTrait}; diff --git a/crates/cgp-component-macro-lib/src/preset/impl_is_preset.rs b/crates/cgp-component-macro-lib/src/preset/impl_is_preset.rs index 3fcdcc1..1168cd3 100644 --- a/crates/cgp-component-macro-lib/src/preset/impl_is_preset.rs +++ b/crates/cgp-component-macro-lib/src/preset/impl_is_preset.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use syn::{parse_quote, Generics, Ident, ItemImpl, Type}; use crate::delegate_components::ast::{ComponentAst, DelegateEntriesAst}; diff --git a/crates/cgp-component-macro-lib/src/tests/delegate_components.rs b/crates/cgp-component-macro-lib/src/tests/delegate_components.rs index 0566db5..1eaa0ce 100644 --- a/crates/cgp-component-macro-lib/src/tests/delegate_components.rs +++ b/crates/cgp-component-macro-lib/src/tests/delegate_components.rs @@ -2,7 +2,6 @@ use quote::quote; use crate::delegate_components; use crate::tests::helper::equal::equal_token_stream; -use crate::tests::helper::format::format_token_stream; #[test] fn test_basic_delegate_components() { @@ -50,8 +49,6 @@ fn test_delegate_components_containing_generics() { }) .unwrap(); - println!("derived: {}", format_token_stream(&derived)); - let expected = quote! { impl<'a, FooParamA, FooParamB: FooConstraint> DelegateComponent for FooComponents<'a, FooParamA, FooParamB> { diff --git a/crates/cgp-component-macro-lib/src/tests/helper/format.rs b/crates/cgp-component-macro-lib/src/tests/helper/format.rs index 2c96595..2e23f5a 100644 --- a/crates/cgp-component-macro-lib/src/tests/helper/format.rs +++ b/crates/cgp-component-macro-lib/src/tests/helper/format.rs @@ -1,3 +1,4 @@ +use alloc::string::{String, ToString}; use prettyplease::unparse; use proc_macro2::TokenStream; use syn::parse_file; diff --git a/crates/cgp-component-macro/src/lib.rs b/crates/cgp-component-macro/src/lib.rs index 23a139a..6fc23d2 100644 --- a/crates/cgp-component-macro/src/lib.rs +++ b/crates/cgp-component-macro/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + /*! This crate provides the proc macros used for defining CGP components. */ diff --git a/crates/cgp-component/src/lib.rs b/crates/cgp-component/src/lib.rs index b44d966..bd1cb97 100644 --- a/crates/cgp-component/src/lib.rs +++ b/crates/cgp-component/src/lib.rs @@ -2,7 +2,6 @@ /*! This crate defines the core CGP traits, [`DelegateComponent`] and [`HasComponents`]. - It also re-export component macros provided by [`cgp_component_macro`]. */ pub mod traits; diff --git a/crates/cgp-core/src/prelude.rs b/crates/cgp-core/src/prelude.rs index 8ebd30c..b63db7d 100644 --- a/crates/cgp-core/src/prelude.rs +++ b/crates/cgp-core/src/prelude.rs @@ -3,6 +3,6 @@ pub use cgp_component::{DelegateComponent, HasComponents}; pub use cgp_component_macro::{ cgp_component, cgp_preset, delegate_components, for_each_replace, replace_with, }; -pub use cgp_error::{CanRaiseError, HasErrorType}; +pub use cgp_error::traits::{CanRaiseError, CanWrapError, HasErrorType}; pub use cgp_field::{Char, Cons, Either, HasField, HasFieldMut, Nil, Void}; pub use cgp_field_macro::{product, symbol, HasField, Product, Sum}; diff --git a/crates/cgp-error-anyhow/Cargo.toml b/crates/cgp-error-anyhow/Cargo.toml new file mode 100644 index 0000000..4f09663 --- /dev/null +++ b/crates/cgp-error-anyhow/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "cgp-error-anyhow" +version = "0.2.0" +edition = { workspace = true } +license = { workspace = true } +repository = { workspace = true } +authors = { workspace = true } +rust-version = { workspace = true } +keywords = { workspace = true } +description = """ + Context-generic programming error handlers implemented using eyre +""" + +[dependencies] +cgp-core = { version = "0.2.0", default-features = false } +anyhow = { version = "1.0.95", default-features = false } \ No newline at end of file diff --git a/crates/cgp-error-anyhow/src/impls/debug_error.rs b/crates/cgp-error-anyhow/src/impls/debug_error.rs new file mode 100644 index 0000000..bbbf27e --- /dev/null +++ b/crates/cgp-error-anyhow/src/impls/debug_error.rs @@ -0,0 +1,17 @@ +use core::fmt::Debug; + +use anyhow::{anyhow, Error}; +use cgp_core::error::ErrorRaiser; +use cgp_core::prelude::*; + +pub struct DebugAnyhowError; + +impl ErrorRaiser for DebugAnyhowError +where + Context: HasErrorType, + E: Debug, +{ + fn raise_error(e: E) -> Error { + anyhow!("{:?}", e) + } +} diff --git a/crates/cgp-error-anyhow/src/impls/display_error.rs b/crates/cgp-error-anyhow/src/impls/display_error.rs new file mode 100644 index 0000000..0b26cb6 --- /dev/null +++ b/crates/cgp-error-anyhow/src/impls/display_error.rs @@ -0,0 +1,17 @@ +use core::fmt::Display; + +use anyhow::{anyhow, Error}; +use cgp_core::error::ErrorRaiser; +use cgp_core::prelude::*; + +pub struct DisplayAnyhowError; + +impl ErrorRaiser for DisplayAnyhowError +where + Context: HasErrorType, + E: Display, +{ + fn raise_error(e: E) -> Error { + anyhow!("{e}") + } +} diff --git a/crates/cgp-error-anyhow/src/impls/mod.rs b/crates/cgp-error-anyhow/src/impls/mod.rs new file mode 100644 index 0000000..90a10d8 --- /dev/null +++ b/crates/cgp-error-anyhow/src/impls/mod.rs @@ -0,0 +1,9 @@ +pub mod debug_error; +pub mod display_error; +pub mod raise_anyhow_error; +pub mod use_anyhow_error; + +pub use debug_error::*; +pub use display_error::*; +pub use raise_anyhow_error::*; +pub use use_anyhow_error::*; diff --git a/crates/cgp-error-anyhow/src/impls/raise_anyhow_error.rs b/crates/cgp-error-anyhow/src/impls/raise_anyhow_error.rs new file mode 100644 index 0000000..7b3f83b --- /dev/null +++ b/crates/cgp-error-anyhow/src/impls/raise_anyhow_error.rs @@ -0,0 +1,17 @@ +use core::error::Error as StdError; + +use anyhow::Error; +use cgp_core::error::ErrorRaiser; +use cgp_core::prelude::*; + +pub struct RaiseAnyhowError; + +impl ErrorRaiser for RaiseAnyhowError +where + Context: HasErrorType, + E: StdError + Send + Sync + 'static, +{ + fn raise_error(e: E) -> Error { + e.into() + } +} diff --git a/crates/cgp-error-anyhow/src/impls/use_anyhow_error.rs b/crates/cgp-error-anyhow/src/impls/use_anyhow_error.rs new file mode 100644 index 0000000..4a03a54 --- /dev/null +++ b/crates/cgp-error-anyhow/src/impls/use_anyhow_error.rs @@ -0,0 +1,8 @@ +use anyhow::Error; +use cgp_core::error::ProvideErrorType; + +pub struct UseAnyhowError; + +impl ProvideErrorType for UseAnyhowError { + type Error = Error; +} diff --git a/crates/cgp-error-anyhow/src/lib.rs b/crates/cgp-error-anyhow/src/lib.rs new file mode 100644 index 0000000..e00dca4 --- /dev/null +++ b/crates/cgp-error-anyhow/src/lib.rs @@ -0,0 +1,5 @@ +#![no_std] + +pub mod impls; + +pub use impls::*; diff --git a/crates/cgp-error-eyre/Cargo.toml b/crates/cgp-error-eyre/Cargo.toml index 2129d86..586db81 100644 --- a/crates/cgp-error-eyre/Cargo.toml +++ b/crates/cgp-error-eyre/Cargo.toml @@ -13,4 +13,4 @@ description = """ [dependencies] cgp-core = { version = "0.2.0", default-features = false } -eyre = { version = "0.6.11" } \ No newline at end of file +eyre = { version = "0.6.12", default-features = false } \ No newline at end of file diff --git a/crates/cgp-error-eyre/src/impls/debug_error.rs b/crates/cgp-error-eyre/src/impls/debug_error.rs new file mode 100644 index 0000000..d38815f --- /dev/null +++ b/crates/cgp-error-eyre/src/impls/debug_error.rs @@ -0,0 +1,17 @@ +use core::fmt::Debug; + +use cgp_core::error::ErrorRaiser; +use cgp_core::prelude::*; +use eyre::{eyre, Error}; + +pub struct DebugEyreError; + +impl ErrorRaiser for DebugEyreError +where + Context: HasErrorType, + E: Debug, +{ + fn raise_error(e: E) -> Error { + eyre!("{:?}", e) + } +} diff --git a/crates/cgp-error-eyre/src/impls/display_error.rs b/crates/cgp-error-eyre/src/impls/display_error.rs new file mode 100644 index 0000000..11b86d0 --- /dev/null +++ b/crates/cgp-error-eyre/src/impls/display_error.rs @@ -0,0 +1,17 @@ +use core::fmt::Display; + +use cgp_core::error::ErrorRaiser; +use cgp_core::prelude::*; +use eyre::{eyre, Error}; + +pub struct DisplayEyreError; + +impl ErrorRaiser for DisplayEyreError +where + Context: HasErrorType, + E: Display, +{ + fn raise_error(e: E) -> Error { + eyre!("{e}") + } +} diff --git a/crates/cgp-error-eyre/src/impls/mod.rs b/crates/cgp-error-eyre/src/impls/mod.rs new file mode 100644 index 0000000..e4eba7f --- /dev/null +++ b/crates/cgp-error-eyre/src/impls/mod.rs @@ -0,0 +1,9 @@ +pub mod debug_error; +pub mod display_error; +pub mod raise_eyre_error; +pub mod use_eyre_error; + +pub use debug_error::*; +pub use display_error::*; +pub use raise_eyre_error::*; +pub use use_eyre_error::*; diff --git a/crates/cgp-error-eyre/src/impls/raise_eyre_error.rs b/crates/cgp-error-eyre/src/impls/raise_eyre_error.rs new file mode 100644 index 0000000..a00473f --- /dev/null +++ b/crates/cgp-error-eyre/src/impls/raise_eyre_error.rs @@ -0,0 +1,17 @@ +use core::error::Error as StdError; + +use cgp_core::error::ErrorRaiser; +use cgp_core::prelude::*; +use eyre::Error; + +pub struct RaiseEyreError; + +impl ErrorRaiser for RaiseEyreError +where + Context: HasErrorType, + E: StdError + Send + Sync + 'static, +{ + fn raise_error(e: E) -> Error { + e.into() + } +} diff --git a/crates/cgp-error-eyre/src/impls/use_eyre_error.rs b/crates/cgp-error-eyre/src/impls/use_eyre_error.rs new file mode 100644 index 0000000..346a59e --- /dev/null +++ b/crates/cgp-error-eyre/src/impls/use_eyre_error.rs @@ -0,0 +1,8 @@ +use cgp_core::error::ProvideErrorType; +use eyre::Error; + +pub struct UseEyreError; + +impl ProvideErrorType for UseEyreError { + type Error = Error; +} diff --git a/crates/cgp-error-eyre/src/lib.rs b/crates/cgp-error-eyre/src/lib.rs index 757767f..e00dca4 100644 --- a/crates/cgp-error-eyre/src/lib.rs +++ b/crates/cgp-error-eyre/src/lib.rs @@ -1,48 +1,5 @@ -use core::fmt::{Debug, Display}; -use std::error::Error as StdError; +#![no_std] -use cgp_core::error::{ErrorRaiser, ProvideErrorType}; -use cgp_core::prelude::*; -use eyre::{eyre, Report}; +pub mod impls; -pub struct ProvideEyreError; - -impl ProvideErrorType for ProvideEyreError { - type Error = Report; -} - -pub struct RaiseStdError; - -impl ErrorRaiser for RaiseStdError -where - Context: HasErrorType, - E: StdError + Send + Sync + 'static, -{ - fn raise_error(e: E) -> Report { - e.into() - } -} - -pub struct RaiseDebugError; - -impl ErrorRaiser for RaiseDebugError -where - Context: HasErrorType, - E: Debug, -{ - fn raise_error(e: E) -> Report { - eyre!("{:?}", e) - } -} - -pub struct RaiseDisplayError; - -impl ErrorRaiser for RaiseDisplayError -where - Context: HasErrorType, - E: Display, -{ - fn raise_error(e: E) -> Report { - eyre!("{e}") - } -} +pub use impls::*; diff --git a/crates/cgp-error-std/src/impls/debug_error.rs b/crates/cgp-error-std/src/impls/debug_error.rs new file mode 100644 index 0000000..90c6558 --- /dev/null +++ b/crates/cgp-error-std/src/impls/debug_error.rs @@ -0,0 +1,19 @@ +use core::fmt::Debug; + +use alloc::boxed::Box; +use alloc::format; +use cgp_core::error::{ErrorRaiser, HasErrorType}; + +use crate::types::{Error, StringError}; + +pub struct DebugBoxedStdError; + +impl ErrorRaiser for DebugBoxedStdError +where + Context: HasErrorType, + E: Debug, +{ + fn raise_error(e: E) -> Error { + Box::new(StringError::from(format!("{e:?}"))) + } +} diff --git a/crates/cgp-error-std/src/impls/display_error.rs b/crates/cgp-error-std/src/impls/display_error.rs new file mode 100644 index 0000000..885c36e --- /dev/null +++ b/crates/cgp-error-std/src/impls/display_error.rs @@ -0,0 +1,19 @@ +use core::fmt::Display; + +use alloc::boxed::Box; +use alloc::format; +use cgp_core::error::{ErrorRaiser, HasErrorType}; + +use crate::types::{Error, StringError}; + +pub struct DisplayBoxedStdError; + +impl ErrorRaiser for DisplayBoxedStdError +where + Context: HasErrorType, + E: Display, +{ + fn raise_error(e: E) -> Error { + Box::new(StringError::from(format!("{e}"))) + } +} diff --git a/crates/cgp-error-std/src/impls/mod.rs b/crates/cgp-error-std/src/impls/mod.rs new file mode 100644 index 0000000..6c3ec59 --- /dev/null +++ b/crates/cgp-error-std/src/impls/mod.rs @@ -0,0 +1,9 @@ +pub mod debug_error; +pub mod display_error; +pub mod raise_boxed; +pub mod use_boxed; + +pub use debug_error::*; +pub use display_error::*; +pub use raise_boxed::*; +pub use use_boxed::*; diff --git a/crates/cgp-error-std/src/impls/raise_boxed.rs b/crates/cgp-error-std/src/impls/raise_boxed.rs new file mode 100644 index 0000000..9b8f262 --- /dev/null +++ b/crates/cgp-error-std/src/impls/raise_boxed.rs @@ -0,0 +1,18 @@ +use core::error::Error as StdError; + +use cgp_core::error::ErrorRaiser; +use cgp_core::prelude::*; + +use crate::types::Error; + +pub struct RaiseBoxedStdError; + +impl ErrorRaiser for RaiseBoxedStdError +where + Context: HasErrorType, + E: StdError + Send + Sync + 'static, +{ + fn raise_error(e: E) -> Error { + e.into() + } +} diff --git a/crates/cgp-error-std/src/impls/use_boxed.rs b/crates/cgp-error-std/src/impls/use_boxed.rs new file mode 100644 index 0000000..01e2385 --- /dev/null +++ b/crates/cgp-error-std/src/impls/use_boxed.rs @@ -0,0 +1,9 @@ +use cgp_core::error::ProvideErrorType; + +use crate::types::Error; + +pub struct UseBoxedStdError; + +impl ProvideErrorType for UseBoxedStdError { + type Error = Error; +} diff --git a/crates/cgp-error-std/src/lib.rs b/crates/cgp-error-std/src/lib.rs index 3a1f811..053cd5d 100644 --- a/crates/cgp-error-std/src/lib.rs +++ b/crates/cgp-error-std/src/lib.rs @@ -2,29 +2,8 @@ extern crate alloc; -use alloc::boxed::Box; -use core::error::Error as StdError; +pub mod impls; +pub mod types; -use cgp_core::error::{ErrorRaiser, ProvideErrorType}; -use cgp_core::prelude::*; - -pub type Error = Box; - -pub struct HandleErrorsWithStdError; - -impl ProvideErrorType for HandleErrorsWithStdError -where - Context: Async, -{ - type Error = Error; -} - -impl ErrorRaiser for HandleErrorsWithStdError -where - Context: HasErrorType, - E: StdError + Send + Sync + 'static, -{ - fn raise_error(e: E) -> Error { - e.into() - } -} +pub use impls::*; +pub use types::*; diff --git a/crates/cgp-error-std/src/types/error.rs b/crates/cgp-error-std/src/types/error.rs new file mode 100644 index 0000000..728c33f --- /dev/null +++ b/crates/cgp-error-std/src/types/error.rs @@ -0,0 +1,4 @@ +use alloc::boxed::Box; +use core::error::Error as StdError; + +pub type Error = Box; diff --git a/crates/cgp-error-std/src/types/mod.rs b/crates/cgp-error-std/src/types/mod.rs new file mode 100644 index 0000000..73715e6 --- /dev/null +++ b/crates/cgp-error-std/src/types/mod.rs @@ -0,0 +1,5 @@ +pub mod error; +pub mod string; + +pub use error::*; +pub use string::*; diff --git a/crates/cgp-error-std/src/types/string.rs b/crates/cgp-error-std/src/types/string.rs new file mode 100644 index 0000000..1a6fd6a --- /dev/null +++ b/crates/cgp-error-std/src/types/string.rs @@ -0,0 +1,28 @@ +use core::error::Error; +use core::fmt::{Debug, Display}; + +use alloc::string::String; + +pub struct StringError { + pub message: String, +} + +impl From for StringError { + fn from(message: String) -> Self { + Self { message } + } +} + +impl Debug for StringError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + Display::fmt(&self.message, f) + } +} + +impl Display for StringError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + Display::fmt(&self.message, f) + } +} + +impl Error for StringError {} diff --git a/crates/cgp-error/src/impls/debug_error.rs b/crates/cgp-error/src/impls/debug_error.rs new file mode 100644 index 0000000..aecf14f --- /dev/null +++ b/crates/cgp-error/src/impls/debug_error.rs @@ -0,0 +1,17 @@ +use alloc::format; +use alloc::string::String; +use core::fmt::Debug; + +use crate::traits::{CanRaiseError, ErrorRaiser}; + +pub struct DebugError; + +impl ErrorRaiser for DebugError +where + Context: CanRaiseError, + E: Debug, +{ + fn raise_error(e: E) -> Context::Error { + Context::raise_error(format!("{e:?}")) + } +} diff --git a/crates/cgp-error/src/impls/discard_detail.rs b/crates/cgp-error/src/impls/discard_detail.rs new file mode 100644 index 0000000..30aa7bd --- /dev/null +++ b/crates/cgp-error/src/impls/discard_detail.rs @@ -0,0 +1,12 @@ +use crate::traits::{ErrorWrapper, HasErrorType}; + +pub struct DiscardDetail; + +impl ErrorWrapper for DiscardDetail +where + Context: HasErrorType, +{ + fn wrap_error(error: Context::Error, _detail: Detail) -> Context::Error { + error + } +} diff --git a/crates/cgp-error/src/impls/display_error.rs b/crates/cgp-error/src/impls/display_error.rs new file mode 100644 index 0000000..96e0861 --- /dev/null +++ b/crates/cgp-error/src/impls/display_error.rs @@ -0,0 +1,17 @@ +use alloc::format; +use alloc::string::String; +use core::fmt::Display; + +use crate::traits::{CanRaiseError, ErrorRaiser}; + +pub struct DisplayError; + +impl ErrorRaiser for DisplayError +where + Context: CanRaiseError, + E: Display, +{ + fn raise_error(e: E) -> Context::Error { + Context::raise_error(format!("{e}")) + } +} diff --git a/crates/cgp-error/src/impls/infallible.rs b/crates/cgp-error/src/impls/infallible.rs new file mode 100644 index 0000000..f0e6f67 --- /dev/null +++ b/crates/cgp-error/src/impls/infallible.rs @@ -0,0 +1,14 @@ +use core::convert::Infallible; + +use crate::traits::{ErrorRaiser, HasErrorType}; + +pub struct RaiseInfallible; + +impl ErrorRaiser for RaiseInfallible +where + Context: HasErrorType, +{ + fn raise_error(e: Infallible) -> Context::Error { + match e {} + } +} diff --git a/crates/cgp-error/src/impls/mod.rs b/crates/cgp-error/src/impls/mod.rs new file mode 100644 index 0000000..521e465 --- /dev/null +++ b/crates/cgp-error/src/impls/mod.rs @@ -0,0 +1,15 @@ +pub mod debug_error; +pub mod discard_detail; +pub mod display_error; +pub mod infallible; +pub mod panic_error; +pub mod raise_from; +pub mod return_error; + +pub use debug_error::*; +pub use discard_detail::*; +pub use display_error::*; +pub use infallible::*; +pub use panic_error::*; +pub use raise_from::*; +pub use return_error::*; diff --git a/crates/cgp-error/src/impls/panic_error.rs b/crates/cgp-error/src/impls/panic_error.rs new file mode 100644 index 0000000..b9d78d4 --- /dev/null +++ b/crates/cgp-error/src/impls/panic_error.rs @@ -0,0 +1,15 @@ +use core::fmt::Debug; + +use crate::traits::{ErrorRaiser, HasErrorType}; + +pub struct PanicOnError; + +impl ErrorRaiser for PanicOnError +where + Context: HasErrorType, + E: Debug, +{ + fn raise_error(e: E) -> Context::Error { + panic!("{e:?}") + } +} diff --git a/crates/cgp-error/src/impls/raise_from.rs b/crates/cgp-error/src/impls/raise_from.rs new file mode 100644 index 0000000..21bf3f0 --- /dev/null +++ b/crates/cgp-error/src/impls/raise_from.rs @@ -0,0 +1,13 @@ +use crate::traits::{ErrorRaiser, HasErrorType}; + +pub struct RaiseFrom; + +impl ErrorRaiser for RaiseFrom +where + Context: HasErrorType, + Context::Error: From, +{ + fn raise_error(e: E) -> Context::Error { + e.into() + } +} diff --git a/crates/cgp-error/src/impls/return_error.rs b/crates/cgp-error/src/impls/return_error.rs new file mode 100644 index 0000000..47ff60a --- /dev/null +++ b/crates/cgp-error/src/impls/return_error.rs @@ -0,0 +1,12 @@ +use crate::traits::{ErrorRaiser, HasErrorType}; + +pub struct ReturnError; + +impl ErrorRaiser for ReturnError +where + Context: HasErrorType, +{ + fn raise_error(e: E) -> E { + e + } +} diff --git a/crates/cgp-error/src/lib.rs b/crates/cgp-error/src/lib.rs index 9d739d9..21d836f 100644 --- a/crates/cgp-error/src/lib.rs +++ b/crates/cgp-error/src/lib.rs @@ -1,5 +1,8 @@ -mod can_raise_error; -mod has_error_type; +#![no_std] -pub use can_raise_error::*; -pub use has_error_type::*; +extern crate alloc; + +pub mod impls; +pub mod traits; + +pub use traits::*; diff --git a/crates/cgp-error/src/can_raise_error.rs b/crates/cgp-error/src/traits/can_raise_error.rs similarity index 94% rename from crates/cgp-error/src/can_raise_error.rs rename to crates/cgp-error/src/traits/can_raise_error.rs index 2d75329..21318d9 100644 --- a/crates/cgp-error/src/can_raise_error.rs +++ b/crates/cgp-error/src/traits/can_raise_error.rs @@ -1,7 +1,7 @@ use cgp_component::{DelegateComponent, HasComponents, UseDelegate}; use cgp_component_macro::cgp_component; -use crate::has_error_type::HasErrorType; +use crate::traits::has_error_type::HasErrorType; /** Used for injecting external error types into [`Self::Error`](HasErrorType::Error). diff --git a/crates/cgp-error/src/traits/can_wrap_error.rs b/crates/cgp-error/src/traits/can_wrap_error.rs new file mode 100644 index 0000000..d92ade0 --- /dev/null +++ b/crates/cgp-error/src/traits/can_wrap_error.rs @@ -0,0 +1,22 @@ +use cgp_component::{DelegateComponent, HasComponents, UseDelegate}; +use cgp_component_macro::cgp_component; + +use crate::traits::HasErrorType; + +#[cgp_component { + provider: ErrorWrapper, +}] +pub trait CanWrapError: HasErrorType { + fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error; +} + +impl ErrorWrapper for UseDelegate +where + Context: HasErrorType, + Components: DelegateComponent, + Components::Delegate: ErrorWrapper, +{ + fn wrap_error(error: Context::Error, detail: Detail) -> Context::Error { + Components::Delegate::wrap_error(error, detail) + } +} diff --git a/crates/cgp-error/src/traits/has_async_error_type.rs b/crates/cgp-error/src/traits/has_async_error_type.rs new file mode 100644 index 0000000..9aca406 --- /dev/null +++ b/crates/cgp-error/src/traits/has_async_error_type.rs @@ -0,0 +1,7 @@ +use cgp_async::Async; + +use crate::traits::HasErrorType; + +pub trait HasAsyncErrorType: HasErrorType {} + +impl HasAsyncErrorType for Context where Context: HasErrorType {} diff --git a/crates/cgp-error/src/has_error_type.rs b/crates/cgp-error/src/traits/has_error_type.rs similarity index 97% rename from crates/cgp-error/src/has_error_type.rs rename to crates/cgp-error/src/traits/has_error_type.rs index c0aba7c..53f483d 100644 --- a/crates/cgp-error/src/has_error_type.rs +++ b/crates/cgp-error/src/traits/has_error_type.rs @@ -27,7 +27,7 @@ pub trait HasErrorType { This is to allow `Self::Error` to be used in calls like `.unwrap()`, as well as for simpler error logging. */ - type Error: Async + Debug; + type Error: Debug; } pub type ErrorOf = ::Error; diff --git a/crates/cgp-error/src/traits/mod.rs b/crates/cgp-error/src/traits/mod.rs new file mode 100644 index 0000000..9a55bfa --- /dev/null +++ b/crates/cgp-error/src/traits/mod.rs @@ -0,0 +1,9 @@ +pub mod can_raise_error; +pub mod can_wrap_error; +pub mod has_async_error_type; +pub mod has_error_type; + +pub use can_raise_error::*; +pub use can_wrap_error::*; +pub use has_async_error_type::*; +pub use has_error_type::*; diff --git a/crates/cgp-extra/src/lib.rs b/crates/cgp-extra/src/lib.rs index d3f186b..e843b06 100644 --- a/crates/cgp-extra/src/lib.rs +++ b/crates/cgp-extra/src/lib.rs @@ -1 +1,3 @@ +#![no_std] + pub use cgp_run as run; diff --git a/crates/cgp-field-macro-lib/src/field.rs b/crates/cgp-field-macro-lib/src/field.rs index 602d44a..1c4fdae 100644 --- a/crates/cgp-field-macro-lib/src/field.rs +++ b/crates/cgp-field-macro-lib/src/field.rs @@ -1,3 +1,5 @@ +use alloc::string::ToString; +use alloc::vec::Vec; use proc_macro2::TokenStream; use quote::ToTokens; use syn::{parse_quote, Fields, ItemImpl, ItemStruct}; diff --git a/crates/cgp-field-macro-lib/src/lib.rs b/crates/cgp-field-macro-lib/src/lib.rs index 2e3fa92..bcb7b89 100644 --- a/crates/cgp-field-macro-lib/src/lib.rs +++ b/crates/cgp-field-macro-lib/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + /*! This is an internal crate used by the `cgp-field-macro` crate. We implement the proc macros for `cgp-field` as a library, so that it can be more easily tested. @@ -5,6 +7,8 @@ which is defined as a proc macro crate. */ +extern crate alloc; + pub mod field; pub mod product; pub mod symbol; diff --git a/crates/cgp-field-macro-lib/src/tests/helper/format.rs b/crates/cgp-field-macro-lib/src/tests/helper/format.rs index 2c96595..2e23f5a 100644 --- a/crates/cgp-field-macro-lib/src/tests/helper/format.rs +++ b/crates/cgp-field-macro-lib/src/tests/helper/format.rs @@ -1,3 +1,4 @@ +use alloc::string::{String, ToString}; use prettyplease::unparse; use proc_macro2::TokenStream; use syn::parse_file; diff --git a/crates/cgp-field-macro-lib/src/tests/product.rs b/crates/cgp-field-macro-lib/src/tests/product.rs index 9271f21..95f79f8 100644 --- a/crates/cgp-field-macro-lib/src/tests/product.rs +++ b/crates/cgp-field-macro-lib/src/tests/product.rs @@ -1,3 +1,4 @@ +use alloc::string::ToString; use quote::quote; use crate::product::{make_product_expr, make_product_type, make_sum_type}; diff --git a/crates/cgp-field-macro/src/lib.rs b/crates/cgp-field-macro/src/lib.rs index 5eb6858..2ecaa83 100644 --- a/crates/cgp-field-macro/src/lib.rs +++ b/crates/cgp-field-macro/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + extern crate proc_macro; use proc_macro::TokenStream; diff --git a/crates/cgp-sync/src/lib.rs b/crates/cgp-sync/src/lib.rs index b8c57ee..13ca9d0 100644 --- a/crates/cgp-sync/src/lib.rs +++ b/crates/cgp-sync/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + pub use cgp_async_macro::strip_async as async_trait; pub trait Async: Sized + 'static {} diff --git a/crates/cgp/src/lib.rs b/crates/cgp/src/lib.rs index 650a3c2..035bc24 100644 --- a/crates/cgp/src/lib.rs +++ b/crates/cgp/src/lib.rs @@ -1,2 +1,4 @@ +#![no_std] + pub use cgp_core::prelude; pub use {cgp_core as core, cgp_extra as extra};