1use isoloom_plan::Refusal;
5use isoloom_spec::model::Target;
6
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
9#[non_exhaustive]
10pub enum Error {
11 #[error("{}: {refusal}", target.id())]
13 Refused {
14 target: Target,
16 refusal: Refusal,
18 },
19 #[error("{}: {what}", target.id())]
21 Unsupported {
22 target: Target,
24 what: String,
26 },
27}
28
29impl Error {
30 #[must_use]
32 pub fn target(&self) -> Target {
33 match self {
34 Error::Refused { target, .. } | Error::Unsupported { target, .. } => *target,
35 }
36 }
37
38 #[must_use]
40 pub fn reason(&self) -> String {
41 match self {
42 Error::Refused { refusal, .. } => refusal.to_string(),
43 Error::Unsupported { what, .. } => what.clone(),
44 }
45 }
46}
47
48pub type Result<T, E = Error> = std::result::Result<T, E>;
50
51#[cfg(test)]
52mod tests {
53 use isoloom_plan::{Refusal, Requirement};
54 use isoloom_spec::model::Target;
55
56 use super::Error;
57
58 #[test]
59 fn each_error_reads_as_its_target_then_why() {
60 let refused = Error::Refused {
61 target: Target::Kubernetes,
62 refusal: Refusal {
63 requirement: Requirement::MachineAliases,
64 },
65 };
66 assert_eq!(
67 refused.to_string(),
68 "kubernetes: machine `aliases` (dotted DNS names) aren't Kubernetes Service names"
69 );
70 let unsupported = Error::Unsupported {
71 target: Target::Proxmox,
72 what: "machine `w`: no image".to_owned(),
73 };
74 assert_eq!(unsupported.to_string(), "proxmox: machine `w`: no image");
75 assert_eq!((refused.target(), unsupported.target()), (Target::Kubernetes, Target::Proxmox));
76 assert_eq!(unsupported.reason(), "machine `w`: no image");
77 assert_eq!(refused.reason(), "machine `aliases` (dotted DNS names) aren't Kubernetes Service names");
78 }
79}