Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ pub struct CliArgs {
pub zoom_level: Option<f32>,
}

/// Reason to abort running Verso.
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum ArgError {
/// Failed to parse command line arguments
#[error("Failed to parse command line arguments: {0}\n try \"--help\"")]
ParseError(#[from] getopts::Fail),
/// `--help` was used. Print help and exit.
#[error("{0}")]
Help(String),
}

/// Configuration of Verso instance.
#[derive(Clone, Debug)]
pub struct Config {
Expand All @@ -64,7 +76,7 @@ pub struct Config {
pub resource_dir: PathBuf,
}

fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {
fn parse_cli_args() -> Result<CliArgs, ArgError> {
let args: Vec<String> = std::env::args().collect();

let mut opts = getopts::Options::new();
Expand Down Expand Up @@ -153,7 +165,12 @@ fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {

opts.optopt("", "zoom", "Initial window's zoom level", "1.5");

opts.optflag("", "help", "Display this help message");

let matches: getopts::Matches = opts.parse(&args[1..])?;
if matches.opt_present("help") {
return Err(ArgError::Help(opts.usage(&opts.short_usage(&args[0]))));
}
let url = matches
.opt_str("url")
.and_then(|url| match url::Url::parse(&url) {
Expand Down Expand Up @@ -273,9 +290,9 @@ fn parse_cli_args() -> Result<CliArgs, getopts::Fail> {

impl Config {
/// Create a new configuration for creating Verso instance.
pub fn new() -> Self {
pub fn new() -> Result<Self, ArgError> {
let args = parse_cli_args()?;
let mut opts = Opts::default();
let args = parse_cli_args().unwrap_or_default();

let (devtools_server_enabled, devtools_port) =
if let Some(devtools_port) = args.devtools_port {
Expand All @@ -301,11 +318,11 @@ impl Config {

let resource_dir = args.resource_dir.clone().unwrap_or(resources_dir_path());

Self {
Ok(Self {
opts,
args,
resource_dir,
}
})
}

/// Register URL scheme protocols
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ pub enum Error {
/// IPC errors.
#[error(transparent)]
IpcError(#[from] ipc_channel::ipc::IpcError),
/// Error parsing arguments (or `--help` was invoked).
#[error(transparent)]
ArgError(#[from] crate::config::ArgError),
}
37 changes: 33 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// Prevent console window from appearing on Windows
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::process::ExitCode;

use versoview::config::{ArgError, Config};
use versoview::verso::EventLoopProxyMessage;
use versoview::{Result, Verso};
use versoview::Verso;
use winit::application::ApplicationHandler;
use winit::event_loop::{self, DeviceEvents};
use winit::event_loop::{EventLoop, EventLoopProxy};

struct App {
/// Config parsed from the command line. `config` XOR `verso` must be `Some`.
config: Option<Config>,
/// The verso browser handle (possibly uninitialized).
verso: Option<Verso>,
proxy: EventLoopProxy<EventLoopProxyMessage>,
}

impl ApplicationHandler<EventLoopProxyMessage> for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
self.verso = Some(Verso::new(event_loop, self.proxy.clone()));
if let Some(config) = self.config.take() {
self.verso = Some(Verso::new(config, event_loop, self.proxy.clone()));
}
}

fn window_event(
Expand Down Expand Up @@ -46,18 +54,39 @@ impl ApplicationHandler<EventLoopProxyMessage> for App {
}
}

fn main() -> Result<()> {
fn run() -> versoview::errors::Result<()> {
let config = Some(versoview::config::Config::new()?);
init_crypto();

let event_loop = EventLoop::<EventLoopProxyMessage>::with_user_event().build()?;
event_loop.listen_device_events(DeviceEvents::Never);
let proxy = event_loop.create_proxy();
let mut app = App { verso: None, proxy };
let mut app = App {
config,
verso: None,
proxy,
};
event_loop.run_app(&mut app)?;

Ok(())
}

fn main() -> ExitCode {
run().map_or_else(
|e| match e {
versoview::Error::ArgError(ArgError::Help(message)) => {
println!("{message}");
ExitCode::SUCCESS
}
e => {
eprintln!("{e}");
ExitCode::FAILURE
}
},
|()| ExitCode::SUCCESS,
)
}

fn init_crypto() {
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
Expand Down
7 changes: 5 additions & 2 deletions src/verso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ impl Verso {
/// - Canvas: Enabled
/// - Constellation: Enabled
/// - Image Cache: Enabled
pub fn new(evl: &ActiveEventLoop, proxy: EventLoopProxy<EventLoopProxyMessage>) -> Self {
let config = Config::new();
pub fn new(
config: Config,
evl: &ActiveEventLoop,
proxy: EventLoopProxy<EventLoopProxyMessage>,
) -> Self {
let to_controller_sender = if let Some(ipc_channel) = &config.args.ipc_channel {
let sender =
IpcSender::<ToControllerMessage>::connect(ipc_channel.to_string()).unwrap();
Expand Down