Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
101 changes: 81 additions & 20 deletions crates/main/cgp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion crates/standalone/error/cgp-error-anyhow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---|---|---|
Expand Down
2 changes: 1 addition & 1 deletion crates/standalone/error/cgp-error-eyre/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---|---|---|
Expand Down
2 changes: 1 addition & 1 deletion crates/standalone/error/cgp-error-std/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---|---|---|
Expand Down
Loading