use std::fs;
use std::path::{Path, PathBuf};
use serde::Deserialize;
use thiserror::Error;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Config {
pub defaults: Defaults,
pub contracts: Vec<ContractConfig>,
pub consumers: Vec<ConsumerConfig>,
pub consumer_options: ConsumerOptions,
}
impl Config {
pub fn from_path(path: &Path) -> Result<Self, ConfigError> {
let contents = fs::read_to_string(path).map_err(|source| ConfigError::ReadFile {
path: path.to_path_buf(),
source,
})?;
Self::parse(&contents)
}
pub fn parse(contents: &str) -> Result<Self, ConfigError> {
let raw: RawConfig = toml::from_str(contents).map_err(ConfigError::ParseToml)?;
raw.validate()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Defaults {
pub compatibility: Compatibility,
pub baseline: Option<Baseline>,
}
impl Default for Defaults {
fn default() -> Self {
Self {
compatibility: Compatibility::WireJson,
baseline: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContractConfig {
pub name: String,
pub format: ContractFormat,
pub source: PathBuf,
pub compatibility: Option<Compatibility>,
pub baseline: Option<Baseline>,
pub allow: Vec<Suppression>,
pub generated: Option<GeneratedConfig>,
}
impl ContractConfig {
pub fn effective_compatibility(&self, defaults: &Defaults) -> Compatibility {
self.compatibility.unwrap_or(defaults.compatibility)
}
pub fn effective_baseline<'a>(&'a self, defaults: &'a Defaults) -> Option<&'a Baseline> {
self.baseline.as_ref().or(defaults.baseline.as_ref())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Compatibility {
Wire,
WireJson,
Surface,
Strict,
}
impl Compatibility {
pub fn parse(input: &str) -> Result<Self, String> {
match input.trim().to_ascii_lowercase().as_str() {
"wire" => Ok(Self::Wire),
"wire-json" | "wirejson" => Ok(Self::WireJson),
"surface" => Ok(Self::Surface),
"strict" => Ok(Self::Strict),
other => Err(format!(
"unknown compatibility level `{other}`; expected one of: \
wire, wire-json, surface, strict"
)),
}
}
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Wire => "wire",
Self::WireJson => "wire-json",
Self::Surface => "surface",
Self::Strict => "strict",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ContractFormat {
Openapi,
Proto,
Graphql,
Asyncapi,
Mcp,
}
impl ContractFormat {
pub fn parse(input: &str) -> Result<Self, String> {
match input.trim().to_ascii_lowercase().as_str() {
"openapi" => Ok(Self::Openapi),
"proto" | "protobuf" => Ok(Self::Proto),
"graphql" => Ok(Self::Graphql),
"asyncapi" => Ok(Self::Asyncapi),
"mcp" => Ok(Self::Mcp),
other => Err(format!(
"unknown format `{other}`; expected one of: openapi, proto, graphql, asyncapi, mcp"
)),
}
}
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Openapi => "openapi",
Self::Proto => "proto",
Self::Graphql => "graphql",
Self::Asyncapi => "asyncapi",
Self::Mcp => "mcp",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DemandFormat {
Pact,
GraphqlOperations,
Manifest,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsumerConfig {
pub name: Option<String>,
pub format: DemandFormat,
pub source: PathBuf,
pub provider: Option<String>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ConsumerOptions {
pub policy: ConsumerPolicy,
pub completeness: Completeness,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ConsumerPolicy {
#[default]
Annotate,
Escalate,
Triage,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Completeness {
#[default]
OpenWorld,
ClosedWorld,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Baseline {
File(PathBuf),
Git {
spec: String,
},
GitMergeBase {
reference: String,
},
Tag {
name: String,
},
LatestTag {
pattern: String,
},
Rev {
rev: String,
},
}
impl Baseline {
#[must_use]
pub fn from_flag(input: &str) -> Self {
let input = input.trim();
if let Some(pattern) = input.strip_prefix("latest-tag:") {
return Self::LatestTag {
pattern: pattern.to_owned(),
};
}
if let Some(reference) = input.strip_prefix("merge-base:") {
return Self::GitMergeBase {
reference: reference.to_owned(),
};
}
if input.contains(':') {
return Self::Git {
spec: input.to_owned(),
};
}
Self::Rev {
rev: input.to_owned(),
}
}
pub fn strategy_name(&self) -> &'static str {
match self {
Self::File(_) => "file",
Self::Git { .. } => "git",
Self::GitMergeBase { .. } => "git-merge-base",
Self::Tag { .. } => "tag",
Self::LatestTag { .. } => "latest-tag",
Self::Rev { .. } => "rev",
}
}
#[must_use]
pub fn is_release_baseline(&self) -> bool {
matches!(
self,
Self::Tag { .. } | Self::LatestTag { .. } | Self::Rev { .. }
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Suppression {
pub rule: String,
pub endpoint: Option<String>,
pub field: Option<String>,
pub reason: String,
pub expires: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GeneratedConfig {
pub command: String,
}
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("failed to read config file {path}: {source}")]
ReadFile {
path: PathBuf,
source: std::io::Error,
},
#[error("failed to parse brake.toml: {0}")]
ParseToml(toml::de::Error),
#[error("invalid brake.toml: {0}")]
Validation(String),
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawConfig {
#[serde(default)]
defaults: RawDefaults,
#[serde(default, rename = "contract")]
contracts: Vec<RawContract>,
#[serde(default, rename = "consumer")]
consumers: Vec<RawConsumer>,
#[serde(default, rename = "consumers")]
consumers_options: Option<RawConsumerOptions>,
}
impl RawConfig {
fn validate(self) -> Result<Config, ConfigError> {
let defaults = self.defaults.validate("defaults")?;
let mut contracts = Vec::with_capacity(self.contracts.len());
for contract in self.contracts {
contracts.push(contract.validate()?);
}
let mut consumers = Vec::with_capacity(self.consumers.len());
for consumer in self.consumers {
consumers.push(consumer.validate()?);
}
Ok(Config {
defaults,
contracts,
consumers,
consumer_options: self
.consumers_options
.map(RawConsumerOptions::validate)
.unwrap_or_default(),
})
}
}
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawDefaults {
compatibility: Option<Compatibility>,
baseline: Option<RawBaseline>,
}
impl RawDefaults {
fn validate(self, context: &str) -> Result<Defaults, ConfigError> {
Ok(Defaults {
compatibility: self.compatibility.unwrap_or(Compatibility::WireJson),
baseline: self.baseline.map(|raw| raw.validate(context)).transpose()?,
})
}
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawContract {
name: String,
format: ContractFormat,
source: PathBuf,
compatibility: Option<Compatibility>,
baseline: Option<RawBaseline>,
#[serde(default)]
allow: Vec<RawSuppression>,
generated: Option<RawGenerated>,
}
impl RawContract {
fn validate(self) -> Result<ContractConfig, ConfigError> {
if self.name.trim().is_empty() {
return Err(ConfigError::Validation(
"contract name cannot be empty".into(),
));
}
if self.source.as_os_str().is_empty() {
return Err(ConfigError::Validation(format!(
"contract `{}` has an empty source path",
self.name
)));
}
let mut allow = Vec::with_capacity(self.allow.len());
for suppression in self.allow {
allow.push(suppression.validate(&self.name)?);
}
Ok(ContractConfig {
name: self.name.clone(),
format: self.format,
source: self.source,
compatibility: self.compatibility,
baseline: self
.baseline
.map(|raw| raw.validate(&format!("contract `{}`", self.name)))
.transpose()?,
allow,
generated: self
.generated
.map(|raw| raw.validate(&self.name))
.transpose()?,
})
}
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawConsumer {
name: Option<String>,
format: DemandFormat,
source: PathBuf,
provider: Option<String>,
}
impl RawConsumer {
fn validate(self) -> Result<ConsumerConfig, ConfigError> {
if self.source.as_os_str().is_empty() {
return Err(ConfigError::Validation(
"a `[[consumer]]` has an empty source path".into(),
));
}
let text = self.source.to_string_lossy();
if text.contains("://") {
return Err(ConfigError::Validation(format!(
"consumer source `{text}` looks like a URL. brake never fetches a demand \
source: have CI write the file and point `source` at the path"
)));
}
if let Some(name) = &self.name
&& name.trim().is_empty()
{
return Err(ConfigError::Validation(
"a `[[consumer]]` has an empty name".into(),
));
}
Ok(ConsumerConfig {
name: self.name,
format: self.format,
source: self.source,
provider: self.provider,
})
}
}
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawConsumerOptions {
policy: Option<ConsumerPolicy>,
completeness: Option<Completeness>,
}
impl RawConsumerOptions {
fn validate(self) -> ConsumerOptions {
ConsumerOptions {
policy: self.policy.unwrap_or_default(),
completeness: self.completeness.unwrap_or_default(),
}
}
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawGenerated {
command: String,
}
impl RawGenerated {
fn validate(self, contract_name: &str) -> Result<GeneratedConfig, ConfigError> {
if self.command.trim().is_empty() {
return Err(ConfigError::Validation(format!(
"contract `{contract_name}` generated.command cannot be empty"
)));
}
Ok(GeneratedConfig {
command: self.command,
})
}
}
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawBaseline {
file: Option<PathBuf>,
git: Option<String>,
#[serde(rename = "git-merge-base")]
git_merge_base: Option<String>,
tag: Option<String>,
#[serde(rename = "latest-tag")]
latest_tag: Option<String>,
rev: Option<String>,
}
impl RawBaseline {
fn validate(self, context: &str) -> Result<Baseline, ConfigError> {
let candidates: [(&str, Option<String>); 5] = [
("git", self.git),
("git-merge-base", self.git_merge_base),
("tag", self.tag),
("latest-tag", self.latest_tag),
("rev", self.rev),
];
let mut chosen: Option<(&str, String)> = None;
let mut set = usize::from(self.file.is_some());
for (key, value) in candidates {
let Some(value) = value else { continue };
set += 1;
if value.trim().is_empty() {
return Err(ConfigError::Validation(format!(
"{context} baseline `{key}` cannot be empty"
)));
}
chosen.get_or_insert((key, value));
}
if set != 1 {
return Err(ConfigError::Validation(format!(
"{context} baseline must set exactly one of `file`, `git`, `git-merge-base`, \
`tag`, `latest-tag`, or `rev`{}",
if set > 1 { ", and it sets several" } else { "" }
)));
}
if let Some(file) = self.file {
if file.as_os_str().is_empty() {
return Err(ConfigError::Validation(format!(
"{context} baseline file path cannot be empty"
)));
}
return Ok(Baseline::File(file));
}
let (key, value) = chosen.expect("exactly one is set and it is not `file`");
Ok(match key {
"git" => Baseline::Git { spec: value },
"git-merge-base" => Baseline::GitMergeBase { reference: value },
"tag" => Baseline::Tag { name: value },
"latest-tag" => Baseline::LatestTag { pattern: value },
"rev" => Baseline::Rev { rev: value },
other => unreachable!("unhandled baseline key `{other}`"),
})
}
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawSuppression {
rule: String,
endpoint: Option<String>,
field: Option<String>,
reason: String,
expires: Option<Expires>,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Expires {
Text(String),
Date(toml::value::Datetime),
}
impl std::fmt::Display for Expires {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Text(text) => formatter.write_str(text),
Self::Date(date) => write!(formatter, "{date}"),
}
}
}
impl RawSuppression {
fn validate(self, contract_name: &str) -> Result<Suppression, ConfigError> {
if self.rule.trim().is_empty() {
return Err(ConfigError::Validation(format!(
"contract `{contract_name}` suppression rule cannot be empty"
)));
}
if self.reason.trim().is_empty() {
return Err(ConfigError::Validation(format!(
"contract `{contract_name}` suppression for rule `{}` must include a non-empty reason",
self.rule
)));
}
let expires = self.expires.map(|expires| expires.to_string());
if let Some(expires) = &expires
&& crate::rules::parse_date(expires).is_none()
{
return Err(ConfigError::Validation(format!(
"contract `{contract_name}` suppression for rule `{}` has an unreadable \
`expires` value `{expires}`; use YYYY-MM-DD",
self.rule
)));
}
if crate::rules::catalogue::lookup(&self.rule).is_none() {
return Err(ConfigError::Validation(format!(
"contract `{contract_name}` suppresses unknown rule `{}`; \
run `brake explain <rule-id>` to see the catalogue",
self.rule
)));
}
Ok(Suppression {
rule: self.rule,
endpoint: self.endpoint,
field: self.field,
reason: self.reason,
expires,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn expires_accepts_both_spellings_of_a_date() {
let with = |expires: &str| {
format!(
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"a.yaml\"\n\
[[contract.allow]]\nrule=\"endpoint-removed\"\nreason=\"r\"\n\
expires={expires}\n"
)
};
for spelling in ["\"2026-09-01\"", "2026-09-01"] {
let config = Config::parse(&with(spelling))
.unwrap_or_else(|error| panic!("`expires={spelling}` should parse: {error}"));
assert_eq!(
config.contracts[0].allow[0].expires.as_deref(),
Some("2026-09-01"),
"both spellings must normalise to the same date"
);
}
}
#[test]
fn expires_still_rejects_something_that_is_not_a_date() {
let config = Config::parse(
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"a.yaml\"\n\
[[contract.allow]]\nrule=\"endpoint-removed\"\nreason=\"r\"\n\
expires=\"soon\"\n",
);
let error = config.expect_err("`soon` is not a date").to_string();
assert!(error.contains("YYYY-MM-DD"), "{error}");
}
#[test]
fn parses_defaults_contract_and_suppression() {
let config = Config::parse(
r#"
[defaults]
compatibility = "wire-json"
baseline = { file = "api/default-openapi.baseline.yaml" }
[[contract]]
name = "payments"
format = "openapi"
source = "api/payments-openapi.yaml"
[[contract.allow]]
rule = "response-field-removed"
endpoint = "GET /payments/{id}"
field = "legacy_reference"
reason = "Removed after deprecation window"
expires = "2026-09-01"
"#,
)
.expect("config should parse");
assert_eq!(config.defaults.compatibility, Compatibility::WireJson);
assert_eq!(
config.defaults.baseline,
Some(Baseline::File(PathBuf::from(
"api/default-openapi.baseline.yaml"
)))
);
assert_eq!(config.contracts.len(), 1);
assert_eq!(config.contracts[0].name, "payments");
assert_eq!(config.contracts[0].allow.len(), 1);
assert!(config.contracts[0].generated.is_none());
}
#[test]
fn suppression_requires_reason() {
let error = Config::parse(
r#"
[[contract]]
name = "payments"
format = "openapi"
source = "api/payments-openapi.yaml"
[[contract.allow]]
rule = "response-field-removed"
endpoint = "GET /payments/{id}"
field = "legacy_reference"
"#,
)
.expect_err("suppression without reason must fail");
assert!(error.to_string().contains("reason"));
}
#[test]
fn baseline_requires_exactly_one_strategy() {
let error = Config::parse(
r#"
[defaults]
baseline = { file = "a.yaml", git = "origin/main:api/a.yaml" }
"#,
)
.expect_err("multiple baseline strategies must fail");
assert!(error.to_string().contains("exactly one"));
}
#[test]
fn default_compatibility_is_wire_json() {
let config = Config::parse(
r#"
[[contract]]
name = "ledger"
format = "openapi"
source = "api/ledger-openapi.yaml"
"#,
)
.expect("config should parse");
assert_eq!(config.defaults.compatibility, Compatibility::WireJson);
assert_eq!(
config.contracts[0].effective_compatibility(&config.defaults),
Compatibility::WireJson
);
}
#[test]
fn parses_generated_command() {
let config = Config::parse(
r#"
[[contract]]
name = "payments"
format = "openapi"
source = "api/payments-openapi.yaml"
generated = { command = "echo generated" }
"#,
)
.expect("config should parse");
assert_eq!(
config.contracts[0]
.generated
.as_ref()
.expect("generated config")
.command,
"echo generated"
);
}
}