1use std::fmt;
4
5use cxx::Exception;
6#[doc(inline)]
7pub use raw::{empty, pending_error, AptError};
8
9#[cxx::bridge]
10pub(crate) mod raw {
11 #[derive(Debug)]
13 struct AptError {
14 pub is_error: bool,
17 pub msg: String,
19 }
20
21 unsafe extern "C++" {
22 include!("rust-apt/apt-pkg-c/error.h");
23
24 pub fn pending_error() -> bool;
26
27 pub fn empty() -> bool;
29
30 pub fn get_all() -> Vec<AptError>;
32 }
33}
34
35impl 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
47impl std::error::Error for AptError {}
48
49#[derive(Debug)]
53pub struct AptErrors {
54 pub(crate) ptr: Vec<AptError>,
55}
56
57impl AptErrors {
58 pub fn new() -> AptErrors {
59 AptErrors {
60 ptr: raw::get_all(),
61 }
62 }
63}
64
65impl Default for AptErrors {
66 fn default() -> Self { Self::new() }
67}
68
69impl 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
78impl 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
89impl From<Exception> for AptErrors {
90 fn from(err: Exception) -> Self {
91 if err.what() == "convert to AptErrors" {
92 return AptErrors::new();
93 }
94 AptErrors::from(err.what().to_string())
96 }
97}
98
99impl From<std::io::Error> for AptErrors {
100 fn from(err: std::io::Error) -> Self { AptErrors::from(err.to_string()) }
101}
102
103impl std::error::Error for AptErrors {}