ra2_types/
lib.rs

1#![deny(missing_debug_implementations, missing_copy_implementations)]
2#![warn(missing_docs, rustdoc::missing_crate_level_docs)]
3#![doc = include_str!("../readme.md")]
4#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/208321371")]
5#![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/208321371")]
6
7//! RA2 MIX file format library
8//!
9//! This library provides functionality for reading and writing Red Alert 2 MIX files.
10//! It supports both encrypted and unencrypted MIX files, and can extract files from MIX archives.
11
12mod errors;
13mod games;
14
15pub use crate::games::CncGame;
16#[cfg(feature = "image")]
17pub use image::{DynamicImage, Rgba, RgbaImage};
18#[cfg(feature = "apng")]
19pub use apng;
20use std::{
21    error::Error,
22    fmt::{Display, Formatter},
23};
24
25/// Result type for RA2 MIX file operations
26pub type Result<T> = std::result::Result<T, Ra2Error>;
27
28/// Error type for RA2 MIX file operations
29#[derive(Debug)]
30pub enum Ra2Error {
31    /// IO error
32    IoError(std::io::Error),
33
34    /// Crypto error
35    CryptoError {
36        /// The error message
37        message: String,
38    },
39    /// Invalid file format
40    InvalidFormat {
41        /// The error message
42        message: String,
43    },
44    /// Encode error
45    EncodeError {
46        /// The error format
47        format: String,
48        /// The error message
49        message: String,
50    },
51    /// Decode error
52    DecodeError {
53        /// The error format
54        format: String,
55        /// The error message
56        message: String,
57    },
58    /// Missing file
59    FileNotFound(String),
60    /// Out of boundary
61    OutOfBoundary {
62        /// Max limit
63        limit: usize,
64        /// The error message
65        message: String,
66    },
67}
68
69impl Error for Ra2Error {}
70
71impl Display for Ra2Error {
72    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Ra2Error::IoError(e) => {
75                write!(f, "IO error: {}", e)
76            }
77            Ra2Error::CryptoError { message: e } => {
78                write!(f, "Crypto error:: {}", e)
79            }
80            Ra2Error::InvalidFormat { message: e } => {
81                write!(f, "Invalid file format: {}", e)
82            }
83            Ra2Error::FileNotFound(e) => {
84                write!(f, "File not found: {}", e)
85            }
86            Ra2Error::DecodeError { format, message } => {
87                write!(f, "Decode error: {}: {}", format, message)
88            }
89            Ra2Error::EncodeError { format, message } => {
90                write!(f, "Encode error: {}: {}", format, message)
91            }
92            Ra2Error::OutOfBoundary { limit, message } => {
93                write!(f, "Out of boundary {}: {}", limit, message)
94            }
95        }
96    }
97}