Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.2.0528.5 KB2026-10-160.1.1542.8 KB2026-10-150.1.0541.1 KB2026-10-15
Version
0.2.0
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-10-16
Size
528.5 KB
Downloads
7
Checksum
f8871632f38084ae53dd9bf16e9ea90c80a209bf6cd234b6767630c5470dc305
Dependencies
None

error.rs

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
use std::fmt;


#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
    pub message: String,
    pub position: usize,
    pub in_font: bool,
}


impl Error {
    pub fn new(message: impl Into<String>, position: usize) -> Error {
        Error {message: message.into(), position, in_font: false}
    }

    pub fn font(message: impl Into<String>, position: usize) -> Error {
        Error {message: message.into(), position, in_font: true}
    }
}


impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let unit = if self.in_font {"byte"} else {"character"};

        write!(formatter, "{} ({unit} {})", self.message, self.position + 1)
    }
}


impl std::error::Error for Error {}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_font_error_counts_bytes() {
        assert_eq!(Error::new("mistake", 3).to_string(), "mistake (character 4)");
        assert_eq!(Error::font("mistake", 3).to_string(), "mistake (byte 4)");
    }
}