1use std::path::PathBuf;
4
5use kallo_io::IoError;
6
7#[derive(Debug)]
9pub enum Error {
10 UnknownStock {
12 kind: &'static str,
14 id: String,
16 },
17 InvalidNumber {
19 flag: &'static str,
21 value: f64,
23 expected: &'static str,
25 },
26 CreateDir {
28 path: PathBuf,
30 source: std::io::Error,
32 },
33 Raw {
35 path: PathBuf,
37 message: String,
39 },
40 Read {
42 path: PathBuf,
44 source: IoError,
46 },
47 Film {
49 path: PathBuf,
51 message: String,
53 },
54 Stock {
56 path: PathBuf,
58 message: String,
60 },
61 Dictionary {
63 path: PathBuf,
65 message: String,
67 },
68 Gpu {
70 message: String,
72 },
73 Camera {
75 path: PathBuf,
77 message: String,
79 },
80 Lut {
82 path: PathBuf,
84 message: String,
86 },
87 Contact {
89 message: String,
91 },
92 Write {
94 path: PathBuf,
96 source: IoError,
98 },
99}
100
101impl std::fmt::Display for Error {
102 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103 match self {
104 Self::UnknownStock { kind, id } => {
105 write!(f, "unknown {kind} stock '{id}' (try `kalo stocks`)")
106 }
107 Self::InvalidNumber {
108 flag,
109 value,
110 expected,
111 } => {
112 write!(f, "{flag} must be {expected}, got {value}")
113 }
114 Self::CreateDir { path, source } => {
115 write!(f, "could not create {}: {source}", path.display())
116 }
117 Self::Raw { path, message } => {
118 write!(f, "could not decode raw file {}: {message}", path.display())
119 }
120 Self::Read { path, source } => {
121 write!(f, "could not read {}: {source}", path.display())
122 }
123 Self::Film { path, message } => {
124 write!(f, "could not load film {}: {message}", path.display())
125 }
126 Self::Dictionary { path, message } => {
127 write!(f, "could not read dictionary {}: {message}", path.display())
128 }
129 Self::Stock { path, message } => {
130 write!(f, "stock {}: {message}", path.display())
131 }
132 Self::Gpu { message } => write!(f, "{message}"),
133 Self::Camera { path, message } => {
134 write!(f, "camera sensitivity {}: {message}", path.display())
135 }
136 Self::Lut { path, message } => {
137 write!(f, "lookup table {}: {message}", path.display())
138 }
139 Self::Contact { message } => write!(f, "{message}"),
140 Self::Write { path, source } => {
141 write!(f, "could not write {}: {source}", path.display())
142 }
143 }
144 }
145}
146
147impl std::error::Error for Error {
148 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
149 match self {
150 Self::CreateDir { source, .. } => Some(source),
151 Self::Read { source, .. } | Self::Write { source, .. } => Some(source),
152 Self::UnknownStock { .. }
153 | Self::InvalidNumber { .. }
154 | Self::Film { .. }
155 | Self::Raw { .. }
156 | Self::Dictionary { .. }
157 | Self::Camera { .. }
158 | Self::Gpu { .. }
159 | Self::Lut { .. }
160 | Self::Stock { .. }
161 | Self::Contact { .. } => None,
162 }
163 }
164}