diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 51d5202b..7ee2ff98 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -78,6 +78,10 @@ jobs: # are UI fixtures in `cargo-cgp`, not tests here. - name: Run tests run: cargo nextest run --all-features --no-fail-fast --workspace + # nextest runs no doctests, so the examples in the crate docs, such as the quick + # look the `cgp` crate's README gives as its documentation, run in this step. + - name: Run doctests + run: cargo test --doc --workspace --all-features --no-fail-fast doc: name: Docs diff --git a/AGENTS.md b/AGENTS.md index 06d47ca8..6607da85 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -18,7 +18,8 @@ holds their canonical wording: read it first, and change a rule there before upd - Verify a claim against the source before writing it. - Keep every view of the same truth in sync in the same change. - Ask the user when a decision is ambiguous. -- Apply point-first writing to internal prose. +- Apply point-first writing to internal prose, and natural writing to public-facing documents + such as READMEs. ## Task guides diff --git a/README.md b/README.md index d6d57598..bbe4a349 100644 --- a/README.md +++ b/README.md @@ -161,9 +161,10 @@ cargo cgp setup # provisions the compiler + driver it needs Then run `cargo cgp check` wherever you would run `cargo check`; it forces the pinned nightly that `cargo cgp setup` installed only for its own check, so your project keeps its toolchain untouched. (Prefer Nix? `nix run github:contextgeneric/cargo-cgp/v0.1.0 -- check` runs it without installing.) -The [installation guide](https://contextgeneric.dev/docs/cargo-cgp/installation) covers every path, -[reading the output](https://contextgeneric.dev/docs/cargo-cgp/reading-output) explains the rewritten -errors, and the [error codes](https://contextgeneric.dev/docs/cargo-cgp/error-codes) catalog every +The [installation guide](https://contextgeneric.dev/docs/tooling/cargo-cgp/installation) covers +every path, [reading the output](https://contextgeneric.dev/docs/tooling/cargo-cgp/reading-output) +explains the rewritten errors, and the +[error codes](https://contextgeneric.dev/docs/tooling/cargo-cgp/error-codes) catalog every `[CGP-Exxx]` code. cargo-cgp is optional and adds only the reading commands `check` and `expand`: keep building, diff --git a/crates/main/cgp/README.md b/crates/main/cgp/README.md index 147a27aa..96dd26be 100644 --- a/crates/main/cgp/README.md +++ b/crates/main/cgp/README.md @@ -2,39 +2,100 @@ **A language extension for Rust, with pluggable trait implementations at compile-time.** -The `cgp` crate is the facade over the collection of crates that provide CGP. It re-exports them -under `cgp::core` and `cgp::extra`, and gathers the names users write in `cgp::prelude`. In ordinary -Rust a trait has one implementation per type; CGP lets one trait have many interchangeable -implementations and lets each *context* choose which one it uses, through a small wiring table the -compiler resolves statically, so provider selection adds nothing at runtime. +Still ordinary Rust: a library on the stable toolchain, with provider selection resolved at compile +time, adopted one trait at a time. -## Learn CGP +In ordinary Rust, a trait has one implementation per type, so a program that needs two behaviors for +the same type has to use a wrapper type or a second trait. Context-Generic Programming (CGP) +works around that limit: one trait can have many interchangeable implementations, called +*providers*, and each *context* chooses the one it uses through a small wiring table. + +To get started, use this crate: it re-exports the crates that provide CGP under `cgp::core` and +`cgp::extra`, and gathers the names you write in `cgp::prelude`. + +## A quick look + +A greeting shows how these pieces fit together. The example below defines a greeting component +and a provider that reads a name, then wires `Person` to that provider. Here `Person` holds the data +being greeted. The `check_components!` line checks the wiring at compile time, making the build +fail if `Person` lacks the `name` field the provider reads: + +```rust +use cgp::prelude::*; + +#[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, +} -The documentation lives on the website, [contextgeneric.dev](https://contextgeneric.dev/): +delegate_components! { Person { GreeterComponent: GreetHello } } +check_components! { Person { GreeterComponent } } -- [Quickstart](https://contextgeneric.dev/docs/quickstart): install it and run a first program. -- [Tutorials](https://contextgeneric.dev/docs/tutorials/hello): learn by building. -- [Concepts](https://contextgeneric.dev/docs/concepts/): why CGP works the way it does. -- [Reference](https://contextgeneric.dev/docs/reference/): one page per construct. +fn main() { + let person = Person { name: "World".to_owned() }; + assert_eq!(person.greet(), "Hello, World!"); +} +``` -The API documentation here covers the items themselves; the website explains how to use them. For the -exhaustive per-construct semantics, the -[CGP knowledge base](https://github.com/contextgeneric/cgp-knowledge-base) is written for agents and -records the internals. +For a greeting alone, a plain trait would be enough. Providers start to help once an application +needs interchangeable implementations or wants to reuse and compose them. That flexibility comes +with more declarations, extra compile-time work, and new concepts to learn. The raw compile errors +can also be verbose, so the installation steps below include a tool to help interpret them. ## Install +Add the crate to your project: + ```bash cargo add cgp ``` -```rust -use cgp::prelude::*; +CGP needs Rust 1.89 or newer on the stable toolchain. With its default features, it needs neither +`std` nor `alloc`, so it builds for `no_std` targets as well. The `DebugError` and `DisplayError` +providers format an error into a `String`, so using them requires an allocator and the `alloc` +feature: + +```toml +[dependencies] +cgp = { version = "0.8.0", features = ["alloc"] } ``` -CGP requires Rust 1.89+ and runs on the stable toolchain. When you start with CGP, install -[`cargo-cgp`](https://github.com/contextgeneric/cargo-cgp) alongside it: `cargo cgp check` leads with -the root cause for the classes it recognizes, and the tool does not yet reshape every class. +Once you start writing CGP, install [`cargo-cgp`](https://github.com/contextgeneric/cargo-cgp) +alongside it to help diagnose wiring mistakes. These otherwise show up as errors about generated +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 + +To learn more, visit [contextgeneric.dev](https://contextgeneric.dev/). Its documentation covers +everything from your first program to the details of individual constructs: + +- [Quickstart](https://contextgeneric.dev/docs/quickstart): how do I install CGP and run a first + program? +- [Tutorials](https://contextgeneric.dev/docs/tutorials/hello): how do I learn it by building + something? +- [Concepts](https://contextgeneric.dev/docs/concepts/): why does CGP work the way it does? +- [Reference](https://contextgeneric.dev/docs/reference/): what does each construct accept and + generate? +- [Costs and maturity](https://contextgeneric.dev/#costs): what should I weigh before adopting it? + +For details about individual API items, use the documentation on docs.rs. Each construct's +documentation also links to the website's reference page explaining how to use it, where one is +available. If you work with a coding agent, the site's +[AI-assisted development](https://contextgeneric.dev/docs/ai/) section explains the agent skill +and the knowledge base it draws on. ## License diff --git a/crates/standalone/error/cgp-error-anyhow/README.md b/crates/standalone/error/cgp-error-anyhow/README.md index dcb64b99..835f0552 100644 --- a/crates/standalone/error/cgp-error-anyhow/README.md +++ b/crates/standalone/error/cgp-error-anyhow/README.md @@ -9,7 +9,7 @@ add context to it. Code written against `HasErrorType`, `CanRaiseError`, and `CanWrapError` stays generic; 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) · [Project record](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/projects/error/README.md)** +**[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)** | Provider | Wire it to | What it does | |---|---|---| diff --git a/crates/standalone/error/cgp-error-eyre/README.md b/crates/standalone/error/cgp-error-eyre/README.md index 8b7c3b48..e501689e 100644 --- a/crates/standalone/error/cgp-error-eyre/README.md +++ b/crates/standalone/error/cgp-error-eyre/README.md @@ -9,7 +9,7 @@ add context to it. Code written against `HasErrorType`, `CanRaiseError`, and `CanWrapError` stays generic; 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) · [Project record](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/projects/error/README.md)** +**[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)** | Provider | Wire it to | What it does | |---|---|---| diff --git a/crates/standalone/error/cgp-error-std/README.md b/crates/standalone/error/cgp-error-std/README.md index 81e82e59..cd4ed28d 100644 --- a/crates/standalone/error/cgp-error-std/README.md +++ b/crates/standalone/error/cgp-error-std/README.md @@ -9,7 +9,7 @@ abstract error type of a [CGP](https://contextgeneric.dev) context, and supplies 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) · [Project record](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/projects/error/README.md)** +**[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)** | Item | Wire it to | What it does | |---|---|---|