redup_abstractions/
problem.rs1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub struct Problem {
8 source: String,
9 message: String,
10}
11
12impl Problem {
13 pub fn new(source: impl Into<String>, message: impl Into<String>) -> Self {
15 Self {
16 source: source.into(),
17 message: message.into(),
18 }
19 }
20
21 pub fn source(&self) -> &str {
23 &self.source
24 }
25
26 pub fn message(&self) -> &str {
28 &self.message
29 }
30}
31
32impl fmt::Display for Problem {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 write!(f, "{}: {}", self.source, self.message)
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use super::Problem;
41
42 #[test]
43 fn text_is_source_and_message() {
44 let problem = Problem::new("cache", "cache is unavailable");
45 assert_eq!(problem.to_string(), "cache: cache is unavailable");
46 assert_eq!(problem.source(), "cache");
47 assert_eq!(problem.message(), "cache is unavailable");
48 }
49}