use std::collections::HashSet;
use std::error::Error;
use std::fmt;
use std::panic::Location;
use crate::key::{BindingKind, Key, KeyName, role_spelling};
use crate::module::{ModuleName, colliding_names};
use crate::redact::Redacted;
use crate::runtime::Runtime;
use crate::scope::ScopeKind;
use crate::timer::Timer;
use crate::type_name::{TypeName, short_type_name};
pub struct WiringErrors {
errors: Vec<WiringError>,
}
impl WiringErrors {
pub(crate) fn new(errors: Vec<WiringError>) -> Self {
WiringErrors { errors }
}
pub fn iter(&self) -> std::slice::Iter<'_, WiringError> {
self.errors.iter()
}
pub fn len(&self) -> usize {
self.errors.len()
}
pub fn is_empty(&self) -> bool {
self.errors.is_empty()
}
}
impl<'a> IntoIterator for &'a WiringErrors {
type Item = &'a WiringError;
type IntoIter = std::slice::Iter<'a, WiringError>;
fn into_iter(self) -> Self::IntoIter {
self.errors.iter()
}
}
impl fmt::Display for WiringErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let count = self.errors.len();
let noun = if count == 1 { "error" } else { "errors" };
write!(f, "error: wiring failed with {count} {noun}")?;
let full = Collisions {
types: TypeName::colliding(self.errors.iter().flat_map(WiringError::type_names)),
modules: colliding_names(self.errors.iter().flat_map(|error| error.module_names())),
};
for error in &self.errors {
let text = Rendered { error, full: &full }.to_string();
let mut lines = text.lines();
if let Some(first) = lines.next() {
write!(f, "\n\n × {first}")?;
}
for line in lines {
write!(f, "\n {line}")?;
}
}
Ok(())
}
}
impl fmt::Debug for WiringErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl Error for WiringErrors {}
#[non_exhaustive]
#[derive(Debug)]
pub enum WiringError {
ImportCycle { path: Vec<ModuleName> },
ReexportNotVisible { module: ModuleName, key: KeyName, at: &'static Location<'static> },
ReexportAmbiguous { module: ModuleName, key: KeyName, sources: Vec<ModuleName>, at: &'static Location<'static> },
KeyedInput { module: ModuleName, key: KeyName, at: &'static Location<'static> },
ExportNotBound { module: ModuleName, key: KeyName, imported: bool, near: Option<KeyName>, at: &'static Location<'static> },
DuplicateBinding { key: KeyName, module: ModuleName, first: &'static Location<'static>, second: &'static Location<'static> },
KindMix { key: KeyName, module: ModuleName, single: &'static Location<'static>, collection: &'static Location<'static> },
DanglingAlias { alias: KeyName, target: KeyName, module: ModuleName, at: &'static Location<'static> },
ValueFailed { module: ModuleName, key: KeyName, error: Redacted, at: &'static Location<'static> },
QualifiedRoleContribution { key: KeyName, module: ModuleName, at: &'static Location<'static> },
SingleRoleBinding { key: KeyName, module: ModuleName, at: &'static Location<'static> },
DuplicateReadiness { key: KeyName, module: ModuleName, first: &'static Location<'static>, second: &'static Location<'static> },
InputConflict { key: KeyName, first: InputOrigin, second: InputOrigin },
OverrideUnmatched { key: KeyName, keyed: Option<ModuleName>, at: &'static Location<'static> },
OverrideAmbiguous { key: KeyName, matches: Vec<ModuleName>, at: &'static Location<'static> },
OverrideModuleAmbiguous { module: &'static str, candidates: Vec<ModuleName>, at: &'static Location<'static> },
OverrideKind { key: KeyName, expected: BindingKind, found: BindingKind, at: &'static Location<'static> },
TimerOverride { at: &'static Location<'static> },
RuntimeOverride { at: &'static Location<'static> },
ReplacementMissingExports { original: ModuleName, replacement: ModuleName, missing: Vec<KeyName> },
ReplacementUnmatched { original: ModuleName, at: &'static Location<'static> },
DuplicateReplacement { original: ModuleName, first: &'static Location<'static>, second: &'static Location<'static> },
UnreadPrefix { controller: KeyName, module: ModuleName, prefix: String, transports: Vec<TypeName>, at: &'static Location<'static> },
Missing { key: KeyName, consumer: String, module: ModuleName, near: Option<(KeyName, ModuleName)> },
Ambiguous { key: KeyName, consumer: String, module: ModuleName, sources: Vec<(ModuleName, &'static Location<'static>)> },
Cycle { path: Vec<(KeyName, ModuleName)> },
ScopeViolation { binding: KeyName, declared: ScopeKind, by_closure: bool, module: ModuleName, path: Vec<String> },
HooksOnPerExecution { binding: KeyName, module: ModuleName },
ClosureScopeViolation {
closure: String,
transport: TypeName,
role: &'static str,
path: Vec<String>,
at: &'static Location<'static>,
},
ClosureNeedsExecution { closure: String, path: Vec<String>, at: Option<&'static Location<'static>> },
InputNotSeeded { handler: String, transport: TypeName, input: KeyName, seeder: TypeName, steps: Vec<String> },
BoundWithoutTimer { item: String, at: &'static Location<'static> },
BackoffWithoutTimer { binding: String, at: &'static Location<'static> },
KnobWithoutTimer { knob: &'static str },
}
impl WiringError {
fn key_names(&self) -> Vec<&KeyName> {
match self {
WiringError::ReexportNotVisible { key, .. }
| WiringError::ReexportAmbiguous { key, .. }
| WiringError::KeyedInput { key, .. }
| WiringError::DuplicateBinding { key, .. }
| WiringError::KindMix { key, .. }
| WiringError::ValueFailed { key, .. }
| WiringError::QualifiedRoleContribution { key, .. }
| WiringError::SingleRoleBinding { key, .. }
| WiringError::DuplicateReadiness { key, .. }
| WiringError::InputConflict { key, .. }
| WiringError::OverrideUnmatched { key, .. }
| WiringError::OverrideAmbiguous { key, .. }
| WiringError::OverrideKind { key, .. }
| WiringError::Ambiguous { key, .. } => vec![key],
WiringError::ExportNotBound { key, near, .. } => std::iter::once(key).chain(near).collect(),
WiringError::DanglingAlias { alias, target, .. } => vec![alias, target],
WiringError::ReplacementMissingExports { missing, .. } => missing.iter().collect(),
WiringError::Missing { key, near, .. } => std::iter::once(key).chain(near.iter().map(|(bound, _)| bound)).collect(),
WiringError::Cycle { path } => path.iter().map(|(key, _)| key).collect(),
WiringError::ScopeViolation { binding, .. } | WiringError::HooksOnPerExecution { binding, .. } => vec![binding],
WiringError::InputNotSeeded { input, .. } => vec![input],
WiringError::UnreadPrefix { controller, .. } => vec![controller],
WiringError::ImportCycle { .. }
| WiringError::OverrideModuleAmbiguous { .. }
| WiringError::TimerOverride { .. }
| WiringError::RuntimeOverride { .. }
| WiringError::ReplacementUnmatched { .. }
| WiringError::DuplicateReplacement { .. }
| WiringError::ClosureNeedsExecution { .. }
| WiringError::ClosureScopeViolation { .. }
| WiringError::BoundWithoutTimer { .. }
| WiringError::BackoffWithoutTimer { .. }
| WiringError::KnobWithoutTimer { .. } => Vec::new(),
}
}
fn type_names(&self) -> Vec<TypeName> {
let keys = self.key_names().into_iter().flat_map(|name| name.key().type_names());
let transports = match self {
WiringError::InputNotSeeded { transport, seeder, .. } => vec![*transport, *seeder],
WiringError::ClosureScopeViolation { transport, .. } => vec![*transport],
WiringError::UnreadPrefix { transports, .. } => transports.clone(),
WiringError::InputConflict { first, second, .. } => first.type_names().into_iter().chain(second.type_names()).collect(),
WiringError::ImportCycle { .. }
| WiringError::ReexportNotVisible { .. }
| WiringError::ReexportAmbiguous { .. }
| WiringError::KeyedInput { .. }
| WiringError::ExportNotBound { .. }
| WiringError::DuplicateBinding { .. }
| WiringError::KindMix { .. }
| WiringError::DanglingAlias { .. }
| WiringError::ValueFailed { .. }
| WiringError::QualifiedRoleContribution { .. }
| WiringError::SingleRoleBinding { .. }
| WiringError::DuplicateReadiness { .. }
| WiringError::OverrideUnmatched { .. }
| WiringError::OverrideAmbiguous { .. }
| WiringError::OverrideModuleAmbiguous { .. }
| WiringError::OverrideKind { .. }
| WiringError::TimerOverride { .. }
| WiringError::RuntimeOverride { .. }
| WiringError::ReplacementMissingExports { .. }
| WiringError::ReplacementUnmatched { .. }
| WiringError::DuplicateReplacement { .. }
| WiringError::Missing { .. }
| WiringError::Ambiguous { .. }
| WiringError::Cycle { .. }
| WiringError::ScopeViolation { .. }
| WiringError::HooksOnPerExecution { .. }
| WiringError::ClosureNeedsExecution { .. }
| WiringError::BoundWithoutTimer { .. }
| WiringError::BackoffWithoutTimer { .. }
| WiringError::KnobWithoutTimer { .. } => Vec::new(),
};
keys.chain(transports).collect()
}
fn module_names(&self) -> Vec<&ModuleName> {
match self {
WiringError::ImportCycle { path } => path.iter().collect(),
WiringError::ReexportNotVisible { module, .. }
| WiringError::KeyedInput { module, .. }
| WiringError::ExportNotBound { module, .. }
| WiringError::DuplicateBinding { module, .. }
| WiringError::KindMix { module, .. }
| WiringError::DanglingAlias { module, .. }
| WiringError::ValueFailed { module, .. }
| WiringError::QualifiedRoleContribution { module, .. }
| WiringError::SingleRoleBinding { module, .. }
| WiringError::DuplicateReadiness { module, .. }
| WiringError::ScopeViolation { module, .. }
| WiringError::HooksOnPerExecution { module, .. }
| WiringError::UnreadPrefix { module, .. } => vec![module],
WiringError::ReexportAmbiguous { module, sources, .. } => std::iter::once(module).chain(sources).collect(),
WiringError::OverrideUnmatched { keyed, .. } => keyed.iter().collect(),
WiringError::OverrideAmbiguous { matches, .. } => matches.iter().collect(),
WiringError::OverrideModuleAmbiguous { candidates, .. } => candidates.iter().collect(),
WiringError::ReplacementMissingExports { original, replacement, .. } => vec![original, replacement],
WiringError::ReplacementUnmatched { original, .. } | WiringError::DuplicateReplacement { original, .. } => {
vec![original]
}
WiringError::Missing { module, near, .. } => {
std::iter::once(module).chain(near.iter().map(|(_, exporter)| exporter)).collect()
}
WiringError::Ambiguous { module, sources, .. } => {
std::iter::once(module).chain(sources.iter().map(|(source, _)| source)).collect()
}
WiringError::Cycle { path } => path.iter().map(|(_, module)| module).collect(),
WiringError::InputConflict { first, second, .. } => first.module().into_iter().chain(second.module()).collect(),
WiringError::OverrideKind { .. }
| WiringError::TimerOverride { .. }
| WiringError::RuntimeOverride { .. }
| WiringError::ClosureNeedsExecution { .. }
| WiringError::ClosureScopeViolation { .. }
| WiringError::InputNotSeeded { .. }
| WiringError::BoundWithoutTimer { .. }
| WiringError::BackoffWithoutTimer { .. }
| WiringError::KnobWithoutTimer { .. } => Vec::new(),
}
}
fn render(&self, f: &mut fmt::Formatter<'_>, full: &Collisions) -> fmt::Result {
let show = |name: &KeyName| name.text_in(&full.types);
let show_module =
|name: &ModuleName| if full.modules.contains(name) { format!("{name:#}") } else { name.to_string() };
let type_text = |name: &KeyName| name.key().type_name().written(&full.types);
let show_type = |name: TypeName| name.written(&full.types);
match self {
WiringError::ImportCycle { path } => tree(
f,
format!("import cycle: {}", closed_loop(path.iter().map(show_module).collect())),
vec!["help: move what the modules in the cycle share into a module each of them imports".to_owned()],
),
WiringError::ReexportNotVisible { module, key, at } => {
let key = show(key);
let module = show_module(module);
tree(
f,
format!("re-export of `{key}` from {module}, which {module} cannot see"),
vec![
format!("declared at {}", place(at)),
format!("help: import a module that exports `{key}`, or remove the re-export"),
],
)
}
WiringError::ReexportAmbiguous { module, key, sources, at } => {
let key = show(key);
let module = show_module(module);
let mut items: Vec<String> = sources.iter().map(|s| format!("exported by {}", show_module(s))).collect();
items.push(format!("declared at {}", place(at)));
items.push(format!("help: import `{key}` into {module} from one module only"));
tree(f, format!("ambiguous re-export of `{key}` in {module}"), items)
}
WiringError::KeyedInput { module, key, at } => tree(
f,
format!("execution input `{}` declared in the keyed module {}", show(key), show_module(module)),
vec![
format!("declared at {}", place(at)),
"help: inputs are app-wide and belong to transports; declare it in a module that is not keyed"
.to_owned(),
],
),
WiringError::ExportNotBound { module, key, imported, near, at } => {
let key = show(key);
let module = show_module(module);
let help = match near {
Some(near) => format!("help: {module} binds `{}`; the export names `{key}`", show(near)),
None if *imported => format!(
"help: an import of {module} provides `{key}`; re-export it with `reexport`, or bind it in {module}"
),
None => format!("help: bind `{key}` in {module}, or remove the export"),
};
tree(
f,
format!("export of `{key}` from {module}, which {module} does not bind"),
vec![format!("declared at {}", place(at)), help],
)
}
WiringError::DuplicateBinding { key, module, first, second } => tree(
f,
format!("duplicate binding for `{}` in {}", show(key), show_module(module)),
vec![
format!("first bound at {}", place(first)),
format!("bound again at {}", place(second)),
"help: remove one, or give one a qualifier with `.qualified::<Q>()`".to_owned(),
],
),
WiringError::KindMix { key, module, single, collection } => tree(
f,
format!("`{}` is bound both as a single binding and as a collection in {}", show(key), show_module(module)),
vec![
format!("single binding at {}", place(single)),
format!("contribution at {}", place(collection)),
"help: contribute every entry with `contribute::<T>()` and read them as `Many<T>`, or bind it once"
.to_owned(),
],
),
WiringError::DanglingAlias { alias, target, module, at } => {
let target = show(target);
let module = show_module(module);
tree(
f,
format!("alias `{}` in {module} points at `{target}`, which {module} cannot see", show(alias)),
vec![
format!("declared at {}", place(at)),
format!("help: bind `{target}` in {module}, or import a module that exports it"),
],
)
}
WiringError::ValueFailed { module, key, error, at } => tree(
f,
format!("the value for `{}` in {} failed to build: {error}", show(key), show_module(module)),
vec![format!("recorded by `try_value` at {}", place(at))],
),
WiringError::QualifiedRoleContribution { key, module, at } => {
let role = role_spelling(&type_text(key));
tree(
f,
format!("a qualified contribution to the role key `{role}` in {} is read by no transport", show_module(module)),
vec![
format!("contributed as `{}` at {}", role_spelling(&show(key)), place(at)),
format!("help: contribute it unqualified; a transport reads only `{role}`"),
],
)
}
WiringError::SingleRoleBinding { key, module, at } => {
let role = role_spelling(&type_text(key));
tree(
f,
format!("a single binding under the role key `{role}` in {} is read by no transport", show_module(module)),
vec![
format!("bound as `{}` at {}", role_spelling(&show(key)), place(at)),
format!("help: contribute it: `into {role}: [..]`, or `m.contribute::<{role}>()`"),
],
)
}
WiringError::DuplicateReadiness { key, module, first, second } => tree(
f,
format!("two readiness checks on `{}` in {}", show(key), show_module(module)),
vec![
format!("first `.ready(..)` at {}", place(first)),
format!("second `.ready(..)` at {}", place(second)),
"help: fold both into one check".to_owned(),
],
),
WiringError::InputConflict { key, first, second } => tree(
f,
format!("execution input `{}` has two sources", show(key)),
vec![
first.line(&show_module, &show_type),
second.line(&show_module, &show_type),
"help: an input is seeded by the one transport that declares it, and no module binds it; remove the other source"
.to_owned(),
],
),
WiringError::OverrideUnmatched { key, keyed, at } => {
let key = show(key);
let help = match keyed {
Some(module) => format!(
"help: {} binds it unqualified, as a keyed module's bindings are; reach it with `.in_module_keyed::<M, Q>()` and no `.qualified`",
show_module(module)
),
None => format!("help: remove the override, or bind `{key}` in the module the test expects"),
};
tree(f, format!("override of `{key}` matches no binding"), vec![format!("declared at {}", place(at)), help])
}
WiringError::OverrideAmbiguous { key, matches, at } => {
let mut items: Vec<String> = matches.iter().map(|m| format!("bound in {}", show_module(m))).collect();
items.push(format!("declared at {}", place(at)));
items.push("help: scope it with `.in_module::<M>()`, or replace every match with `.everywhere()`".to_owned());
tree(f, format!("override of `{}` matches several bindings", show(key)), items)
}
WiringError::OverrideModuleAmbiguous { module, candidates, at } => {
let module = short_type_name(*module);
let mut items: Vec<String> = candidates.iter().map(|c| format!("instance {}", show_module(c))).collect();
items.push(format!("declared at {}", place(at)));
items.push(format!("help: name one with `.in_module_keyed::<{module}, Q>()` or `.in_module_of(&config)`"));
tree(f, format!("`.in_module::<{module}>()` matches several instances of {module}"), items)
}
WiringError::OverrideKind { key, expected, found, at } => tree(
f,
format!(
"override of `{}` would change its kind: the binding is {}, the override {}",
show(key),
kind_text(*expected),
kind_text(*found)
),
vec![format!("declared at {}", place(at)), "help: `override_many` replaces a whole collection".to_owned()],
),
WiringError::TimerOverride { at } => tree(
f,
"`override_value::<dyn Timer>` overrides app configuration, not a binding".to_owned(),
vec![format!("declared at {}", place(at)), "help: set it with `TestApp::timer(..)`".to_owned()],
),
WiringError::RuntimeOverride { at } => tree(
f,
"`override_value::<dyn Runtime>` overrides app configuration, not a binding".to_owned(),
vec![format!("declared at {}", place(at)), "help: set it with `TestApp::runtime(..)`".to_owned()],
),
WiringError::ReplacementMissingExports { original, replacement, missing } => {
let original = show_module(original);
let replacement = show_module(replacement);
let mut items: Vec<String> = missing.iter().map(|k| format!("missing `{}`", show(k))).collect();
items.push(format!("help: export the missing keys from {replacement}"));
tree(f, format!("{replacement} replaces {original} without exporting every key {original} exports"), items)
}
WiringError::ReplacementUnmatched { original, at } => {
let original = show_module(original);
tree(
f,
format!("`replace_module` replaces {original}, which no module imports"),
vec![
format!("declared at {}", place(at)),
format!("help: remove the replacement, or import {original} where the test expects it"),
],
)
}
WiringError::DuplicateReplacement { original, first, second } => tree(
f,
format!("two `replace_module` calls replace {}", show_module(original)),
vec![
format!("first `replace_module` at {}", place(first)),
format!("second `replace_module` at {}", place(second)),
"help: keep one; wiring applied only the first".to_owned(),
],
),
WiringError::UnreadPrefix { controller, module, prefix, transports, at } => {
let readers = if transports.is_empty() {
"it mounts no handler".to_owned()
} else {
let names: Vec<String> = transports.iter().map(|transport| show_type(*transport)).collect();
format!("its handlers belong to {}, which join no prefix to their routes", names.join(", "))
};
tree(
f,
format!("the prefix `{prefix}` on `{}` in {} is read by none of its handlers", show(controller), show_module(module)),
vec![
readers,
format!("set by `.at(..)` at {}", place(at)),
"help: remove the `.at(..)`; a prefix reaches HTTP routes and WebSocket gateway paths".to_owned(),
],
)
}
WiringError::Missing { key, consumer, module, near } => {
let builder = builder_help(key.key());
let key = show(key);
let module = show_module(module);
let help = match (builder, near) {
(Some(help), _) => help.to_owned(),
(None, Some((bound, exporter))) => {
format!("help: {} exports `{}`; the injection point reads `{key}`", show_module(exporter), show(bound))
}
(None, None) => format!("help: import a module that exports `{key}`, or provide it in {module}"),
};
tree(f, format!("missing dependency `{key}`"), vec![format!("needed by {consumer} in {module}"), help])
}
WiringError::Ambiguous { key, consumer, module, sources } => {
let shown: Vec<(String, &Location<'static>)> = sources.iter().map(|(m, at)| (show_module(m), *at)).collect();
let width = shown.iter().map(|(m, _)| m.chars().count()).max().unwrap_or(0);
let mut items = vec![format!("needed by {consumer}")];
items.extend(shown.iter().map(|(m, at)| format!("exported by {m:<width$} ({})", place(at))));
tree(f, format!("ambiguous dependency `{}` in {}", show(key), show_module(module)), items)
}
WiringError::Cycle { path } => {
let mut steps: &[(KeyName, ModuleName)] = path;
if let [first, .., last] = steps {
if first.0 == last.0 {
steps = &steps[..steps.len() - 1];
}
}
let head = format!("dependency cycle: {}", closed_loop(steps.iter().map(|(k, _)| show(k)).collect()));
tree(f, head, steps.iter().map(|(k, m)| format!("`{}` in {}", show(k), show_module(m))).collect())
}
WiringError::ScopeViolation { binding, declared, by_closure, module, path } => {
let short = binding.to_string();
let name = show(binding);
let module = show_module(module);
let head = match (declared, by_closure) {
(ScopeKind::Singleton, _) => {
format!("scope violation: singleton `{name}` in {module} depends on per-execution data")
}
(ScopeKind::Auto, false) => format!(
"scope violation: `{name}` in {module} is a provider, so a singleton, and depends on per-execution data"
),
(ScopeKind::Auto, true) => format!(
"scope violation: `{name}` in {module} is a provider declared by closure with no scope, so a singleton, and depends on per-execution data"
),
_ => format!("scope violation: `{name}` in {module} depends on per-execution data"),
};
let help = match (declared, by_closure) {
(_, true) => "help: declare the closure `with(execution) = ..`, or register it with `.execution(..)`".to_owned(),
(ScopeKind::Singleton, false) => format!("help: declare {name} #[injectable(execution)], or inject a factory"),
_ => "help: declare it #[injectable(execution)]".to_owned(),
};
let item = match path.first() {
None => help,
Some(first) if first.starts_with(&short) => format!("{}\n{help}", path.join(" → ")),
Some(_) => format!("{name} → {}\n{help}", path.join(" → ")),
};
tree(f, head, vec![item])
}
WiringError::HooksOnPerExecution { binding, module } => {
let binding = show(binding);
tree(
f,
format!("lifecycle hooks on `{binding}` in {}, which the scope pass inferred per-execution", show_module(module)),
vec![format!(
"help: hooks run on singletons only; move them to a singleton, or remove what makes `{binding}` need an execution"
)],
)
}
WiringError::ClosureNeedsExecution { closure, path, at } => {
let mut items = Vec::new();
if !path.is_empty() {
items.push(path.join(" → "));
}
if let Some(at) = at {
items.push(format!("declared at {}", place(at)));
}
items.push(
"help: hooks, readiness checks and metadata run where no execution exists; read singletons there, or take `ModuleRef` and open an execution with `execute`"
.to_owned(),
);
tree(f, format!("{closure} reads per-execution data"), items)
}
WiringError::ClosureScopeViolation { closure, transport, role, path, at } => {
let method = role.replace(' ', "_");
let mut items = Vec::new();
if !path.is_empty() {
items.push(path.join(" → "));
}
items.push(format!("declared at {}", place(at)));
items.push(format!(
"help: build it per call: drop the scope (`with = ..`, `{method}_with`), or declare it `with(execution) = ..` (`{method}_with_in::<PerExecution>`)"
));
tree(
f,
format!(
"scope violation: {closure} ({}), declared by closure as a singleton, depends on per-execution data",
show_type(*transport)
),
items,
)
}
WiringError::InputNotSeeded { handler, transport, input, seeder, steps } => {
let input = show(input);
let transport = show_type(*transport);
let seeder = show_type(*seeder);
let route: Vec<String> = std::iter::once(format!("{handler} ({transport})")).chain(steps.iter().cloned()).collect();
let route = route.join(" → ");
tree(
f,
format!("input `{input}` is seeded by {seeder}, read on a path from the {transport} handler {handler}"),
vec![route, format!("help: read it as `Option<Dep<{input}>>` where the path is shared across transports")],
)
}
WiringError::BoundWithoutTimer { item, at } => tree(
f,
format!("{item} needs a `Timer`, and the app has none"),
vec![
format!("declared at {}", place(at)),
"help: set one with `.timer(..)`, or leave the bound at its default or write `.unbounded()`".to_owned(),
],
),
WiringError::BackoffWithoutTimer { binding, at } => tree(
f,
format!("readiness `.backoff` of `{binding}` waits between attempts on a `Timer`, and the app has none"),
vec![format!("declared at {}", place(at)), "help: set one with `.timer(..)`, or remove the `.backoff(..)`".to_owned()],
),
WiringError::KnobWithoutTimer { knob } => tree(
f,
format!("`{knob}` is set on an app with no `Timer`"),
vec!["help: set one with `.timer(..)`; every timing knob is timed by it".to_owned()],
),
}
}
}
impl fmt::Display for WiringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let full = Collisions { types: TypeName::colliding(self.type_names()), modules: colliding_names(self.module_names()) };
self.render(f, &full)
}
}
#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum InputOrigin {
Transport { name: TypeName, at: &'static Location<'static> },
Module { module: ModuleName, seeders: Vec<TypeName>, at: &'static Location<'static> },
Binding { module: ModuleName, at: &'static Location<'static> },
}
impl InputOrigin {
fn module(&self) -> Option<&ModuleName> {
match self {
InputOrigin::Transport { .. } => None,
InputOrigin::Module { module, .. } | InputOrigin::Binding { module, .. } => Some(module),
}
}
fn type_names(&self) -> Vec<TypeName> {
match self {
InputOrigin::Transport { name, .. } => vec![*name],
InputOrigin::Module { seeders, .. } => seeders.clone(),
InputOrigin::Binding { .. } => Vec::new(),
}
}
fn line(&self, module: impl Fn(&ModuleName) -> String, ty: impl Fn(TypeName) -> String) -> String {
match self {
InputOrigin::Transport { name, at } => format!("declared by transport `{}` at {}", ty(*name), place(at)),
InputOrigin::Module { module: declared_in, seeders, at } => {
let noun = if seeders.len() == 1 { "seeder" } else { "seeders" };
let names: Vec<String> = seeders.iter().map(|&seeder| format!("`{}`", ty(seeder))).collect();
format!("declared in {} with {noun} {} at {}", module(declared_in), names.join(", "), place(at))
}
InputOrigin::Binding { module: bound_in, at } => format!("bound in {} at {}", module(bound_in), place(at)),
}
}
}
impl fmt::Display for InputOrigin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let full = f.alternate();
let types = TypeName::colliding(self.type_names());
f.write_str(&self.line(|name| if full { format!("{name:#}") } else { name.to_string() }, |name| name.written(&types)))
}
}
struct Collisions {
types: HashSet<TypeName>,
modules: HashSet<ModuleName>,
}
struct Rendered<'a> {
error: &'a WiringError,
full: &'a Collisions,
}
impl fmt::Display for Rendered<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.render(f, self.full)
}
}
fn builder_help(key: Key) -> Option<&'static str> {
if key == Key::of::<dyn Runtime, ()>() {
Some("help: set one on the app with `.runtime(..)`, which sets its timer too")
} else if key == Key::of::<dyn Timer, ()>() {
Some("help: set one on the app with `.timer(..)`, or with `.runtime(..)`, which sets both")
} else {
None
}
}
fn tree(f: &mut fmt::Formatter<'_>, head: String, items: Vec<String>) -> fmt::Result {
f.write_str(&head)?;
let last = items.len().saturating_sub(1);
for (i, item) in items.iter().enumerate() {
let (branch, continued) = if i == last { ("└─", " ") } else { ("├─", "│ ") };
let mut lines = item.lines();
if let Some(first) = lines.next() {
write!(f, "\n{branch} {first}")?;
}
for line in lines {
write!(f, "\n{continued}{line}")?;
}
}
Ok(())
}
fn closed_loop(mut steps: Vec<String>) -> String {
let closed = steps.len() > 1 && steps.first() == steps.last();
if !closed {
if let Some(first) = steps.first().cloned() {
steps.push(first);
}
}
steps.join(" → ")
}
fn place(at: &Location<'_>) -> String {
format!("{}:{}", at.file(), at.line())
}
fn kind_text(kind: BindingKind) -> &'static str {
match kind {
BindingKind::Single => "a single binding",
BindingKind::Collection => "a collection",
}
}