Skip to main content

gravityfile_ops/
error.rs

1//! Per-path operation diagnostics.
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7/// A problem with one path during an operation.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct OperationError {
10    pub path: PathBuf,
11    pub message: String,
12    /// When nonzero, this record stands in for that many omitted diagnostics.
13    #[serde(default)]
14    pub omitted_count: usize,
15}
16
17impl OperationError {
18    pub fn new(path: PathBuf, message: impl Into<String>) -> Self {
19        Self {
20            path,
21            message: message.into(),
22            omitted_count: 0,
23        }
24    }
25
26    /// Number of diagnostics this record represents.
27    pub fn represented_count(&self) -> usize {
28        self.omitted_count.max(1)
29    }
30
31    pub(crate) fn omitted(count: usize) -> Self {
32        Self {
33            path: PathBuf::new(),
34            message: format!("{count} more problem(s) not listed"),
35            omitted_count: count,
36        }
37    }
38}
39
40impl std::fmt::Display for OperationError {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        if self.path.as_os_str().is_empty() {
43            f.write_str(&self.message)
44        } else {
45            write!(f, "{}: {}", self.path.display(), self.message)
46        }
47    }
48}