pages_and_pages/
error.rs

1use std::{
2    error::Error,
3    fmt::{self, Debug, Display},
4    io,
5};
6
7#[allow(clippy::exhaustive_structs)]
8#[derive(Debug)]
9/// Represents an [`io::Error`] when trying to convert an object to a pages object, while also
10/// containg the object the operation was attempted on
11pub struct IntoPagesError<T: Debug> {
12    /// The [`io::Error`] that occured when attempting the operation
13    pub err: io::Error,
14    /// The object the operation was attempted on
15    pub from: T,
16}
17
18#[allow(clippy::exhaustive_structs)]
19#[derive(Debug)]
20/// Represents an error when trying to join multiple pages objects into 1, while also containing
21/// the pages which were attempted to be joined.
22pub struct JoinError<T: Debug> {
23    /// Contains the specific error that occured when attempting the operation
24    pub kind: JoinErrorKind,
25    /// Contains the different pages that were trying to be joined
26    pub parts: T,
27}
28
29#[allow(clippy::exhaustive_enums)]
30#[derive(Debug)]
31/// Represents the different errors that could occur when trying to join pages objects
32pub enum JoinErrorKind {
33    /// An [`io::Error`] occured when trying to apply the correct protection to the newly joined
34    /// pages
35    IO(io::Error),
36    /// The pages that were trying to be joined were not part of the same allocation or were not
37    /// contiguous
38    NonContiguous,
39}
40
41impl<T: Debug> Display for IntoPagesError<T> {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        f.write_str("Error converting...\n")?;
44        Debug::fmt(&self.from, f)?;
45        f.write_str("\n... into a Pages object:\n")?;
46        Display::fmt(&self.err, f)
47    }
48}
49
50#[allow(clippy::missing_trait_methods)]
51impl<T: Debug> Error for IntoPagesError<T> {
52    fn source(&self) -> Option<&(dyn Error + 'static)> {
53        Some(&self.err)
54    }
55}
56
57impl<T: Debug> Display for JoinError<T> {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        f.write_str("Error joining...\n")?;
60        Debug::fmt(&self.parts, f)?;
61        f.write_str("\n... ")?;
62        Display::fmt(&self.kind, f)
63    }
64}
65
66#[allow(clippy::missing_trait_methods)]
67impl<T: Debug> Error for JoinError<T> {
68    fn source(&self) -> Option<&(dyn Error + 'static)> {
69        match self.kind {
70            JoinErrorKind::NonContiguous => None,
71            JoinErrorKind::IO(ref e) => Some(e),
72        }
73    }
74}
75
76impl Display for JoinErrorKind {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        match *self {
79            Self::NonContiguous => {
80                f.write_str("Provided pages are not contiguous or belong to different allocations")
81            }
82            Self::IO(ref e) => Display::fmt(e, f),
83        }
84    }
85}