Skip to main content

debian_workspace/
lib.rs

1//! Utilities for reading and writing a Debian package working tree.
2
3#![deny(missing_docs)]
4
5/// Actions that describe changes to apply to a working tree.
6pub mod action;
7
8/// Apply [`action::Action`]s to a working tree.
9pub mod appliers;
10
11/// Operate on a Debian package.
12pub mod workspace;
13
14/// A workspace implementation on a filesystem.
15pub mod fs_workspace;
16
17/// Allow notification on certain events.
18pub mod trigger;
19
20pub use debversion::Version;
21pub use trigger::{ChangelogAspect, Trigger, WatchAspect};
22pub use workspace::{Workspace, compat_level};
23
24/// Errors that can occur while reading or writing workspace files.
25#[derive(Debug)]
26pub enum Error {
27    /// The file is missing; the caller should treat this as "nothing to do".
28    NotFound,
29    /// An I/O error other than file-not-found.
30    Io(std::io::Error),
31    /// The file could not be parsed.
32    Parse(String),
33    /// Any other error.
34    Other(String),
35    /// A required external tool is not installed.
36    MissingDependency(String),
37}
38
39impl std::fmt::Display for Error {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            Error::NotFound => write!(f, "file not found"),
43            Error::Io(e) => write!(f, "I/O error: {}", e),
44            Error::Parse(msg) => write!(f, "parse error: {}", msg),
45            Error::Other(msg) => write!(f, "{}", msg),
46            Error::MissingDependency(dep) => write!(f, "missing dependency: {}", dep),
47        }
48    }
49}
50
51impl std::error::Error for Error {
52    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53        match self {
54            Error::Io(e) => Some(e),
55            _ => None,
56        }
57    }
58}
59
60impl From<debian_analyzer::editor::EditorError> for Error {
61    fn from(e: debian_analyzer::editor::EditorError) -> Self {
62        match e {
63            debian_analyzer::editor::EditorError::IoError(e) => Error::Io(e),
64            debian_analyzer::editor::EditorError::BrzError(e) => Error::Other(e.to_string()),
65            debian_analyzer::editor::EditorError::GeneratedFile(p, _) => {
66                Error::Other(format!("generated file: {}", p.display()))
67            }
68            debian_analyzer::editor::EditorError::FormattingUnpreservable(p, _) => {
69                Error::Other(format!("formatting unpreservable: {}", p.display()))
70            }
71            debian_analyzer::editor::EditorError::TemplateError(p, _) => {
72                Error::Other(format!("template error in {}", p.display()))
73            }
74        }
75    }
76}
77
78impl From<std::io::Error> for Error {
79    fn from(e: std::io::Error) -> Self {
80        if e.kind() == std::io::ErrorKind::NotFound {
81            Error::NotFound
82        } else {
83            Error::Io(e)
84        }
85    }
86}