Debian Codegraph @

src/error.rs

raw symbols SCIP
1 //! There be Errors here.
2
3 use std::fmt;
4
5 use cxx::Exception;
6 #[doc(inline)]
7 pub use raw::{empty, pending_error, AptError};
8
9 #[cxx::bridge]
10 pub(crate) mod raw {
11 /// Representation of a single Apt Error or Warning
12 #[derive(Debug)]
13 struct AptError {
14 /// * [`true`] = Error.
15 /// * [`false`] = Warning, Notice, etc.
16 pub is_error: bool,
17 /// The String version of the Error.
18 pub msg: String,
19 }
20
21 unsafe extern "C++" {
22 include!("rust-apt/apt-pkg-c/error.h");
23
24 /// Returns [`true`] if there are any pending Apt Errors.
25 pub fn pending_error() -> bool;
26
27 /// Returns [`true`] if there are no Errors or Warnings.
28 pub fn empty() -> bool;
29
30 /// Returns all Apt Errors or Warnings.
31 pub fn get_all() -> Vec<AptError>;
32 }
33 }
34
35 impl fmt::Display for AptError {
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 if self.is_error {
38 write!(f, "E: {}", self.msg)?;
39 } else {
40 write!(f, "W: {}", self.msg)?;
41 }
42
43 Ok(())
44 }
45 }
46
47 impl std::error::Error for AptError {}
48
49 /// Struct that represents multiple apt errors and warnings.
50 ///
51 /// This is essentially just a wrapper around [`Vec<AptError>`]
52 #[derive(Debug)]
53 pub struct AptErrors {
54 pub(crate) ptr: Vec<AptError>,
55 }
56
57 impl AptErrors {
58 pub fn new() -> AptErrors {
59 AptErrors {
60 ptr: raw::get_all(),
61 }
62 }
63 }
64
65 impl Default for AptErrors {
66 fn default() -> Self { Self::new() }
67 }
68
69 impl fmt::Display for AptErrors {
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 for error in self.iter() {
72 writeln!(f, "{error}")?;
73 }
74 Ok(())
75 }
76 }
77
78 impl From<String> for AptErrors {
79 fn from(err: String) -> Self {
80 AptErrors {
81 ptr: vec![AptError {
82 is_error: true,
83 msg: err,
84 }],
85 }
86 }
87 }
88
89 impl From<Exception> for AptErrors {
90 fn from(err: Exception) -> Self {
91 if err.what() == "convert to AptErrors" {
92 return AptErrors::new();
93 }
94 // The times where it's not an Apt error to be converted are slim
95 AptErrors::from(err.what().to_string())
96 }
97 }
98
99 impl From<std::io::Error> for AptErrors {
100 fn from(err: std::io::Error) -> Self { AptErrors::from(err.to_string()) }
101 }
102
103 impl std::error::Error for AptErrors {}