pkmn_savedata/
lib.rs

1#![doc = include_str!("../README.md")]
2//! # Examples
3//!
4//! ```ignore
5//! use pkmn_savedata::gba::{GbaSave, LanguageGBA};
6//! use std::fs::File;
7//!
8//! let file = File::open("Ruby.sav")?;
9//! let save = GbaSave::from_reader(file, LanguageGBA::English)?;
10//! let game_state = save.game_state;
11//! assert_eq!(game_state.trainer_id(), (12345, 22222));
12//! assert_eq!(game_state.pokedex_count(), 50);
13//!
14//! // Print the names of every Pokemon in Box 1.
15//! let box1 = save.boxes().box_ref(0)?;
16//! for pkm in box1.contents() {
17//!     if pkm.exists() {
18//!         let decoded = pkm.decode();
19//!         if let Some(nickname) = decoded.nickname() {
20//!             println!("{}", nickname.to_ref().decode());
21//!         }
22//!     }
23//! }
24//! ```
25
26#![cfg_attr(not(feature = "std"), no_std)]
27#![forbid(unsafe_code)]
28
29extern crate alloc;
30extern crate static_assertions as sa;
31
32pub mod gba;
33pub use pkmn_core_types as core_types;
34pub use pkmn_strings as strings;
35
36use core::{fmt, result};
37
38#[cfg(feature = "std")]
39use std::io;
40
41#[cfg(not(feature = "std"))]
42use no_std_io2::io;
43
44/// Error type used by this crate.
45#[derive(Debug)]
46pub enum SaveError {
47    IoError(io::Error),
48    Filesize,
49    CorruptSave,
50    NeverSaved,
51    OutOfBounds,
52    InvalidData,
53    NotFound,
54}
55
56impl fmt::Display for SaveError {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        match self {
59            SaveError::IoError(e) => e.fmt(f),
60            SaveError::Filesize => write!(f, "File size error."),
61            SaveError::CorruptSave => write!(f, "Save file is corrupt."),
62            SaveError::NeverSaved => write!(f, "No save data."),
63            SaveError::OutOfBounds => write!(f, "Out of bounds."),
64            SaveError::InvalidData => write!(f, "Invalid data."),
65            SaveError::NotFound => write!(f, "Not found."),
66        }
67    }
68}
69
70impl From<io::Error> for SaveError {
71    fn from(value: io::Error) -> Self {
72        SaveError::IoError(value)
73    }
74}
75
76/// Result type used by this crate.
77pub type Result<T> = result::Result<T, SaveError>;