id_effect 0.13.0

Effect<A, E, R>: lazy Send effects over a typed capability list, run on a fiber runtime (Tokio executor), with layers, streams, schedules and coordination
Documentation
# Profiles: `dev`, `test`, `prod`

One binary, three environments. [`Profiles`](https://docs.rs/id_effect_config/latest/id_effect_config/profile/struct.Profiles.html) (in `id_effect_config`, default features `toml` + `env`) picks a profile at startup and layers the configuration for it, so the same [`Config`](../part2/ch07-10-config.md) descriptors read different values in development, CI and production.

## Layout

```text
config/
  base.toml   # shared by every profile
  dev.toml    # local development
  test.toml   # CI / integration tests
  prod.toml   # production
```

```toml
# config/base.toml
name = "ledger"
port = 3000

[db]
host = "localhost"
password = "dev-only"
```

```toml
# config/prod.toml
port = 8080

[db]
host = "db.internal"
```

## Precedence

Sources merge key by key; a later row overrides an earlier one.

| Rank | Source | Example |
|------|--------|---------|
| 1 (lowest) | `config/base.toml` | `port = 3000` |
| 2 | `config/<profile>.toml` | `port = 8080` |
| 3 | overlays added with `Profiles::overlay` (secret-manager snapshots) | `db.password` |
| 4 (highest) | `APP_`-prefixed environment variables | `APP_DB__PASSWORD=…` |

In short: **env > profile > base**. Environment keys lose the prefix, are lower-cased, and `__` nests: `APP_DB__PASSWORD` sets `db.password`. A missing TOML file is skipped (so `dev.toml` can be absent); a file that does not parse fails the load with `ConfigError::Figment`.

## Selecting the profile

Highest priority first:

1. `--profile prod` or `--profile=prod` on the command line (the last one wins);
2. the `ID_EFFECT_PROFILE` environment variable (empty counts as unset);
3. the default, `dev`.

Only declared profiles are accepted (`dev`, `test`, `prod` unless you call `.known([...])`). Anything else, a typo such as `ID_EFFECT_PROFILE=prdo` included, is a typed `ConfigError::UnknownProfile { profile, known }` that fails startup. A typo never silently falls back to the base configuration:

```text
unknown configuration profile "prdo" (known: dev, test, prod)
```

## Wiring it into a program

`Profiles::layer` resolves and loads the profile when the layer is built and installs the merged sources as the `ConfigProviderService` capability, so existing `Config::layer` / `Config::run` descriptors work unchanged:

```rust,no_run
use id_effect::{Effect, Exit, HCons, HNil, need};
use id_effect_config::{Config, ConfigError, Profiles, Secret};

struct Port(i64);
struct DbPassword(Secret<String>);

fn main() {
    // `config/` relative to the working directory, `ID_EFFECT_PROFILE` / `--profile`, `APP_` env.
    let config = Profiles::new().layer::<ConfigError>();

    let port = Config::integer("port").map(Port).layer::<ConfigError>();
    let program: Effect<i64, ConfigError, HCons<Port, HNil>> = need::<Port>().map(|p| p.0);

    match program.provide_layer(config.provide_to(port)).block_on() {
        Exit::Success(port) => println!("listening on {port}"),
        Exit::Failure(cause) => eprintln!("bad configuration: {cause:?}"),
    }

    // Secrets keep their redaction whatever layer they come from.
    let _password = Config::string("db.password").secret().map(DbPassword);
}
```

Paths are the dotted, lower-case TOML keys: `Config::string("db.host")`.

Outside an effect, `Profiles::new().load()?` returns a `ProfiledConfig` with the chosen `profile()`, the merged `figment()`, `extract::<T>()` for a whole serde struct, and `provider()` for descriptors.

## Customising

```rust
use id_effect_config::Profiles;

let profiles = Profiles::new()
    .dir("deploy/config")            // instead of ./config
    .known(["dev", "test", "staging", "prod"])
    .default_profile("dev")
    .env_var("APP_PROFILE")          // instead of ID_EFFECT_PROFILE
    .env_prefix("LEDGER_");          // LEDGER_DB__HOST instead of APP_DB__HOST
assert_eq!(profiles.known_profiles().len(), 4);
```

`Profiles::select(args, env_value)` is the pure form of the selection rules, handy in tests: `profiles.select(["--profile", "test"], None)`.

## Hot reload

`Profiles::watch_for::<C>(profile, every)` (or `watch(every)` for the resolved profile, `watch_with` for a custom parser) loads the profile once, exactly like `load_for`, and returns a `ProfileWatch<C>` holding the value in a `SubscriptionRef`. The initial load must succeed; after that the configuration can change under a running service:

- `watch.run()` sleeps `every`, compares the modification time and length of `base.toml` and `<profile>.toml` with the previous poll and, on a change, merges every source again (files, overlays, environment) and parses the result. Fork it in the scope that owns the subscription: `scope.fork_in(watch.run())`. It polls instead of using file-system notifications, so it behaves the same on every platform and under the virtual clock of `TestExecutor`.
- A value that parses replaces the current one and is published: `watch.subscribe(&scope)` yields a queue that receives the current value first, then every reloaded one; `watch.get()` reads the latest.
- A change that does not parse (a TOML syntax error, a value of the wrong type) **keeps the previous value**, publishes nothing, and is reported: `reload_if_changed` fails with the `ConfigError`, `last_error()` keeps its message until the next good reload. The poll loop goes on, so a bad edit never takes the service down, and the next edit that fixes the file is picked up.
- Only the two profile files trigger a reload. A change to an overlay or the environment alone does not; call `watch.reload()` to force one.

```rust
use std::time::Duration;

use id_effect::{Effect, Exit, Never};
use id_effect_config::{ConfigError, Profiles};
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize, PartialEq)]
struct Server {
    port: u16,
}

let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("base.toml"), "port = 3000").unwrap();
let profiles = Profiles::new().dir(dir.path()).env_prefix("BOOK_WATCH_");
let watch = profiles.watch_for::<Server>(profiles.profile("prod")?, Duration::from_secs(2))?;

// The service forks `watch.run()` next to its subscription; here, one poll by hand.
std::fs::write(dir.path().join("prod.toml"), "port = 8080").unwrap();
let poll: Effect<bool, ConfigError> = watch.reload_if_changed();
assert!(matches!(poll.block_on(), Exit::Success(true)));

// A broken edit is rejected and the last good value stays.
std::fs::write(dir.path().join("base.toml"), "port = ").unwrap();
let poll: Effect<bool, ConfigError> = watch.reload_if_changed();
assert!(matches!(poll.block_on(), Exit::Failure(_)));
let current: Effect<Server, Never> = watch.get();
assert!(matches!(current.block_on(), Exit::Success(Server { port: 8080 })));
assert!(watch.last_error().unwrap().contains("TOML parse error"));
# Ok::<(), ConfigError>(())
```

`cargo run -p id_effect_config --example 007_watch` runs the whole loop on a temporary directory: it subscribes, edits `dev.toml` under the running poller, prints the reloaded value, then writes a broken file and shows it rejected. The integration test `crates/id_effect_config/tests/watch.rs` checks the same two behaviours under virtual time.

## Secrets per profile

Keep real secrets out of `prod.toml`. Either inject them through the environment (`APP_DB__PASSWORD`), or add a secret-manager snapshot with `Profiles::overlay(provider)`: overlays sit above the profile files and below the environment, so production reads the managed secret while a developer can still override it locally. Read them with `Config::…().secret()` so `Debug` and logs print `<redacted>` (see [Config + `Secret` from flags](./ch16-02-cli-config-secret.md)).