mirror of
https://github.com/eiskasten/wc-beacon.git
synced 2026-09-09 04:45:09 -05:00
Wrap set command errors
This commit is contained in:
27
src/error.rs
Normal file
27
src/error.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use std::error::Error;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ReasonError<'a, E: Error> {
|
||||
msg: &'a str,
|
||||
source: E,
|
||||
}
|
||||
|
||||
impl<E: Error> Display for ReasonError<'_, E> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(self.msg)?;
|
||||
f.write_str(": ")?;
|
||||
Display::fmt(&self.source, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Error> Error for ReasonError<'_, E> {}
|
||||
|
||||
pub fn err_reason<E: Error>(msg: &str, source: E) -> ReasonError<E> {
|
||||
ReasonError {
|
||||
msg,
|
||||
source,
|
||||
}
|
||||
}
|
||||
13
src/info.rs
13
src/info.rs
@@ -5,8 +5,10 @@ use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use crate::error::err_reason;
|
||||
use crate::pcd::{CardType, Deserialized, Game, Partitioned, PCD, PCD_LENGTH, Raw};
|
||||
|
||||
|
||||
pub fn info(pcd: PathBuf) -> Result<(), Box<dyn Error>> {
|
||||
let pcd: PCD<Raw> = PCD::try_from(fs::read(pcd)?.as_slice())?;
|
||||
let partitioned: PCD<Partitioned> = pcd.into();
|
||||
@@ -17,8 +19,9 @@ pub fn info(pcd: PathBuf) -> Result<(), Box<dyn Error>> {
|
||||
|
||||
pub fn set(title: Option<String>, card_type: Option<CardType>, card_id: Option<u16>, games: Option<Vec<Game>>, comment: Option<String>, redistribution: Option<u8>, icons: Option<Vec<u16>>, pgt: Option<PathBuf>, received: Option<u16>, pcd: Option<PathBuf>, output: PathBuf) -> Result<(), Box<dyn Error>> {
|
||||
let mut pcd = if let Some(f) = pcd {
|
||||
let raw: PCD<Raw> = PCD::try_from(fs::read(f)?.as_slice())?;
|
||||
let parts: PCD<Partitioned> = raw.try_into().unwrap();
|
||||
let data = fs::read(f).map_err(|e| err_reason("Unable to read pcd file", e))?;
|
||||
let raw: PCD<Raw> = PCD::try_from(data.as_slice())?;
|
||||
let parts: PCD<Partitioned> = PCD::from(raw);
|
||||
parts.deserialize()
|
||||
} else {
|
||||
PCD::<Deserialized>::new()
|
||||
@@ -57,15 +60,15 @@ pub fn set(title: Option<String>, card_type: Option<CardType>, card_id: Option<u
|
||||
}
|
||||
|
||||
if let Some(p) = pgt {
|
||||
let mut f = File::open(p).expect("Unable to open pgt file");
|
||||
f.read_exact(&mut pcd.state.pgt).expect("Unable to read pgt");
|
||||
let mut f = File::open(p).map_err(|e| err_reason("Unable to read pgt", e))?;
|
||||
f.read_exact(&mut pcd.state.pgt).map_err(|e| err_reason("Unable to read pgt", e))?;
|
||||
}
|
||||
|
||||
let pcd: PCD<Raw> = (&pcd.serialize()).try_into()?;
|
||||
let pcd_data: [u8; PCD_LENGTH] = pcd.into();
|
||||
|
||||
let mut f = File::create(output)?;
|
||||
f.write(&pcd_data).expect("Unable to write pcd file");
|
||||
f.write(&pcd_data).map_err(|e| err_reason("Unable to write pcd file", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -19,6 +19,7 @@ mod beacon;
|
||||
mod decrypt;
|
||||
mod pokestr;
|
||||
mod info;
|
||||
mod error;
|
||||
|
||||
pub mod pokestrmap {
|
||||
include!(concat!(env!("OUT_DIR"), "/pokestrmap.rs"));
|
||||
|
||||
Reference in New Issue
Block a user