Skip to content

Commit

Permalink
settings: Allow specifying a proxy
Browse files Browse the repository at this point in the history
This will in the future be used by the http library to proxy
requests and allow inspecting them.
  • Loading branch information
simonwuelker committed Aug 12, 2024
1 parent 8f3420e commit dbd7c05
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
29 changes: 19 additions & 10 deletions crates/settings/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
use clap::Parser;
use std::net;
use url::URL;

use crate::Settings;

#[derive(Parser, Debug)]
#[command(name = "Stormlicht")]
#[command(version, about, long_about = None)]
#[derive(clap::Parser, Debug)]
#[command(name = "Stormlicht", version, about="A modern browser engine", long_about = None)]
pub struct Arguments {
/// Disable javascript execution
#[arg(long)]
disable_javascript: Option<bool>,
#[clap(
long,
action = clap::ArgAction::SetTrue,
)]
disable_javascript: bool,

/// URL to load initially
#[arg(value_parser = parse_url)]
#[arg(value_parser = parse_url, value_hint = clap::ValueHint::Url)]
url: Option<URL>,

/// Proxy for http requests
#[arg(long, value_parser = parse_socketaddr)]
proxy: Option<net::SocketAddr>,
}

impl Arguments {
pub(crate) fn update_settings(self, settings: &mut Settings) {
if let Some(disable_javascript) = self.disable_javascript {
settings.disable_javascript = disable_javascript
}
settings.disable_javascript = settings.disable_javascript;

if let Some(url) = self.url {
settings.url = url;
Expand All @@ -31,3 +35,8 @@ impl Arguments {
fn parse_url(s: &str) -> Result<URL, String> {
s.parse().map_err(|e: url::Error| format!("{e:?}"))
}

fn parse_socketaddr(s: &str) -> Result<net::SocketAddr, String> {
s.parse()
.map_err(|e: <net::SocketAddr as std::str::FromStr>::Err| format!("{e}"))
}
7 changes: 6 additions & 1 deletion crates/settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod cli;

use std::sync::LazyLock;
use std::{net, sync::LazyLock};

use clap::Parser;
use url::URL;
Expand All @@ -23,6 +23,9 @@ pub struct Settings {

/// URL to load initially
pub url: URL,

/// Proxy for networking
pub proxy: Option<net::SocketAddr>,
}

impl Settings {
Expand All @@ -31,6 +34,7 @@ impl Settings {
let mut settings = Self::default();

let args = cli::Arguments::parse();

args.update_settings(&mut settings);

settings
Expand All @@ -42,6 +46,7 @@ impl Default for Settings {
Self {
disable_javascript: false,
url: WELCOME_PAGE.parse().expect("welcome page is a valid url"),
proxy: None,
}
}
}

0 comments on commit dbd7c05

Please sign in to comment.