diff --git a/crates/core/cgp-base-types/README.md b/crates/core/cgp-base-types/README.md index e3231a17..69d4aa3a 100644 --- a/crates/core/cgp-base-types/README.md +++ b/crates/core/cgp-base-types/README.md @@ -1,13 +1,36 @@ # cgp-base-types -`cgp-base-types` provides the type-level primitives of Context-Generic Programming (CGP): the -symbols that name fields, the lists and sums that describe a type's fields and variants, and the -paths that namespaces route along. +[Context-Generic Programming (CGP)](https://contextgeneric.dev/) selects trait implementations at +compile time through types, so the names it works with are types too: a field's name is a type-level +string, and a list of fields is a type-level list. `cgp-base-types` provides those primitives: the +symbols, the lists and sums that describe a type's fields and variants, and the paths that +namespaces route along. A `Product!` list, for example, is a chain of `Cons` cells closed by `Nil`: -The `cgp` crate re-exports these types under `cgp::core::base`, and the macros that build them, such -as `Symbol!` and `Product!`, under `cgp::prelude`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::core::field::types::{Cons, Nil}; +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +// `Product![u8, String]` is the list `Cons>`. +fn as_cells(list: Product![u8, String]) -> Cons> { + list +} + +fn main() { + let Cons(first, Cons(second, Nil)) = as_cells(product![1, "one".to_owned()]); + assert_eq!((first, second.as_str()), (1, "one")); +} +``` + +Projects reach these types through the `cgp` crate, which re-exports them under `cgp::core::base` +and the macros that build them under `cgp::prelude`, so they depend on +[`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's reference has a page +for each of its main items: [`Symbol!`](https://contextgeneric.dev/docs/reference/macros/symbol), +[`Cons`](https://contextgeneric.dev/docs/reference/types/cons), +[`Nil`](https://contextgeneric.dev/docs/reference/types/nil), +[`Chars`](https://contextgeneric.dev/docs/reference/types/chars), +[`PathCons`](https://contextgeneric.dev/docs/reference/types/path_cons), +[`StaticFormat`](https://contextgeneric.dev/docs/reference/traits/formatting/static_format), and +[`ConcatPath`](https://contextgeneric.dev/docs/reference/traits/formatting/concat_path). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/core/cgp-base-types/src/lib.rs b/crates/core/cgp-base-types/src/lib.rs index 25df6979..72930919 100644 --- a/crates/core/cgp-base-types/src/lib.rs +++ b/crates/core/cgp-base-types/src/lib.rs @@ -1,6 +1,4 @@ -//! This crate provides CGP's type-level primitives: symbols, lists, sums, and paths. -//! -//! See [`Symbol!`](https://contextgeneric.dev/docs/reference/macros/symbol), [`Cons`](https://contextgeneric.dev/docs/reference/types/cons), [`Nil`](https://contextgeneric.dev/docs/reference/types/nil), [`Chars`](https://contextgeneric.dev/docs/reference/types/chars), [`PathCons`](https://contextgeneric.dev/docs/reference/types/path_cons), [`StaticFormat`](https://contextgeneric.dev/docs/reference/traits/formatting/static_format), and [`ConcatPath`](https://contextgeneric.dev/docs/reference/traits/formatting/concat_path). +#![doc = include_str!("../README.md")] #![no_std] #![warn(missing_docs)] diff --git a/crates/core/cgp-component/README.md b/crates/core/cgp-component/README.md index 48184bfe..c1d48f08 100644 --- a/crates/core/cgp-component/README.md +++ b/crates/core/cgp-component/README.md @@ -1,12 +1,48 @@ # cgp-component -`cgp-component` provides the component machinery of Context-Generic Programming (CGP): the -delegation traits that let a context choose a provider for each component, the lookup that finds -that provider, and the providers CGP ships with. +In [Context-Generic Programming (CGP)](https://contextgeneric.dev/), each context chooses, at +compile time, which implementation of a trait it uses. `cgp-component` provides the machinery behind +that choice: the delegation traits that record which provider a context chose for each component, +the lookup that reads the choice, and the providers CGP ships with. The choice is recorded in a +type-level table, which the lookup reads: -The `cgp` crate re-exports it as `cgp::core::component`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::core::component::DelegateComponent; +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[cgp_component(Greeter)] +pub trait CanGreet { + fn greet(&self) -> String; +} + +#[cgp_impl(new GreetHello)] +impl Greeter { + fn greet(&self) -> String { + "Hello!".to_owned() + } +} + +pub struct Person; + +delegate_components! { Person { GreeterComponent: GreetHello } } + +fn main() { + // The wiring is a type-level table: this entry names `GreetHello`. + let _provider: >::Delegate = GreetHello; + assert_eq!(Person.greet(), "Hello!"); +} +``` + +Projects reach this machinery through the `cgp` crate, which re-exports it as +`cgp::core::component`, so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the +example does. The website's reference has a page for each of its main items: +[`DelegateComponent`](https://contextgeneric.dev/docs/reference/traits/wiring/delegate_component), +[`IsProviderFor`](https://contextgeneric.dev/docs/reference/traits/wiring/is_provider_for), +[`CanUseComponent`](https://contextgeneric.dev/docs/reference/traits/wiring/can_use_component), +[`UseContext`](https://contextgeneric.dev/docs/reference/providers/use_context), +[`UseDefault`](https://contextgeneric.dev/docs/reference/providers/use_default), +[`UseDelegate`](https://contextgeneric.dev/docs/reference/providers/use_delegate), and +[`RedirectLookup`](https://contextgeneric.dev/docs/reference/providers/redirect_lookup). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/core/cgp-component/src/lib.rs b/crates/core/cgp-component/src/lib.rs index 21743b0c..2cce7a8e 100644 --- a/crates/core/cgp-component/src/lib.rs +++ b/crates/core/cgp-component/src/lib.rs @@ -1,8 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's component machinery: delegation, provider lookup, and the built-in -//! providers. -//! -//! See [`DelegateComponent`](https://contextgeneric.dev/docs/reference/traits/wiring/delegate_component), [`IsProviderFor`](https://contextgeneric.dev/docs/reference/traits/wiring/is_provider_for), [`CanUseComponent`](https://contextgeneric.dev/docs/reference/traits/wiring/can_use_component), [`UseContext`](https://contextgeneric.dev/docs/reference/providers/use_context), [`UseDefault`](https://contextgeneric.dev/docs/reference/providers/use_default), [`UseDelegate`](https://contextgeneric.dev/docs/reference/providers/use_delegate), and [`RedirectLookup`](https://contextgeneric.dev/docs/reference/providers/redirect_lookup). #![warn(missing_docs)] /// Re-exports the component machinery that the macros expand to. diff --git a/crates/core/cgp-error/README.md b/crates/core/cgp-error/README.md index 515e5068..cdd6c700 100644 --- a/crates/core/cgp-error/README.md +++ b/crates/core/cgp-error/README.md @@ -1,12 +1,46 @@ # cgp-error -`cgp-error` provides the error components of Context-Generic Programming (CGP): an error type that -each context chooses, and the components that raise errors into it and wrap them with detail. +[Context-Generic Programming (CGP)](https://contextgeneric.dev/) lets each context choose how a +trait is implemented, and its error handling extends that choice to the error type itself. +`cgp-error` provides the components behind it: the abstract error type, and the components that +raise errors into it and wrap them with detail. Generic code raises an error without knowing its +type, and each context decides what it becomes: -The `cgp` crate re-exports it as `cgp::core::error`, and its traits `HasErrorType`, `CanRaiseError`, -and `CanWrapError` under `cgp::prelude`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::core::error::{ErrorRaiserComponent, ErrorTypeProviderComponent}; +use cgp::extra::error::RaiseFrom; +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +pub fn parse_port(input: &str) -> Result +where + Context: CanRaiseError, +{ + input.parse().map_err(|_| Context::raise_error(format!("not a port: {input}"))) +} + +pub struct App; + +delegate_components! { + App { + open ErrorRaiserComponent; + + ErrorTypeProviderComponent: UseType, + @ErrorRaiserComponent.String: RaiseFrom, + } +} + +fn main() { + assert_eq!(parse_port::("80"), Ok(80)); + assert_eq!(parse_port::("http"), Err("not a port: http".to_owned())); +} +``` + +Projects reach these components through the `cgp` crate, which re-exports them as +`cgp::core::error`, so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example +does. The website's reference has a page for each of its main items: +[`HasErrorType`](https://contextgeneric.dev/docs/reference/components/has_error_type), +[`CanRaiseError`](https://contextgeneric.dev/docs/reference/components/can_raise_error), and +[`CanWrapError`](https://contextgeneric.dev/docs/reference/components/can_wrap_error). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/core/cgp-error/src/lib.rs b/crates/core/cgp-error/src/lib.rs index 7304fd50..f866138d 100644 --- a/crates/core/cgp-error/src/lib.rs +++ b/crates/core/cgp-error/src/lib.rs @@ -1,7 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's error components: the abstract error type, raising, and wrapping. -//! -//! See [`HasErrorType`](https://contextgeneric.dev/docs/reference/components/has_error_type), [`CanRaiseError`](https://contextgeneric.dev/docs/reference/components/can_raise_error), and [`CanWrapError`](https://contextgeneric.dev/docs/reference/components/can_wrap_error). #![warn(missing_docs)] mod contexts; diff --git a/crates/core/cgp-field/README.md b/crates/core/cgp-field/README.md index cce45538..1512345f 100644 --- a/crates/core/cgp-field/README.md +++ b/crates/core/cgp-field/README.md @@ -1,13 +1,34 @@ # cgp-field -`cgp-field` provides the field access of Context-Generic Programming (CGP): the traits that read and -build a type's fields by name, the tags that name them, and the structural casts between records and -between enums. +A [Context-Generic Programming (CGP)](https://contextgeneric.dev/) provider reads the values it +needs from its context's fields by name, so it fits any context that has those fields. `cgp-field` +provides that field access: the traits that read and build a type's fields by name, the tags that +name them, and the structural casts between records and between enums. `#[derive(HasField)]` gives +each field a tag, and the field can then be read by name: -The `cgp` crate re-exports it as `cgp::core::field`, and the derives that implement its traits under -`cgp::prelude`. Most projects therefore depend on [`cgp`](https://crates.io/crates/cgp) alone, -without adding this crate to their manifest. +```rust,ignore +use cgp::core::field::traits::HasField; +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[derive(HasField)] +pub struct Person { + pub name: String, + pub age: u8, +} + +fn main() { + let person = Person { name: "Alice".to_owned(), age: 30 }; + assert_eq!(person.get_field(PhantomData::), &30); +} +``` + +Projects reach this field access through the `cgp` crate, which re-exports it as `cgp::core::field`, +so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's +reference has a page for each of its main items: +[`HasField`](https://contextgeneric.dev/docs/reference/traits/field-access/has_field), +[`Index`](https://contextgeneric.dev/docs/reference/types/index_type), +[`MRef`](https://contextgeneric.dev/docs/reference/types/mref), and the +[casts](https://contextgeneric.dev/docs/reference/traits/casting/). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/core/cgp-field/src/lib.rs b/crates/core/cgp-field/src/lib.rs index 7e3dad28..cd65b4ec 100644 --- a/crates/core/cgp-field/src/lib.rs +++ b/crates/core/cgp-field/src/lib.rs @@ -1,8 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's field access: the field traits, the field tags, the type-level lists, -//! and the structural casts. -//! -//! See [`HasField`](https://contextgeneric.dev/docs/reference/traits/field-access/has_field), [`Index`](https://contextgeneric.dev/docs/reference/types/index_type), [`MRef`](https://contextgeneric.dev/docs/reference/types/mref), and the [casts](https://contextgeneric.dev/docs/reference/traits/casting/). #![warn(missing_docs)] /// The field traits' implementations and the built-in providers. diff --git a/crates/core/cgp-type/README.md b/crates/core/cgp-type/README.md index e5c6eee0..96f11929 100644 --- a/crates/core/cgp-type/README.md +++ b/crates/core/cgp-type/README.md @@ -1,11 +1,34 @@ # cgp-type -`cgp-type` provides the abstract types of Context-Generic Programming (CGP), which let each context -choose a type that generic code names only through a trait. +[Context-Generic Programming (CGP)](https://contextgeneric.dev/) lets each context choose a trait's +implementation at compile time, and it can choose a type the same way. `cgp-type` provides these +abstract types, which generic code names only through a trait while each context picks the concrete +type in its wiring: -The `cgp` crate re-exports it as `cgp::core::types`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[cgp_type] +pub trait HasNameType { + type Name; +} + +pub struct App; + +delegate_components! { App { NameTypeProviderComponent: UseType } } + +fn main() { + let name: ::Name = String::from("app"); + assert_eq!(name, "app"); +} +``` + +Projects reach these types through the `cgp` crate, which re-exports them as `cgp::core::types`, so +they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's +reference has a page for each of its main items: +[`HasType`](https://contextgeneric.dev/docs/reference/components/has_type), +[`UseType`](https://contextgeneric.dev/docs/reference/providers/use_type), and +[`UseDelegatedType`](https://contextgeneric.dev/docs/reference/providers/use_delegated_type). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/core/cgp-type/src/lib.rs b/crates/core/cgp-type/src/lib.rs index fd135862..f7e8080e 100644 --- a/crates/core/cgp-type/src/lib.rs +++ b/crates/core/cgp-type/src/lib.rs @@ -1,7 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's abstract types: a type the context chooses behind a trait. -//! -//! See [`HasType`](https://contextgeneric.dev/docs/reference/components/has_type), [`UseType`](https://contextgeneric.dev/docs/reference/providers/use_type), and [`UseDelegatedType`](https://contextgeneric.dev/docs/reference/providers/use_delegated_type). #![warn(missing_docs)] mod impls; diff --git a/crates/extra/cgp-dispatch/README.md b/crates/extra/cgp-dispatch/README.md index 8b915ad7..736a5fed 100644 --- a/crates/extra/cgp-dispatch/README.md +++ b/crates/extra/cgp-dispatch/README.md @@ -1,12 +1,66 @@ # cgp-dispatch -`cgp-dispatch` provides the dispatch combinators of Context-Generic Programming (CGP), which split a -value between handlers. They match an enum's variants or extract a record's fields and pass each one -to its own handler, or build a record from what the handlers return. +In [Context-Generic Programming (CGP)](https://contextgeneric.dev/), a computation can be a +component like any other, and `cgp-dispatch` provides the combinators that split a value between +several of them: they match an enum's variants or extract a record's fields and pass each part to +its own handler, or build a record from what the handlers return. Here each variant of an enum gets +its own area computation: -The `cgp` crate re-exports it as `cgp::extra::dispatch`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::extra::dispatch::MatchWithValueHandlers; +use cgp::extra::handler::{CanCompute, Computer, ComputerComponent}; +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +pub struct Circle { + pub radius: f64, +} + +pub struct Rectangle { + pub width: f64, + pub height: f64, +} + +#[derive(CgpData)] +pub enum Shape { + Circle(Circle), + Rectangle(Rectangle), +} + +#[cgp_new_provider] +impl Computer for ComputeArea { + type Output = f64; + + fn compute(_context: &Context, _code: PhantomData, circle: Circle) -> f64 { + 3.0 * circle.radius * circle.radius + } +} + +#[cgp_provider] +impl Computer for ComputeArea { + type Output = f64; + + fn compute(_context: &Context, _code: PhantomData, rectangle: Rectangle) -> f64 { + rectangle.width * rectangle.height + } +} + +pub struct App; + +delegate_components! { App { ComputerComponent: MatchWithValueHandlers } } + +fn main() { + let shape = Shape::Rectangle(Rectangle { width: 2.0, height: 3.0 }); + assert_eq!(App.compute(PhantomData::<()>, shape), 6.0); +} +``` + +Projects reach these combinators through the `cgp` crate, which re-exports them as +`cgp::extra::dispatch`, so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the +example does. The website's reference has a page for each of its main items: +[`MatchWithHandlers`](https://contextgeneric.dev/docs/reference/providers/dispatch/match_with_handlers), +[`MatchWithValueHandlers`](https://contextgeneric.dev/docs/reference/providers/dispatch/match_with_value_handlers), +and +[`ExtractFieldAndHandle`](https://contextgeneric.dev/docs/reference/providers/dispatch/extract_field_and_handle). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/extra/cgp-dispatch/src/lib.rs b/crates/extra/cgp-dispatch/src/lib.rs index 7db2541e..18f84dd8 100644 --- a/crates/extra/cgp-dispatch/src/lib.rs +++ b/crates/extra/cgp-dispatch/src/lib.rs @@ -1,9 +1,6 @@ +#![doc = include_str!("../README.md")] #![no_std] #![allow(clippy::needless_lifetimes)] -//! This crate provides CGP's dispatch providers, which match, extract, and build a value on the way -//! to a handler. -//! -//! See [`MatchWithHandlers`](https://contextgeneric.dev/docs/reference/providers/dispatch/match_with_handlers), [`MatchWithValueHandlers`](https://contextgeneric.dev/docs/reference/providers/dispatch/match_with_value_handlers), and [`ExtractFieldAndHandle`](https://contextgeneric.dev/docs/reference/providers/dispatch/extract_field_and_handle). #![warn(missing_docs)] mod providers; diff --git a/crates/extra/cgp-error-extra/README.md b/crates/extra/cgp-error-extra/README.md index 7dc8d03a..74e9f176 100644 --- a/crates/extra/cgp-error-extra/README.md +++ b/crates/extra/cgp-error-extra/README.md @@ -1,13 +1,49 @@ # cgp-error-extra -`cgp-error-extra` provides the generic error providers of Context-Generic Programming (CGP), which -raise an error by conversion, return it unchanged, discard a detail, or format it into a message. -The `DebugError` and `DisplayError` providers need an allocator, so they sit behind the `alloc` -feature. +[Context-Generic Programming (CGP)](https://contextgeneric.dev/) lets each context choose its error +type, and how each kind of error is raised into it. `cgp-error-extra` provides generic providers for +that choice: they raise an error by conversion, return it unchanged, discard a detail, or format it +into a message, and a context routes each source error to one of them: -The `cgp` crate re-exports it as `cgp::extra::error`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use core::num::ParseIntError; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +use cgp::core::error::{ErrorRaiserComponent, ErrorTypeProviderComponent}; +use cgp::extra::error::{DebugError, RaiseFrom}; +use cgp::prelude::*; + +pub struct App; + +delegate_components! { + App { + open ErrorRaiserComponent; + + ErrorTypeProviderComponent: UseType, + @ErrorRaiserComponent.String: RaiseFrom, + @ErrorRaiserComponent.ParseIntError: DebugError, + } +} + +fn main() { + let error = "http".parse::().map_err(App::raise_error).unwrap_err(); + assert!(error.contains("InvalidDigit")); +} +``` + +`DebugError` and `DisplayError` format an error into a `String`, so they need an allocator and the +`alloc` feature, which this example turns on with +`cgp = { version = "0.8.0", features = ["alloc"] }`. + +Projects reach these providers through the `cgp` crate, which re-exports them as +`cgp::extra::error`, so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example +does. The website's reference has a page for each of its main items: +[`RaiseFrom`](https://contextgeneric.dev/docs/reference/providers/error/raise_from), +[`ReturnError`](https://contextgeneric.dev/docs/reference/providers/error/return_error), +[`RaiseInfallible`](https://contextgeneric.dev/docs/reference/providers/error/raise_infallible), +[`PanicOnError`](https://contextgeneric.dev/docs/reference/providers/error/panic_on_error), +[`DiscardDetail`](https://contextgeneric.dev/docs/reference/providers/error/discard_detail), +[`DebugError`](https://contextgeneric.dev/docs/reference/providers/error/debug_error), and +[`DisplayError`](https://contextgeneric.dev/docs/reference/providers/error/display_error). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/extra/cgp-error-extra/src/lib.rs b/crates/extra/cgp-error-extra/src/lib.rs index 1c05b414..e4605570 100644 --- a/crates/extra/cgp-error-extra/src/lib.rs +++ b/crates/extra/cgp-error-extra/src/lib.rs @@ -1,9 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's generic error providers, which raise, return, discard, and format an -//! error. The `DebugError` and `DisplayError` providers need an allocator, so they are behind the -//! `alloc` feature. -//! -//! See [`RaiseFrom`](https://contextgeneric.dev/docs/reference/providers/error/raise_from), [`ReturnError`](https://contextgeneric.dev/docs/reference/providers/error/return_error), [`RaiseInfallible`](https://contextgeneric.dev/docs/reference/providers/error/raise_infallible), [`PanicOnError`](https://contextgeneric.dev/docs/reference/providers/error/panic_on_error), [`DiscardDetail`](https://contextgeneric.dev/docs/reference/providers/error/discard_detail), [`DebugError`](https://contextgeneric.dev/docs/reference/providers/error/debug_error), and [`DisplayError`](https://contextgeneric.dev/docs/reference/providers/error/display_error). #![warn(missing_docs)] #[cfg(feature = "alloc")] diff --git a/crates/extra/cgp-field-extra/README.md b/crates/extra/cgp-field-extra/README.md index 0bb4645d..9ffaf1ba 100644 --- a/crates/extra/cgp-field-extra/README.md +++ b/crates/extra/cgp-field-extra/README.md @@ -1,12 +1,41 @@ # cgp-field-extra -`cgp-field-extra` provides the optional-field layer of Context-Generic Programming (CGP), which -builds a value from fields set in any order. The builder finishes once every field is set, or fills -the missing ones from their defaults. +[Context-Generic Programming (CGP)](https://contextgeneric.dev/) can build a record field by field, +so separate pieces of code can each supply part of a value. `cgp-field-extra` provides the +optional-field layer for that: it builds a value from fields set in any order, then finishes once +every field is set, or fills the missing ones from their defaults. A record can be widened into a +larger one this way: -The `cgp` crate re-exports it as `cgp::extra::field`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::extra::field::impls::CanBuildWithDefault; +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[derive(Debug, PartialEq, CgpData)] +pub struct Point2d { + pub x: u64, + pub y: u64, +} + +#[derive(Debug, PartialEq, CgpData)] +pub struct Point3d { + pub x: u64, + pub y: u64, + pub z: u64, +} + +fn main() { + let point = Point3d::build_with_default(Point2d { x: 1, y: 2 }); + assert_eq!(point, Point3d { x: 1, y: 2, z: 0 }); +} +``` + +Projects reach this layer through the `cgp` crate, which re-exports it as `cgp::extra::field`, so +they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's +reference has a page for each of its main items: +[`HasOptionalBuilder`](https://contextgeneric.dev/docs/reference/traits/optional/has_optional_builder), +[`ToOptional`](https://contextgeneric.dev/docs/reference/traits/optional/to_optional), +[`SetOptional`](https://contextgeneric.dev/docs/reference/traits/optional/set_optional), and +[`FinalizeOptional`](https://contextgeneric.dev/docs/reference/traits/optional/finalize_optional). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/extra/cgp-field-extra/src/lib.rs b/crates/extra/cgp-field-extra/src/lib.rs index dcf6604a..69c01d90 100644 --- a/crates/extra/cgp-field-extra/src/lib.rs +++ b/crates/extra/cgp-field-extra/src/lib.rs @@ -1,7 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's optional-field layer, for a builder whose fields are set in any order. -//! -//! See [`HasOptionalBuilder`](https://contextgeneric.dev/docs/reference/traits/optional/has_optional_builder), [`ToOptional`](https://contextgeneric.dev/docs/reference/traits/optional/to_optional), [`SetOptional`](https://contextgeneric.dev/docs/reference/traits/optional/set_optional), and [`FinalizeOptional`](https://contextgeneric.dev/docs/reference/traits/optional/finalize_optional). #![warn(missing_docs)] /// The optional-field traits' implementations. diff --git a/crates/extra/cgp-handler/README.md b/crates/extra/cgp-handler/README.md index adc3ab1b..681ac792 100644 --- a/crates/extra/cgp-handler/README.md +++ b/crates/extra/cgp-handler/README.md @@ -1,12 +1,39 @@ # cgp-handler -`cgp-handler` provides the handler family of Context-Generic Programming (CGP): the components that -model a computation, whether synchronous or async and fallible or not, and the combinators that -chain, nest, and promote them. +In [Context-Generic Programming (CGP)](https://contextgeneric.dev/), a computation can be a +component too, which each context wires like any other. `cgp-handler` provides that handler family: +components for computations synchronous or async and fallible or not, and the combinators that +chain, nest, and promote them. A provider of `Computer` computes an output from an input: -The `cgp` crate re-exports it as `cgp::extra::handler`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::extra::handler::{CanCompute, Computer, ComputerComponent}; +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[cgp_impl(new Double)] +impl Computer { + type Output = u64; + + fn compute(&self, _code: PhantomData, input: u64) -> u64 { + input * 2 + } +} + +pub struct App; + +delegate_components! { App { ComputerComponent: Double } } + +fn main() { + assert_eq!(App.compute(PhantomData::<()>, 21), 42); +} +``` + +Projects reach the handlers through the `cgp` crate, which re-exports them as `cgp::extra::handler`, +so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's +reference has a page for each of its main items: +[`CanCompute`](https://contextgeneric.dev/docs/reference/components/handler/computer), +[`CanHandle`](https://contextgeneric.dev/docs/reference/components/handler/handler), +[`PipeHandlers`](https://contextgeneric.dev/docs/reference/providers/handler/pipe_handlers), and +[`Promote`](https://contextgeneric.dev/docs/reference/providers/handler/promote). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/extra/cgp-handler/src/lib.rs b/crates/extra/cgp-handler/src/lib.rs index a0b36473..66b7bc89 100644 --- a/crates/extra/cgp-handler/src/lib.rs +++ b/crates/extra/cgp-handler/src/lib.rs @@ -1,8 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's handler family: the computation components and the combinators that -//! compose them. -//! -//! See [`CanCompute`](https://contextgeneric.dev/docs/reference/components/handler/computer), [`CanHandle`](https://contextgeneric.dev/docs/reference/components/handler/handler), [`PipeHandlers`](https://contextgeneric.dev/docs/reference/providers/handler/pipe_handlers), and [`Promote`](https://contextgeneric.dev/docs/reference/providers/handler/promote). #![warn(missing_docs)] mod components; diff --git a/crates/extra/cgp-monad/README.md b/crates/extra/cgp-monad/README.md index 1e6d1033..14c60dc6 100644 --- a/crates/extra/cgp-monad/README.md +++ b/crates/extra/cgp-monad/README.md @@ -1,12 +1,10 @@ # cgp-monad -`cgp-monad` provides the monadic handlers of Context-Generic Programming (CGP), which compose +`cgp-monad` provides the monadic handlers of Context-Generic Programming (CGP). They compose handlers through a monad that decides whether each step runs, such as one that stops a pipeline at -its first error. +its first error. Projects reach them through the `cgp` crate, which re-exports them as +`cgp::extra::monad`, so most depend on [`cgp`](https://crates.io/crates/cgp) alone and never add +this crate to their manifest. -The `cgp` crate re-exports it as `cgp::extra::monad`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/extra/cgp-run/README.md b/crates/extra/cgp-run/README.md index da870f42..e6a3e7a3 100644 --- a/crates/extra/cgp-run/README.md +++ b/crates/extra/cgp-run/README.md @@ -1,11 +1,9 @@ # cgp-run -`cgp-run` provides the task runners of Context-Generic Programming (CGP), the components that run a -task named by a type-level code. +`cgp-run` provides the task runners of Context-Generic Programming (CGP): the components that run a +task named by a type-level code. Projects reach them through the `cgp` crate, which re-exports them +as `cgp::extra::run`, so most depend on [`cgp`](https://crates.io/crates/cgp) alone and never add +this crate to their manifest. -The `cgp` crate re-exports it as `cgp::extra::run`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/extra/cgp-runtime/README.md b/crates/extra/cgp-runtime/README.md index 950982c3..4bfec3f5 100644 --- a/crates/extra/cgp-runtime/README.md +++ b/crates/extra/cgp-runtime/README.md @@ -1,11 +1,9 @@ # cgp-runtime `cgp-runtime` provides the runtime abstraction of Context-Generic Programming (CGP), which lets each -context choose its async runtime while generic code reaches it through a trait. +context choose its async runtime while generic code reaches it through a trait. Projects reach it +through the `cgp` crate, which re-exports it as `cgp::extra::runtime`, so most depend on +[`cgp`](https://crates.io/crates/cgp) alone and never add this crate to their manifest. -The `cgp` crate re-exports it as `cgp::extra::runtime`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-async-macro/README.md b/crates/macros/cgp-async-macro/README.md index 8b2db696..bcda0144 100644 --- a/crates/macros/cgp-async-macro/README.md +++ b/crates/macros/cgp-async-macro/README.md @@ -1,11 +1,35 @@ # cgp-async-macro -`cgp-async-macro` provides the `#[async_trait]` macro of Context-Generic Programming (CGP), which -rewrites each `async fn` in a trait to return `impl Future`, without boxing the future. +Many traits in [Context-Generic Programming (CGP)](https://contextgeneric.dev/) are async, and +`cgp-async-macro` provides the `#[async_trait]` macro for them, which rewrites each `async fn` in a +trait to return `impl Future` without boxing the future: -The `cgp` crate re-exports the macro under `cgp::prelude`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[async_trait] +pub trait CanFetch { + async fn fetch(&self, key: &str) -> String; +} + +pub struct Store; + +impl CanFetch for Store { + async fn fetch(&self, key: &str) -> String { + format!("value of {key}") + } +} + +fn main() { + // `fetch` returns `impl Future`, which any executor can run. + let _future = Store.fetch("port"); +} +``` + +Projects use the macro through `cgp::prelude`, so they depend on +[`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's reference page for +[`#[async_trait]`](https://contextgeneric.dev/docs/reference/macros/async_trait) describes it in +full. + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-async-macro/src/lib.rs b/crates/macros/cgp-async-macro/src/lib.rs index 8fe73621..2077f243 100644 --- a/crates/macros/cgp-async-macro/src/lib.rs +++ b/crates/macros/cgp-async-macro/src/lib.rs @@ -1,8 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's `#[async_trait]` macro, which rewrites a trait's `async fn` to return -//! `impl Future`. -//! -//! See [`#[async_trait]`](https://contextgeneric.dev/docs/reference/macros/async_trait). #![warn(missing_docs)] extern crate alloc; diff --git a/crates/macros/cgp-macro-core/README.md b/crates/macros/cgp-macro-core/README.md index c79404cf..5c69c14a 100644 --- a/crates/macros/cgp-macro-core/README.md +++ b/crates/macros/cgp-macro-core/README.md @@ -2,10 +2,7 @@ `cgp-macro-core` holds the shared core of the procedural macros of Context-Generic Programming (CGP): the syntax trees, parsers, visitors, and code-generation helpers that the macro crates build -on. +on. Projects use the macros through `cgp::prelude`, so they have no reason to depend on this crate. -Projects use the macros through `cgp::prelude` and have no reason to depend on this crate. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-macro-extra-core/README.md b/crates/macros/cgp-macro-extra-core/README.md index 7ddd2f35..e95906dd 100644 --- a/crates/macros/cgp-macro-extra-core/README.md +++ b/crates/macros/cgp-macro-extra-core/README.md @@ -2,10 +2,8 @@ `cgp-macro-extra-core` holds the shared core of the extra procedural macros of Context-Generic Programming (CGP): the syntax trees, parsers, and visitors behind `#[cgp_computer]`, -`#[cgp_producer]`, and `#[cgp_auto_dispatch]`. +`#[cgp_producer]`, and `#[cgp_auto_dispatch]`. Projects use the macros through `cgp::prelude`, so +they have no reason to depend on this crate. -Projects use the macros through `cgp::prelude` and have no reason to depend on this crate. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-macro-extra-lib/README.md b/crates/macros/cgp-macro-extra-lib/README.md index 1a80bfc1..581e3c97 100644 --- a/crates/macros/cgp-macro-extra-lib/README.md +++ b/crates/macros/cgp-macro-extra-lib/README.md @@ -1,10 +1,8 @@ # cgp-macro-extra-lib -`cgp-macro-extra-lib` implements the extra procedural macros of Context-Generic Programming (CGP) -that `cgp-macro-extra` exports: `#[cgp_computer]`, `#[cgp_producer]`, and `#[cgp_auto_dispatch]`. +`cgp-macro-extra-lib` implements the extra procedural macros of Context-Generic Programming (CGP), +`#[cgp_computer]`, `#[cgp_producer]`, and `#[cgp_auto_dispatch]`, which `cgp-macro-extra` exports. +Projects use the macros through `cgp::prelude`, so they have no reason to depend on this crate. -Projects use the macros through `cgp::prelude` and have no reason to depend on this crate. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-macro-extra/README.md b/crates/macros/cgp-macro-extra/README.md index 164c4b17..c8f23a8c 100644 --- a/crates/macros/cgp-macro-extra/README.md +++ b/crates/macros/cgp-macro-extra/README.md @@ -1,12 +1,55 @@ # cgp-macro-extra -`cgp-macro-extra` provides the extra procedural macros of Context-Generic Programming (CGP): -`#[cgp_computer]` and `#[cgp_producer]`, which define a handler provider from a function, and -`#[cgp_auto_dispatch]`, which implements a trait for an enum by dispatching to its variants. +In [Context-Generic Programming (CGP)](https://contextgeneric.dev/), each pluggable implementation +of a trait is a provider, and some providers follow a common pattern that `cgp-macro-extra` writes +for you: `#[cgp_computer]` and `#[cgp_producer]` define a handler provider from a function, and +`#[cgp_auto_dispatch]` implements a trait for an enum by dispatching to its variants, as here: -The `cgp` crate re-exports these macros under `cgp::prelude`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[cgp_auto_dispatch] +pub trait HasArea { + fn area(&self) -> f64; +} + +pub struct Circle { + pub radius: f64, +} + +impl HasArea for Circle { + fn area(&self) -> f64 { + 3.0 * self.radius * self.radius + } +} + +pub struct Square { + pub side: f64, +} + +impl HasArea for Square { + fn area(&self) -> f64 { + self.side * self.side + } +} + +#[derive(CgpData)] +pub enum Shape { + Circle(Circle), + Square(Square), +} + +fn main() { + assert_eq!(Shape::Square(Square { side: 2.0 }).area(), 4.0); +} +``` + +Projects use these macros through `cgp::prelude`, so they depend on +[`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's reference has a page +for each of its main items: +[`#[cgp_computer]`](https://contextgeneric.dev/docs/reference/macros/cgp_computer), +[`#[cgp_producer]`](https://contextgeneric.dev/docs/reference/macros/cgp_producer), and +[`#[cgp_auto_dispatch]`](https://contextgeneric.dev/docs/reference/macros/cgp_auto_dispatch). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-macro-extra/src/lib.rs b/crates/macros/cgp-macro-extra/src/lib.rs index ecc67c3e..923ee9a9 100644 --- a/crates/macros/cgp-macro-extra/src/lib.rs +++ b/crates/macros/cgp-macro-extra/src/lib.rs @@ -1,8 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's extra procedural macros, which define handler providers from functions -//! and dispatch a trait over an enum. -//! -//! See [`#[cgp_computer]`](https://contextgeneric.dev/docs/reference/macros/cgp_computer), [`#[cgp_producer]`](https://contextgeneric.dev/docs/reference/macros/cgp_producer), and [`#[cgp_auto_dispatch]`](https://contextgeneric.dev/docs/reference/macros/cgp_auto_dispatch). #![warn(missing_docs)] use proc_macro::TokenStream; diff --git a/crates/macros/cgp-macro-lib/README.md b/crates/macros/cgp-macro-lib/README.md index ccff7f6e..bf44f85a 100644 --- a/crates/macros/cgp-macro-lib/README.md +++ b/crates/macros/cgp-macro-lib/README.md @@ -1,10 +1,8 @@ # cgp-macro-lib `cgp-macro-lib` implements the procedural macros of Context-Generic Programming (CGP), one module -per macro. The `cgp-macro` crate exports them. +per macro, and the `cgp-macro` crate exports them. Projects use the macros through `cgp::prelude`, +so they have no reason to depend on this crate. -Projects use the macros through `cgp::prelude` and have no reason to depend on this crate. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-macro-test-util-lib/README.md b/crates/macros/cgp-macro-test-util-lib/README.md index 6bed6e4d..e0921b15 100644 --- a/crates/macros/cgp-macro-test-util-lib/README.md +++ b/crates/macros/cgp-macro-test-util-lib/README.md @@ -1,11 +1,8 @@ # cgp-macro-test-util-lib -`cgp-macro-test-util-lib` implements the snapshot-testing macros of Context-Generic Programming -(CGP) that `cgp-macro-test-util` exports. CGP's own tests use them to emit the code a CGP macro -generates and pin it as an inline snapshot. +`cgp-macro-test-util-lib` implements the snapshot-testing macros that `cgp-macro-test-util` exports. +The tests of Context-Generic Programming (CGP) use them to emit the code a CGP macro generates and +pin it as an inline snapshot, so a project that uses CGP has no reason to depend on this crate. -Projects that use CGP have no reason to depend on this crate. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-macro-test-util/README.md b/crates/macros/cgp-macro-test-util/README.md index 250f5e87..524b063e 100644 --- a/crates/macros/cgp-macro-test-util/README.md +++ b/crates/macros/cgp-macro-test-util/README.md @@ -4,29 +4,29 @@ **[Website](https://contextgeneric.dev/) · [Reference](https://contextgeneric.dev/docs/reference/) · [Implementation record](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/entrypoints/snapshot_macros.md)** -This crate exposes a family of `snapshot_*!` procedural macros that pin the code the -core CGP macros generate. Each one does **two** things at once: it emits the real -generated code into the surrounding module, exactly as the underlying CGP macro -would, so the traits, structs, and impls stay live and the rest of the module can -still wire them up and assert their runtime behavior, and it generates a `#[test]` -that asserts a pretty-printed string of that same code against an inline -[`insta`](https://insta.rs) snapshot. Adding or removing a snapshot therefore -changes only the golden assertion, never the compile-time or runtime coverage. - -Two documents carry what this README does not. The +The procedural macros of [Context-Generic Programming (CGP)](https://contextgeneric.dev/) generate +the traits and impls behind its pluggable trait implementations, and CGP's own test suite pins that +generated code with the `snapshot_*!` macros this crate provides. Each one emits the real generated +code into the surrounding module, exactly as the underlying CGP macro would, so the traits, structs, +and impls stay live and the rest of the module can still wire them and assert their behavior at run +time. It also generates a `#[test]` that compares a pretty-printed copy of the same code with an +inline [`insta`](https://insta.rs) snapshot. Adding or removing a snapshot therefore changes only +that comparison, never what compiles or runs. + +Two documents cover what this README leaves out. The [implementation document](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/entrypoints/snapshot_macros.md) -in the knowledge base explains how the macros are built and what an invocation -expands to; the [test suite's rules](https://github.com/contextgeneric/cgp/blob/main/crates/tests/README.md#snapshots) set the convention for -*when* to snapshot: a macro's expansion is pinned only in the concept target that -owns that macro's feature, and written plainly everywhere else. +in the knowledge base explains how the macros are built and what an invocation expands to. The +[test suite's rules](https://github.com/contextgeneric/cgp/blob/main/crates/tests/README.md#snapshots) +say *when* to snapshot: a macro's expansion is pinned only in the concept target that owns that +macro's feature, and written plainly everywhere else. ## Crate layout This is the proc-macro crate, a thin shell: every macro forwards to -[`cgp-macro-test-util-lib`](https://crates.io/crates/cgp-macro-test-util-lib), a normal library crate so -the logic can be unit-tested without the `proc-macro = true` restriction. +[`cgp-macro-test-util-lib`](https://crates.io/crates/cgp-macro-test-util-lib), a normal library +crate, so the logic can be unit-tested without the `proc-macro = true` restriction. -``` +```text cgp-macro-test-util/ # proc-macro entry points (#[proc_macro] fns) └── cgp-macro-test-util-lib/ # the actual implementation ├── entrypoints/ # one function per macro @@ -34,9 +34,8 @@ cgp-macro-test-util/ # proc-macro entry points (#[proc_macro] fns) └── functions/ # parse_attribute, pretty_format ``` -The implementation calls the real macro logic in `cgp-macro-lib` and -`cgp-macro-extra-lib`, not a copy of it, -so a snapshot is guaranteed to show what the production macros generate. +The implementation calls the real macro logic in `cgp-macro-lib` and `cgp-macro-extra-lib`, not a +copy of it, so a snapshot shows what the production macros generate. ## Available macros @@ -65,15 +64,15 @@ so a snapshot is guaranteed to show what the production macros generate. | `snapshot_cgp_producer!` | `#[cgp_producer]` | | `snapshot_cgp_auto_dispatch!` | `#[cgp_auto_dispatch]` | -Each accepts the same argument forms as the macro it wraps, because it drives the -production entry function; `snapshot_cgp_component!` takes both -`#[cgp_component(Greeter)]` and the brace form, for instance. +Each accepts the same argument forms as the macro it wraps, because it drives the production entry +function; `snapshot_cgp_component!` takes both `#[cgp_component(Greeter)]` and the brace form, for +instance. ## Anatomy of an invocation -Every snapshot macro takes the item under test, written exactly as you would write -the underlying macro invocation, followed by a test block naming the generated -`#[test]` and the identifier bound to the pretty-printed expansion: +Every snapshot macro takes the item under test, written exactly as you would write the underlying +macro invocation, followed by a test block naming the generated `#[test]` and the identifier bound +to the pretty-printed expansion: ```rust use cgp::prelude::*; @@ -92,16 +91,15 @@ snapshot_cgp_getter! { } ``` -That emits the real `#[cgp_getter]` expansion, which is the consumer trait and its -blanket impl, the provider trait, the marker struct, and the `UseField`/`UseFields`/ -`RedirectLookup` provider impls, and then a `fn expand_has_name()` holding the same -code as a string. The test-function name must be unique within its module, so give -each snapshot in a file a distinct one. +That emits the real `#[cgp_getter]` expansion, which is the consumer trait and its blanket impl, the +provider trait, the marker struct, and the `UseField`, `UseFields`, and `RedirectLookup` provider +impls, and then a `fn expand_has_name()` holding the same code as a string. The test-function name +must be unique within its module, so give each snapshot in a file a distinct one. ## Workflow with `insta` -Write the test with an **empty** inline snapshot first (`assert_snapshot!(output, @"")`), -then let `insta` fill it in: +Write the test with an **empty** inline snapshot first (`assert_snapshot!(output, @"")`), then let +`insta` fill it in: ```bash cargo insta test -p cgp-tests --test getter # then: cargo insta review @@ -109,9 +107,9 @@ cargo insta test -p cgp-tests --accept # accept everything non-interactiv INSTA_UPDATE=always cargo test -p cgp-tests # or via the env var ``` -After the first accept, the inline `@"..."` holds the pretty-printed generated code. -On later runs the test fails if that code changes, showing a diff to re-accept once -you have confirmed the change is intended. +After the first accept, the inline `@"..."` holds the pretty-printed generated code. On later runs +the test fails if that code changes, showing a diff to re-accept once you have confirmed the change +is intended. ## Notes and limitations diff --git a/crates/macros/cgp-macro/README.md b/crates/macros/cgp-macro/README.md index b6691f65..a7985819 100644 --- a/crates/macros/cgp-macro/README.md +++ b/crates/macros/cgp-macro/README.md @@ -1,12 +1,47 @@ # cgp-macro -`cgp-macro` provides the procedural macros of Context-Generic Programming (CGP), from -`#[cgp_component]`, which defines a component, and `#[cgp_impl]`, which writes a provider, to -`delegate_components!` and `check_components!`, which wire and check a context. +[Context-Generic Programming (CGP)](https://contextgeneric.dev/) extends Rust with pluggable trait +implementations, chosen at compile time, and `cgp-macro` provides the macros that write the traits +and impls behind them: `#[cgp_component]` defines a component, `#[cgp_impl]` writes a provider, and +`delegate_components!` and `check_components!` wire a context and check the wiring. This greeting +uses all four: -The `cgp` crate re-exports these macros under `cgp::prelude`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[cgp_component(Greeter)] +pub trait CanGreet { + fn greet(&self) -> String; +} + +#[cgp_impl(new GreetHello)] +impl Greeter { + fn greet(&self, #[implicit] name: &str) -> String { + format!("Hello, {name}!") + } +} + +#[derive(HasField)] +pub struct Person { + pub name: String, +} + +delegate_components! { Person { GreeterComponent: GreetHello } } +check_components! { Person { GreeterComponent } } + +fn main() { + let person = Person { name: "World".to_owned() }; + assert_eq!(person.greet(), "Hello, World!"); +} +``` + +Projects use these macros through `cgp::prelude`, so they depend on +[`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's reference has a page +for each of its main items: +[`#[cgp_component]`](https://contextgeneric.dev/docs/reference/macros/cgp_component), +[`#[cgp_impl]`](https://contextgeneric.dev/docs/reference/macros/cgp_impl), +[`delegate_components!`](https://contextgeneric.dev/docs/reference/macros/delegate_components), and +[`check_components!`](https://contextgeneric.dev/docs/reference/macros/check_components). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/macros/cgp-macro/src/lib.rs b/crates/macros/cgp-macro/src/lib.rs index 9fb333cd..c452926c 100644 --- a/crates/macros/cgp-macro/src/lib.rs +++ b/crates/macros/cgp-macro/src/lib.rs @@ -1,8 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate provides CGP's procedural macros, from defining a component to wiring and checking a -//! context. -//! -//! See [`#[cgp_component]`](https://contextgeneric.dev/docs/reference/macros/cgp_component), [`#[cgp_impl]`](https://contextgeneric.dev/docs/reference/macros/cgp_impl), [`delegate_components!`](https://contextgeneric.dev/docs/reference/macros/delegate_components), and [`check_components!`](https://contextgeneric.dev/docs/reference/macros/check_components). #![warn(missing_docs)] use proc_macro::TokenStream; diff --git a/crates/main/cgp-base-extra/README.md b/crates/main/cgp-base-extra/README.md index b6c4e838..72f316b2 100644 --- a/crates/main/cgp-base-extra/README.md +++ b/crates/main/cgp-base-extra/README.md @@ -1,12 +1,9 @@ # cgp-base-extra -`cgp-base-extra` gathers the base layer of Context-Generic Programming (CGP) together with its -abstract types, the foundation that CGP's error components are built on. +`cgp-base-extra` adds the abstract types of Context-Generic Programming (CGP) to CGP's base layer, +the foundation CGP's error components are built on. It is an internal layer of the `cgp` crate, +which re-exports everything it holds under `cgp::core`, so most projects depend on +[`cgp`](https://crates.io/crates/cgp) alone and never add this crate to their manifest. -It is an internal layer of the `cgp` crate, which re-exports everything it holds under `cgp::core`. -Most projects therefore depend on [`cgp`](https://crates.io/crates/cgp) alone, without adding this -crate to their manifest. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/main/cgp-base/README.md b/crates/main/cgp-base/README.md index a4c8d6f0..de30c745 100644 --- a/crates/main/cgp-base/README.md +++ b/crates/main/cgp-base/README.md @@ -1,11 +1,9 @@ # cgp-base `cgp-base` gathers the base layer of Context-Generic Programming (CGP): the type-level primitives, -the component machinery, and the procedural macros, which CGP's higher layers build on. +the component machinery, and the procedural macros that CGP's higher layers build on. Projects reach +it through the `cgp` crate, which re-exports it as `cgp::core::base`, so most depend on +[`cgp`](https://crates.io/crates/cgp) alone and never add this crate to their manifest. -The `cgp` crate re-exports it as `cgp::core::base`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. - -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/main/cgp-core/README.md b/crates/main/cgp-core/README.md index 4a0047e3..b87bf357 100644 --- a/crates/main/cgp-core/README.md +++ b/crates/main/cgp-core/README.md @@ -1,11 +1,36 @@ # cgp-core -`cgp-core` gathers the core of Context-Generic Programming (CGP): the base layer and the procedural -macros, the `#[async_trait]` macro, and the error, field, and abstract-type components. +[Context-Generic Programming (CGP)](https://contextgeneric.dev/) extends Rust with pluggable trait +implementations chosen at compile time, and `cgp-core` gathers the core it rests on: the base layer +and the procedural macros, the `#[async_trait]` macro, and the error, field, and abstract-type +components. A `#[cgp_fn]` function, for example, reads its arguments from the context's fields: -The `cgp` crate re-exports it as `cgp::core`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[cgp_fn] +fn rectangle_area(&self, #[implicit] width: f64, #[implicit] height: f64) -> f64 { + width * height +} + +#[derive(HasField)] +pub struct Rectangle { + pub width: f64, + pub height: f64, +} + +fn main() { + let rectangle = Rectangle { width: 2.0, height: 3.0 }; + assert_eq!(rectangle.rectangle_area(), 6.0); +} +``` + +Projects reach `cgp-core` through the `cgp` crate, which re-exports it as `cgp::core`, so they +depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's reference +has a page for each of its main items: +[`#[cgp_component]`](https://contextgeneric.dev/docs/reference/macros/cgp_component), +[`HasField`](https://contextgeneric.dev/docs/reference/traits/field-access/has_field), and +[`HasErrorType`](https://contextgeneric.dev/docs/reference/components/has_error_type). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/main/cgp-core/src/lib.rs b/crates/main/cgp-core/src/lib.rs index 792d518d..e4252a00 100644 --- a/crates/main/cgp-core/src/lib.rs +++ b/crates/main/cgp-core/src/lib.rs @@ -1,9 +1,6 @@ +#![doc = include_str!("../README.md")] #![no_std] #![allow(mixed_script_confusables)] -//! This crate re-exports CGP's core layer through `cgp::core`: the base layer, the macros, the -//! async trait macro, and the error, field, and type components. -//! -//! See [`#[cgp_component]`](https://contextgeneric.dev/docs/reference/macros/cgp_component), [`HasField`](https://contextgeneric.dev/docs/reference/traits/field-access/has_field), and [`HasErrorType`](https://contextgeneric.dev/docs/reference/components/has_error_type). #![warn(missing_docs)] /// The core names users write, re-exported through `cgp::prelude`. diff --git a/crates/main/cgp-extra/README.md b/crates/main/cgp-extra/README.md index b13c7b8d..e9062042 100644 --- a/crates/main/cgp-extra/README.md +++ b/crates/main/cgp-extra/README.md @@ -1,13 +1,48 @@ # cgp-extra -`cgp-extra` gathers the extra features of Context-Generic Programming (CGP): the handlers that model -computations, the dispatch and monadic combinators that compose them, task runners and the runtime -abstraction, the optional-field builders, and the generic error providers. Its `alloc` feature turns -on the providers that need an allocator. +`cgp-extra` gathers the features [Context-Generic Programming (CGP)](https://contextgeneric.dev/) +builds on its pluggable trait implementations: the handlers and the dispatch and monadic combinators +that compose them, task runners and the runtime abstraction, the optional-field builders, and the +generic error providers, two of which format an error into a `String` and so need the `alloc` +feature. A handler pipeline, for example, runs one computer after another: -The `cgp` crate re-exports it as `cgp::extra`. Most projects therefore depend on -[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest. +```rust,ignore +use cgp::extra::handler::{CanCompute, Computer, ComputerComponent, PipeHandlers}; +use cgp::prelude::*; -To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the -[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the -reference for each construct. +#[cgp_impl(new Double)] +impl Computer { + type Output = u64; + + fn compute(&self, _code: PhantomData, input: u64) -> u64 { + input * 2 + } +} + +#[cgp_impl(new Increment)] +impl Computer { + type Output = u64; + + fn compute(&self, _code: PhantomData, input: u64) -> u64 { + input + 1 + } +} + +pub struct App; + +delegate_components! { App { ComputerComponent: PipeHandlers } } + +fn main() { + assert_eq!(App.compute(PhantomData::<()>, 20), 41); +} +``` + +Projects reach `cgp-extra` through the `cgp` crate, which re-exports it as `cgp::extra`, so they +depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's reference +has a page for each of its main items: +[`CanHandle`](https://contextgeneric.dev/docs/reference/components/handler/handler), +[`PipeHandlers`](https://contextgeneric.dev/docs/reference/providers/handler/pipe_handlers), and +[`RaiseFrom`](https://contextgeneric.dev/docs/reference/providers/error/raise_from). + +To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and +the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API. diff --git a/crates/main/cgp-extra/src/lib.rs b/crates/main/cgp-extra/src/lib.rs index 52905e69..21cac623 100644 --- a/crates/main/cgp-extra/src/lib.rs +++ b/crates/main/cgp-extra/src/lib.rs @@ -1,9 +1,5 @@ +#![doc = include_str!("../README.md")] #![no_std] -//! This crate re-exports CGP's extra layer through `cgp::extra`: handlers, monads, dispatch, -//! runtime, runners, the optional-field layer, and the generic error providers. The `alloc` feature -//! turns on the providers that need an allocator. -//! -//! See [`CanHandle`](https://contextgeneric.dev/docs/reference/components/handler/handler), [`PipeHandlers`](https://contextgeneric.dev/docs/reference/providers/handler/pipe_handlers), and [`RaiseFrom`](https://contextgeneric.dev/docs/reference/providers/error/raise_from). #![warn(missing_docs)] /// The extra names users write, re-exported through `cgp::prelude`. diff --git a/crates/main/cgp/README.md b/crates/main/cgp/README.md index 54624532..515b9ebc 100644 --- a/crates/main/cgp/README.md +++ b/crates/main/cgp/README.md @@ -78,8 +78,8 @@ cgp = { version = "0.8.0", features = ["alloc"] } Once you start writing CGP, install [`cargo-cgp`](https://github.com/contextgeneric/cargo-cgp) alongside it to help diagnose wiring mistakes, which otherwise show up as errors about generated -types. `cargo cgp check` leads with the root cause for the classes of error it recognizes, though -it does not yet reshape every class. +types. `cargo cgp check` leads with the root cause for the classes it recognizes, and the tool does +not yet reshape every class. ## Learn CGP diff --git a/crates/standalone/error/cgp-error-anyhow/README.md b/crates/standalone/error/cgp-error-anyhow/README.md index 835f0552..6683d1b1 100644 --- a/crates/standalone/error/cgp-error-anyhow/README.md +++ b/crates/standalone/error/cgp-error-anyhow/README.md @@ -5,12 +5,14 @@ # cgp-error-anyhow: anyhow as a context's error type `cgp-error-anyhow` makes [`anyhow::Error`](https://docs.rs/anyhow) the abstract error type of a -[CGP](https://contextgeneric.dev) context, and supplies the providers that raise errors into it and -add context to it. Code written against `HasErrorType`, `CanRaiseError`, and `CanWrapError` stays -generic; only the context's wiring names anyhow. +[Context-Generic Programming (CGP)](https://contextgeneric.dev) context, and supplies the providers +that raise errors into it and add context to it. Code written against `HasErrorType`, +`CanRaiseError`, and `CanWrapError` stays generic, and only the context's wiring names anyhow. **[Website](https://contextgeneric.dev/) · [Modular error handling](https://contextgeneric.dev/docs/concepts/modular-error-handling) · [The error components](https://contextgeneric.dev/docs/reference/components/has_error_type)** +Each provider goes into the wiring under the components its row names: + | Provider | Wire it to | What it does | |---|---|---| | `UseAnyhowError` | `ErrorTypeProviderComponent` | sets the context's `Error` to `anyhow::Error` | @@ -38,18 +40,17 @@ delegate_components! { } } -let error = App::raise_error(std::io::Error::other("disk full")); -let error = App::wrap_error(error, "while saving"); -assert_eq!(format!("{error:#}"), "while saving: disk full"); +fn main() { + let error = App::raise_error(std::io::Error::other("disk full")); + let error = App::wrap_error(error, "while saving"); + assert_eq!(format!("{error:#}"), "while saving: disk full"); +} ``` -A `String` needs `DisplayAnyhowError` or `DebugAnyhowError`, because it is not a standard error. -`DebugAnyhowError` prints it with quotes. The crate is `no_std` and builds anyhow without its `std` -feature. Enabling that feature elsewhere in the dependency graph turns on the parts of anyhow that -need `std`, such as backtrace capture. +A `String` is not a standard error, so it needs `DisplayAnyhowError` or `DebugAnyhowError`, and the +debug form prints it with quotes. The crate itself is `no_std` and builds anyhow without its `std` +feature. When another crate in the dependency graph enables that feature, the parts of anyhow that +need `std`, such as backtrace capture, turn on as well. The crate re-exports `anyhow::Error` as `cgp_error_anyhow::Error`. -## License - -Licensed under the [Apache License, Version 2.0](https://github.com/contextgeneric/cgp/blob/main/LICENSE). diff --git a/crates/standalone/error/cgp-error-eyre/README.md b/crates/standalone/error/cgp-error-eyre/README.md index e501689e..e1421da9 100644 --- a/crates/standalone/error/cgp-error-eyre/README.md +++ b/crates/standalone/error/cgp-error-eyre/README.md @@ -5,12 +5,14 @@ # cgp-error-eyre: eyre as a context's error type `cgp-error-eyre` makes [`eyre::Report`](https://docs.rs/eyre) the abstract error type of a -[CGP](https://contextgeneric.dev) context, and supplies the providers that raise errors into it and -add context to it. Code written against `HasErrorType`, `CanRaiseError`, and `CanWrapError` stays -generic; only the context's wiring names eyre. +[Context-Generic Programming (CGP)](https://contextgeneric.dev) context, and supplies the providers +that raise errors into it and add context to it. Code written against `HasErrorType`, +`CanRaiseError`, and `CanWrapError` stays generic, and only the context's wiring names eyre. **[Website](https://contextgeneric.dev/) · [Modular error handling](https://contextgeneric.dev/docs/concepts/modular-error-handling) · [The error components](https://contextgeneric.dev/docs/reference/components/has_error_type)** +Each provider goes into the wiring under the components its row names: + | Provider | Wire it to | What it does | |---|---|---| | `UseEyreError` | `ErrorTypeProviderComponent` | sets the context's `Error` to `eyre::Report` | @@ -38,23 +40,22 @@ delegate_components! { } } -let error = App::raise_error(std::io::Error::other("disk full")); -let error = App::wrap_error(error, "while saving"); -assert_eq!(format!("{error:#}"), "while saving: disk full"); +fn main() { + let error = App::raise_error(std::io::Error::other("disk full")); + let error = App::wrap_error(error, "while saving"); + assert_eq!(format!("{error:#}"), "while saving: disk full"); +} ``` -A `String` needs `DisplayEyreError` or `DebugEyreError`, because it is not a standard error. -`DebugEyreError` prints it with quotes. +A `String` is not a standard error, so it needs `DisplayEyreError` or `DebugEyreError`, and the +debug form prints it with quotes. The crate enables eyre's `auto-install` feature, so eyre's default report handler is installed the first time a report is built. To use another handler, such as `color-eyre`, install it with -`eyre::set_hook` before the first error is raised; once a report exists, `set_hook` returns an +`eyre::set_hook` before the first error is raised, since once a report exists, `set_hook` returns an error. The crate also enables eyre's `track-caller` feature, and CGP's `raise_error` is `#[track_caller]`, so a report's `Location:` names the line that called `raise_error`. eyre requires `std`, so this crate does too. The crate re-exports `eyre::Error`, eyre's alias for `Report`, as `cgp_error_eyre::Error`. -## License - -Licensed under the [Apache License, Version 2.0](https://github.com/contextgeneric/cgp/blob/main/LICENSE). diff --git a/crates/standalone/error/cgp-error-std/README.md b/crates/standalone/error/cgp-error-std/README.md index cd4ed28d..cb7b93ac 100644 --- a/crates/standalone/error/cgp-error-std/README.md +++ b/crates/standalone/error/cgp-error-std/README.md @@ -5,12 +5,15 @@ # cgp-error-std: a boxed standard error as a context's error type `cgp-error-std` makes a boxed standard error, `Box`, the -abstract error type of a [CGP](https://contextgeneric.dev) context, and supplies the providers that -raise errors into it and add context to it. It depends only on `cgp` and `alloc`, so it suits a -`no_std` context that still wants an open-ended error type. +abstract error type of a [Context-Generic Programming (CGP)](https://contextgeneric.dev) context, +and supplies the providers that raise errors into it and add context to it. It depends only on `cgp` +and `alloc`, so it suits a `no_std` context that still wants an open-ended error type. **[Website](https://contextgeneric.dev/) · [Modular error handling](https://contextgeneric.dev/docs/concepts/modular-error-handling) · [The error components](https://contextgeneric.dev/docs/reference/components/has_error_type)** +The providers go into the wiring under the components their rows name, and the last three rows +are the types they build: + | Item | Wire it to | What it does | |---|---|---| | `UseBoxedStdError` | `ErrorTypeProviderComponent` | sets the context's `Error` to `cgp_error_std::Error` | @@ -41,17 +44,15 @@ delegate_components! { } } -let error = App::raise_error(std::io::Error::other("disk full")); -let error = App::wrap_error(error, "while saving"); -assert_eq!(format!("{error}"), "while saving"); -assert_eq!(format!("{error:#}"), "while saving: disk full"); +fn main() { + let error = App::raise_error(std::io::Error::other("disk full")); + let error = App::wrap_error(error, "while saving"); + assert_eq!(format!("{error}"), "while saving"); + assert_eq!(format!("{error:#}"), "while saving: disk full"); +} ``` -A `WrapError` prints its detail alone with `{}`, so a reporter that walks `source()` prints each -message once, and prints the whole chain with `{:#}` or `{:?}`. A `String` needs -`DisplayBoxedStdError` or `DebugBoxedStdError`, because it is not a standard error, and -`DebugBoxedStdError` prints it with quotes. - -## License +A `WrapError` prints only its detail with `{}`, so a reporter that walks `source()` prints each +message once, while `{:#}` and `{:?}` print the whole chain. A `String` is not a standard error, so +it needs `DisplayBoxedStdError` or `DebugBoxedStdError`, and the debug form prints it with quotes. -Licensed under the [Apache License, Version 2.0](https://github.com/contextgeneric/cgp/blob/main/LICENSE). diff --git a/crates/tests/cgp-tests/build.rs b/crates/tests/cgp-tests/build.rs index 9a0a5507..2818b2fb 100644 --- a/crates/tests/cgp-tests/build.rs +++ b/crates/tests/cgp-tests/build.rs @@ -1,9 +1,9 @@ //! Turns the wiring example in each error backend's README into a test module, so the //! `error_backends` target compiles and runs the README's own code instead of a copy of it. //! -//! Each README carries one `rust,ignore` block: `use` lines and items, then top-level statements -//! after the last item's closing brace. The block is split there, and the statements become the -//! body of a `#[test]` function, written to `$OUT_DIR/readme_.rs`. +//! Each README carries one `rust,ignore` block: `use` lines and items, then a `fn main()` that +//! runs the example. The block is split there, and the body of `main` becomes the body of a +//! `#[test]` function, written to `$OUT_DIR/readme_.rs`. use std::path::Path; use std::{env, fs}; @@ -42,19 +42,22 @@ fn readme_test(backend: &str, readme: &str) -> Result { .ok_or("no `rust,ignore` block")?; let lines: Vec<&str> = block.lines().collect(); - let split = lines + let main = lines .iter() - .rposition(|line| line.starts_with('}')) - .ok_or("the block has no top-level item")?; + .position(|line| *line == "fn main() {") + .ok_or("the block has no `fn main() {` line")?; + if lines.last() != Some(&"}") { + return Err("the block does not end with the closing brace of `main`"); + } - let items = lines[..=split].join("\n"); - let statements: Vec = lines[split + 1..] + let items = lines[..main].join("\n"); + let statements: Vec<&str> = lines[main + 1..lines.len() - 1] .iter() + .copied() .filter(|line| !line.trim().is_empty()) - .map(|line| format!(" {line}")) .collect(); if statements.is_empty() { - return Err("the block has no statements after its items"); + return Err("`main` has no statements"); } Ok(format!(