use results for slightly less boneheaded error handling

This commit is contained in:
Lily Tsuru 2024-08-02 03:41:27 -04:00
parent 6f829c5252
commit 312c94af1a
4 changed files with 27 additions and 16 deletions

7
Cargo.lock generated
View file

@ -60,6 +60,12 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "anyhow"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]] [[package]]
name = "bindgen" name = "bindgen"
version = "0.69.4" version = "0.69.4"
@ -401,6 +407,7 @@ dependencies = [
name = "retrovnc" name = "retrovnc"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"clap", "clap",
"libvnc-sys", "libvnc-sys",
"retro_frontend", "retro_frontend",

View file

@ -5,6 +5,7 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.6", features = ["cargo"] } clap = { version = "4.5.6", features = ["cargo"] }
libvnc-sys = "0.1.4" libvnc-sys = "0.1.4"
retro_frontend = { path = "../retro_frontend" } retro_frontend = { path = "../retro_frontend" }

View file

@ -3,6 +3,8 @@ use std::{
rc::Rc, rc::Rc,
}; };
use anyhow::Result;
use retro_frontend::{ use retro_frontend::{
core::Core, core::Core,
frontend, frontend,
@ -22,25 +24,25 @@ struct App {
} }
impl App { impl App {
fn new() -> Self { fn new() -> Result<Self> {
Self { Ok(Self {
rfb_server: RfbServer::new(RfbServerConfig { rfb_server: RfbServer::new(RfbServerConfig {
width: 640, width: 640,
height: 480, height: 480,
}), })?,
// nasty, but idk a better way // nasty, but idk a better way
pad: Rc::new(RefCell::new(RetroPad::new())), pad: Rc::new(RefCell::new(RetroPad::new())),
} })
} }
fn new_and_init() -> Rc<RefCell<App>> { fn new_and_init() -> Result<Rc<RefCell<App>>> {
let app = App::new(); let app = App::new()?;
let rc = Rc::new(RefCell::new(app)); let rc = Rc::new(RefCell::new(app));
// Initalize all the frontend callbacks and stuff. // Initalize all the frontend callbacks and stuff.
App::init(&rc); App::init(&rc);
rc Ok(rc)
} }
/// Initalizes the frontend library with callbacks back to us, /// Initalizes the frontend library with callbacks back to us,
@ -114,7 +116,7 @@ impl App {
} }
fn main() { fn main() -> Result<()> {
// Setup a tracing subscriber // Setup a tracing subscriber
let subscriber = FmtSubscriber::builder() let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO) .with_max_level(Level::INFO)
@ -132,17 +134,16 @@ fn main() {
let core_path: &String = matches.get_one("core").unwrap(); let core_path: &String = matches.get_one("core").unwrap();
// Load the user's provided core // Load the user's provided core
let mut core = Core::load(core_path).expect("Provided core failed to load"); let mut core = Core::load(core_path)?;
// Initalize the app // Initalize the app
let _app = App::new_and_init(); let _app = App::new_and_init();
if let Some(rom_path) = matches.get_one::<String>("rom") { if let Some(rom_path) = matches.get_one::<String>("rom") {
core.load_game(rom_path) core.load_game(rom_path)?
.expect("Provided ROM failed to load");
} }
let av_info = frontend::get_av_info().expect("Should have AV info by this point."); let av_info = frontend::get_av_info()?;
let step_ms = ((1.0 / av_info.timing.fps) * 1000.) as u64; let step_ms = ((1.0 / av_info.timing.fps) * 1000.) as u64;
// Do the main loop // Do the main loop

View file

@ -3,6 +3,8 @@ use libvnc_sys::rfb::{self, bindings::_rfbScreenInfo};
use retro_frontend::libretro_sys_new; use retro_frontend::libretro_sys_new;
use std::ptr::{addr_of, NonNull}; use std::ptr::{addr_of, NonNull};
use anyhow::{anyhow, Result};
pub struct RfbServerConfig { pub struct RfbServerConfig {
pub width: u16, pub width: u16,
pub height: u16, pub height: u16,
@ -18,8 +20,9 @@ pub struct RfbServer {
buttons: [bool; 32], buttons: [bool; 32],
} }
impl RfbServer { impl RfbServer {
pub fn new(config: RfbServerConfig) -> Box<Self> { pub fn new(config: RfbServerConfig) -> Result<Box<Self>> {
unsafe { unsafe {
// Feed a fake argv in (TODO: Make this better.) // Feed a fake argv in (TODO: Make this better.)
let argc = 3; let argc = 3;
@ -41,7 +44,7 @@ impl RfbServer {
// result // result
if screen.is_null() { if screen.is_null() {
panic!("rfbGetScreen() failed"); return Err(anyhow!("rfbGetScreen() failed"));
} }
let mut ret = Box::new(Self { let mut ret = Box::new(Self {
@ -66,7 +69,7 @@ impl RfbServer {
ret.resize(config.width, config.height); ret.resize(config.width, config.height);
ret Ok(ret)
} }
} }
@ -120,7 +123,6 @@ impl RfbServer {
} }
} }
pub fn get_buttons(&self) -> [bool; 32] { pub fn get_buttons(&self) -> [bool; 32] {
self.buttons self.buttons
} }