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

View file

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

View file

@ -3,6 +3,8 @@ use std::{
rc::Rc,
};
use anyhow::Result;
use retro_frontend::{
core::Core,
frontend,
@ -22,25 +24,25 @@ struct App {
}
impl App {
fn new() -> Self {
Self {
fn new() -> Result<Self> {
Ok(Self {
rfb_server: RfbServer::new(RfbServerConfig {
width: 640,
height: 480,
}),
})?,
// nasty, but idk a better way
pad: Rc::new(RefCell::new(RetroPad::new())),
}
})
}
fn new_and_init() -> Rc<RefCell<App>> {
let app = App::new();
fn new_and_init() -> Result<Rc<RefCell<App>>> {
let app = App::new()?;
let rc = Rc::new(RefCell::new(app));
// Initalize all the frontend callbacks and stuff.
App::init(&rc);
rc
Ok(rc)
}
/// Initalizes the frontend library with callbacks back to us,
@ -114,7 +116,7 @@ impl App {
}
fn main() {
fn main() -> Result<()> {
// Setup a tracing subscriber
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
@ -132,17 +134,16 @@ fn main() {
let core_path: &String = matches.get_one("core").unwrap();
// 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
let _app = App::new_and_init();
if let Some(rom_path) = matches.get_one::<String>("rom") {
core.load_game(rom_path)
.expect("Provided ROM failed to load");
core.load_game(rom_path)?
}
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;
// Do the main loop

View file

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