sgf_render/
errors.rs

1use sgf_parse::SgfParseError;
2
3#[derive(Debug)]
4pub enum GobanError {
5    ParseError(SgfParseError),
6    StyleDefError(minidom::Error),
7    InsufficientSgfNodes,
8    MissingGame,
9    MissingVariation,
10    InvalidMove,
11    InvalidRange,
12    UnlabellableRange,
13    InvalidSzProperty,
14}
15
16impl std::fmt::Display for GobanError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Self::ParseError(e) => write!(f, "{e}"),
20            Self::StyleDefError(e) => write!(f, "Invalid defs in style: {e}"),
21            Self::InvalidMove => write!(f, "Invalid move"),
22            Self::InsufficientSgfNodes => write!(f, "Insufficient SGF nodes found"),
23            Self::MissingGame => write!(f, "Selected game not found"),
24            Self::MissingVariation => write!(f, "Selected variation not found"),
25            Self::InvalidRange => write!(f, "Invalid range to render in goban"),
26            Self::UnlabellableRange => write!(f, "Range too large for use with labels"),
27            Self::InvalidSzProperty => write!(f, "SZ property invalid"),
28        }
29    }
30}
31
32impl std::error::Error for GobanError {}
33
34impl From<SgfParseError> for GobanError {
35    fn from(error: SgfParseError) -> Self {
36        Self::ParseError(error)
37    }
38}
39
40#[derive(Debug)]
41pub enum UsageError {
42    InvalidRange,
43    StyleReadError(Box<dyn std::error::Error>),
44    InvalidFirstMoveNumber,
45    InvalidLastMoveNumber,
46    InvalidBoardSides,
47    InvalidNodeNumber(String),
48    InvalidTextOutputOption(String),
49    InvalidTileSet,
50}
51
52impl std::fmt::Display for UsageError {
53    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
54        match self {
55            UsageError::InvalidRange => write!(f, "Invalid range"),
56            UsageError::StyleReadError(e) => write!(f, "Failed to read style file: {e}"),
57            UsageError::InvalidFirstMoveNumber => write!(f, "Invalid first move number"),
58            UsageError::InvalidLastMoveNumber => write!(f, "Invalid last move number"),
59            UsageError::InvalidBoardSides => write!(f, "Invalid board sides"),
60            UsageError::InvalidNodeNumber(s) => write!(f, "Invalid node number '{s}'"),
61            UsageError::InvalidTextOutputOption(s) => {
62                write!(f, "{s} not supported for text output")
63            }
64            UsageError::InvalidTileSet => write!(f, "Must be 11 characters long"),
65        }
66    }
67}
68
69impl std::error::Error for UsageError {}
70unsafe impl Send for UsageError {}
71unsafe impl Sync for UsageError {}
72
73#[derive(Debug)]
74pub enum QueryError {
75    ParseError(SgfParseError),
76    IoError(std::io::Error),
77    GameNotFound,
78    VariationNotFound,
79}
80
81impl std::fmt::Display for QueryError {
82    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
83        match self {
84            QueryError::ParseError(e) => write!(f, "{e}"),
85            QueryError::IoError(e) => write!(f, "{e}"),
86            QueryError::GameNotFound => write!(f, "Game not found."),
87            QueryError::VariationNotFound => write!(f, "Variation not found."),
88        }
89    }
90}
91
92impl std::error::Error for QueryError {}
93
94impl From<SgfParseError> for QueryError {
95    fn from(error: SgfParseError) -> Self {
96        Self::ParseError(error)
97    }
98}
99
100impl From<std::io::Error> for QueryError {
101    fn from(error: std::io::Error) -> Self {
102        Self::IoError(error)
103    }
104}