Skip to main content

redup_abstractions/
problem.rs

1//! Проблема состояния.
2
3use std::fmt;
4
5/// Проблема: откуда она и в чём. Работа с непустым списком проблем — деградация, а не отдельная фаза.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub struct Problem {
8    source: String,
9    message: String,
10}
11
12impl Problem {
13    /// Проблема с источником — именем зависимости или меткой — и текстом на английском.
14    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    /// Источник: имя зависимости или метка проблемы.
22    pub fn source(&self) -> &str {
23        &self.source
24    }
25
26    /// Текст на английском.
27    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}