1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::fmt;

/// A type that represents an error when saving a tensor to a file.
#[derive(Debug)]
pub enum SaveError {
    /// The error occured when creating the file. Probably a wrong path.
    CreatingFile {
        filename: String,
        source: std::io::Error,
    },
    /// The error occured when writing the file.
    WritingFile {
        filename: String,
        source: bincode::Error,
    },
}

impl fmt::Display for SaveError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CreatingFile { filename, source } => {
                write!(f, "Failed to open file {}: {}", filename, source)
            }
            Self::WritingFile { filename, source } => {
                write!(f, "Failed to write file {}: {}", filename, source)
            }
        }
    }
}

impl std::error::Error for SaveError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::CreatingFile { source, .. } => Some(source),
            Self::WritingFile { source, .. } => Some(source),
        }
    }
}

/// A type representing an error when loading a tensor from a file.
#[derive(Debug)]
pub enum LoadError {
    /// The error occurred when opening the file. Probably a wrong path.
    OpeningFile {
        filename: String,
        source: std::io::Error,
    },
    /// The error occurred when reading the file.
    ReadingFile {
        filename: String,
        source: bincode::Error,
    },
}

impl fmt::Display for LoadError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::OpeningFile { filename, source } => {
                write!(f, "Failed to open file {}: {}", filename, source)
            }
            Self::ReadingFile { filename, source } => {
                write!(f, "Failed to read file {}: {}", filename, source)
            }
        }
    }
}

impl std::error::Error for LoadError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::OpeningFile { source, .. } => Some(source),
            Self::ReadingFile { source, .. } => Some(source),
        }
    }
}