Skip to main content

chess_lab/common/errors/
fen.rs

1use thiserror::Error;
2
3/// An error that occurs when parsing a FEN string
4///
5#[derive(Debug, Error, PartialEq)]
6#[error("Invalid FEN: {fen}")]
7pub struct FenError {
8    /// The FEN string that caused the error
9    pub fen: String,
10}
11
12impl FenError {
13    /// Creates a new [FenError] with the given FEN string
14    ///
15    /// # Arguments
16    /// * `fen` - The FEN string that caused the error
17    ///
18    /// # Example
19    /// ```
20    /// # use chess_lab::errors::FenError;
21    /// let error = FenError::new("invalid_fen".to_string());
22    /// ```
23    ///
24    pub fn new(fen: String) -> Self {
25        FenError { fen }
26    }
27}
28
29/// An error that occurs when an invalid piece representation is encountered
30///
31#[derive(Debug, Error, PartialEq)]
32#[error("Invalid piece representation: {piece_repr}")]
33pub struct PieceReprError {
34    /// The piece character that caused the error
35    pub piece_repr: char,
36}
37
38impl PieceReprError {
39    /// Creates a new [PieceReprError] with the given [Piece](crate::core::Piece) representation
40    ///
41    /// # Arguments
42    /// * `piece_repr` - The char representation of a [Piece](crate::core::Piece) that caused the error
43    ///
44    /// # Example
45    /// ```
46    /// # use chess_lab::errors::PieceReprError;
47    /// let error = PieceReprError::new('X');
48    /// ```
49    ///
50    pub fn new(piece_repr: char) -> Self {
51        PieceReprError { piece_repr }
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_fen_error_creation() {
61        let fen = "invalid_fen".to_string();
62        let error = FenError::new(fen.clone());
63        assert_eq!(error.fen, fen);
64        assert_eq!(format!("{}", error), format!("Invalid FEN: {}", fen));
65    }
66
67    #[test]
68    fn test_piece_repr_error_creation() {
69        let piece_repr = 'X';
70        let error = PieceReprError::new(piece_repr);
71        assert_eq!(error.piece_repr, piece_repr);
72        assert_eq!(
73            format!("{}", error),
74            format!("Invalid piece representation: {}", piece_repr)
75        );
76    }
77}