Configuration
Most applications need configuration values to connect to databases, external services, or define internal behavior. Sword provides a flexible configuration system for this.
Configuration uses the thisconfig crate to load TOML files. By default, building the application looks for config/config.toml, but you can also set a different path. If the file is missing or contains invalid TOML, the application fails to build.
The 'Config' struct
Once the configuration is loaded, it is represented by the Config struct. This struct gives you typed, checked access to the configuration values.
You can access it through the config field of an ApplicationBuilder or Application instance, or through dependency injection in other components.
API Reference: Methods and attributes of the 'Config' struct
Example snippet used in this section
use serde::Deserialize;
use sword::prelude::*;
#[config(key = "database")]
#[derive(Debug, Clone, Deserialize)]
struct DatabaseConfig {
database_url: String,
max_connections: u32,
}
impl Default for DatabaseConfig {
fn default() -> Self {
Self {
database_url: "postgres://user:password@localhost/mydb".to_string(),
max_connections: 10,
}
}
}Getting a configuration section
Extracts a configuration struct and returns Option<T>.
pub fn get<T>(&self) -> Option<T>
where
T: DeserializeOwned + ConfigItem,Parameters
Tis the configuration struct to extract. It must implementDeserializeOwnedandConfigItem.
Example
let database = config.get::<DatabaseConfig>();Getting a configuration section or its default value
Extracts the struct or returns its Default if it is not present.
pub fn get_or_default<T>(&self) -> T
where
T: DeserializeOwned + ConfigItem + Default,Parameters
Tis the configuration struct to extract. It must implementDeserializeOwned,ConfigItem, andDefault.
Example
let database = config.get_or_default::<DatabaseConfig>();Getting a configuration section or panicking
Extracts the struct or panics if it is not present.
pub fn expect<T>(&self) -> T
where
T: DeserializeOwned + ConfigItem,Parameters
Tis the configuration struct to extract. It must implementDeserializeOwnedandConfigItem.
Example
let database = config.expect::<DatabaseConfig>();This is useful when the configuration is required and the application cannot continue without it. It is equivalent to the code below.
let database = config.get::<DatabaseConfig>().expect("Expected configuration item not found");Application configuration
Sword defines a shared configuration for every application type.
| Key | Type | Default | Description |
|---|---|---|---|
name | Option<String> | None | Application name |
environment | Option<String> | None | Environment name, informational only |
diagnostics | miette | tracing | miette | How Sword reports build and startup failures |
TOML example
[application]
name = "My Sword App"
environment = "development"
diagnostics = "miette"Environment selection
The Application::builder() method picks the configuration file from SWORD_ENV:
| SWORD_ENV | File |
|---|---|
| unset | config/config.toml |
| dev / development | config/config.dev.toml |
| prod / production | config/config.prod.toml |
| test / testing | config/config.test.toml |
SWORD_ENV=prod cargo runThe selected file is required: if it does not exist, startup fails. An unrecognized value also fails, with a diagnostic that lists the accepted ones.
Application::from_config_path and Application::from_config do not read SWORD_ENV. In those cases the code decides the file.
Diagnostics
Sword reports build and startup failures as diagnostics. A diagnostic has a stable code, a phase, a reason and optional context. Some diagnostics tied to source code also include a span.
The [application] diagnostics key selects how Sword shows them.
mietteprints a readable report. This is the default.tracingemits a structured event through the active subscriber.
A configuration error with miette looks like this.
sword.build.web · Build error
× Invalid [web] configuration: host must not be empty
help: Use 0.0.0.0, 127.0.0.1, or a specific interfaceWith tracing, Sword emits one event per diagnostic. If no subscriber is active, Sword reports a configuration error.
Custom configuration
Sword lets you define your own configuration on top of the framework's base configuration. This is useful when the application needs domain-specific or integration-specific settings.
Annotate a struct with the #[config] macro and set the TOML key where it will be loaded.
use serde::Deserialize;
use sword::prelude::*;
#[config(key = "database")]
#[derive(Debug, Clone, Deserialize)]
pub struct DatabaseConfig {
database_url: String,
max_connections: u32,
}[database]
database_url = "postgres://user:password@localhost/mydb"
max_connections = 50[dependencies]
sword = { version = "0.6" }
serde = { version = "x.y.z", features = ["derive"] }Required traits
To use a struct as custom configuration, it must derive or implement the following traits.
DebugCloneDeserialize
The macro generates the ConfigItem implementation and registers the configuration in the application state.
Validation
Sword checks the configuration sections it owns when the application starts. These are [application], [web], [grpc], [socketio], [events] and [tracing].
If a section has an invalid value, the application does not start. The error names the section and the key.
sword.build.web · Build error
× Invalid [web] configuration: host must not be empty
help: Use 0.0.0.0, 127.0.0.1, or a specific interfaceSections that are absent use their defaults and are not validated.
For your own sections created with #[config], invalid types fail at startup. Semantic rules (ranges, combinations, and similar) are not checked automatically. Add those checks in your code when you need them.
Environment variables
Configuration supports direct interpolation of environment variables.
[database]
database_url = "${DATABASE_URL:postgres://localhost/app}"
max_connections = "${DB_MAX_CONNECTIONS:20}"The syntax is ${VARIABLE_NAME:default_value}. If you do not set a default and the variable does not exist, loading fails.
Loading content from files
You can load file content into TOML values with the file: prefix.
[auth]
jwt_secret = "file:secrets/jwt_secret.txt"This is useful for secrets, certificates, and private keys.
Special units
You can also use human-readable units for sizes and durations. These units keep the parsed value and the original raw value (raw) for logging and display.
Byte representation
ByteConfig represents byte sizes.
rawis the original string read from TOML (for example,"10MB").parsedis the value converted to bytes (usize) for internal use.
pub struct ByteConfig {
pub parsed: usize,
pub raw: String,
}max-payload = "100KB"
body-limit = "1MB"You can also use binary units such as KiB, MiB, etc. See the available formats in byte-unit.
Time representation
TimeConfig represents durations.
rawis the original string (for example,"30s","1h 30m").parsedis astd::time::Durationready for timeouts, intervals, etc.
pub struct TimeConfig {
pub parsed: Duration,
pub raw: String,
}request-timeout = { enabled = true, timeout = "10s", display = true }
ping-timeout = "20s"
ping-interval = "25s"See the available formats in duration-str.
Configuration per application type
You can find configuration examples for each Sword application type in the practical guides.

