Skip to main content

chess_lab/common/errors/
pgn.rs

1use thiserror::Error;
2
3use super::FenError;
4
5/// An error that occurs when parsing a PGN string
6///
7#[non_exhaustive]
8#[derive(Debug, Error)]
9pub enum PGNError {
10    /// The FEN string is invalid
11    #[error("Invalid FEN: {0}")]
12    InvalidFen(#[from] FenError),
13    /// The PGN string is invalid
14    #[error("Invalid PGN: {0}")]
15    InvalidPgn(String),
16    /// The file does not exist
17    #[error("No such file or directory: {0}")]
18    NoSuchFile(#[from] std::io::Error),
19    /// The metadata is invalid
20    #[error("Invalid or not supported metadata: {0}")]
21    InvalidMetadata(#[from] PGNMetadataError),
22    /// The variant does not exists
23    #[error("Invalid or not variant provided: {0}")]
24    InvalidVariant(String),
25}
26
27/// An error that occurs when parsing PGN metadata
28///
29/// # Attributes
30/// * `metadata` - The metadata that caused the error
31///
32#[derive(Debug, Error)]
33#[error("Invalid or not supported metadata: {metadata}")]
34pub struct PGNMetadataError {
35    /// The metadata key/value that caused the error
36    pub metadata: String,
37}
38
39impl PGNMetadataError {
40    /// Creates a new [PGNMetadataError] with the given metadata
41    ///
42    /// # Arguments
43    /// * `metadata` - The metadata that caused the error
44    ///
45    /// # Example
46    /// ```
47    /// # use chess_lab::errors::PGNMetadataError;
48    /// let error = PGNMetadataError::new("Invalid metadata".to_string());
49    /// ```
50    ///
51    pub fn new(metadata: String) -> Self {
52        PGNMetadataError { metadata }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_pgn_metadata_error_new() {
62        let metadata = "Invalid metadata".to_string();
63        let error = PGNMetadataError::new(metadata.clone());
64        assert_eq!(error.metadata, metadata);
65    }
66}