Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.1.098.9 KB2026-09-14
Version
0.1.0
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-09-14
Size
98.9 KB
Downloads
1
Checksum
bdac84a1e4fe4dd3505b93c2781b1f15833b5189c2e4ee976225fa3c512885cc
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! 読み書きで起きるエラー

use std::fmt;


/// 何に失敗したかと、どこで失敗したか
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
    /// 何が起きたかの説明
    pub message: String,
    /// 読んでいたパートの名前。パートに関わらないときは空
    pub source: String,
}


impl Error {
    /// パートに関わらないエラー
    pub fn new(message: impl Into<String>) -> Error {
        Error {message: message.into(), source: String::new()}
    }

    /// どのパートで起きたか分かっているエラー
    pub fn at(message: impl Into<String>, source: impl Into<String>) -> Error {
        Error {message: message.into(), source: source.into()}
    }

    /// パートの名前を後から足す。すでに入っているときは変えない
    ///
    /// 展開や XML の解釈は、どのパートを読んでいるかを知らない。
    /// 呼んだ側がここで名前を足すので、利用者には「どのファイルのどこ」まで届く。
    pub fn within(mut self, source: &str) -> Error {
        if self.source.is_empty() {
            self.source = source.to_string();
        }

        self
    }
}


impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        if self.source.is_empty() {
            return write!(formatter, "{}", self.message);
        }

        write!(formatter, "{} ({})", self.message, self.source)
    }
}


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


impl From<std::io::Error> for Error {
    fn from(error: std::io::Error) -> Error {
        Error::new(error.to_string())
    }
}