From e70a19ec0ed8f5eef6f208dda7c7dbe5e28e6a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 24 Nov 2021 15:33:02 +0100 Subject: [PATCH] rom: build.rs: error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- rom/build.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/rom/build.rs b/rom/build.rs index cf5456c..807ecf5 100644 --- a/rom/build.rs +++ b/rom/build.rs @@ -13,15 +13,58 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -use std::fs::File; +use std::env; +use std::fmt; +use std::fs::{self, File}; use std::io::{BufWriter, Write}; +use std::error::Error as StdError; use std::path::Path; use quote::{format_ident, quote}; -fn main() { +#[derive(Debug, Eq, PartialEq)] +pub enum Error { + EnvVariableError(env::VarError), + ROMDoesntExist, + UnableToOpenROM, +} + +impl StdError for Error {} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + Error::EnvVariableError(e) => write!(f, "POKEMON_ROM env var error: {}", e), + Error::ROMDoesntExist => write!(f, "ROM doesn't exist"), + Error::UnableToOpenROM => write!(f, "Unable to open ROM"), + } + } +} + +impl From for Error { + fn from(err: env::VarError) -> Error { + Error::EnvVariableError(err) + } +} + +fn get_rom() -> Result, Error> { + let env_romp = env::var("POKEMON_ROM")?; + let romp = Path::new(&env_romp); + + if ! romp.exists() { + return Err(Error::ROMDoesntExist); + } + + Ok(match fs::read(romp) { + Ok(v) => v, + Err(_) => return Err(Error::UnableToOpenROM), + }) +} + +fn main() -> Result<(), Error> { let output = Path::new(&env::var("OUT_DIR").unwrap()).join("output.rs"); let mut output = BufWriter::new(File::create(&output).unwrap()); + let _rom: Vec = get_rom()?; let pkmn_names: Vec<_> = [ "Rhydon", @@ -40,4 +83,5 @@ fn main() { }; write!(output, "{}", tokens).unwrap(); + Ok(()) }