1use std::io;
4
5use thiserror::Error;
6
7pub type DomainResult<T> = Result<T, DomainError>;
9
10#[derive(Debug, Error)]
12pub enum DomainError {
13 #[error("I/O failure while {context}: {source}")]
15 Io {
16 context: &'static str,
18 #[source]
20 source: io::Error,
21 },
22
23 #[error("{entity} not found: {id}")]
25 NotFound {
26 entity: &'static str,
28 id: String,
30 },
31
32 #[error("Ambiguous {entity} target '{input}'. Matches: {matches}")]
34 AmbiguousTarget {
35 entity: &'static str,
37 input: String,
39 matches: String,
41 },
42}
43
44impl DomainError {
45 pub fn io(context: &'static str, source: io::Error) -> Self {
47 Self::Io { context, source }
48 }
49
50 pub fn not_found(entity: &'static str, id: impl Into<String>) -> Self {
52 Self::NotFound {
53 entity,
54 id: id.into(),
55 }
56 }
57
58 pub fn ambiguous_target(entity: &'static str, input: &str, matches: &[String]) -> Self {
60 Self::AmbiguousTarget {
61 entity,
62 input: input.to_string(),
63 matches: matches.join(", "),
64 }
65 }
66}