Skip to content

Commit

Permalink
Add more selections for config
Browse files Browse the repository at this point in the history
  • Loading branch information
jo1gi committed Apr 3, 2023
1 parent f75de90 commit 8d32f80
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dirs = { version = "4" }
base64 = "0.21"
thiserror = "1.0"
displaydoc = "0.2"
regex = "1"

[profile.release]
lto = true
Expand Down
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::path::Path;

#[derive(Deserialize)]
pub struct Config {
Expand All @@ -8,6 +8,7 @@ pub struct Config {

#[derive(Default, Deserialize)]
pub struct Rule {
pub url: Option<String>,
pub scheme: Option<String>,
pub host: Option<String>,
pub path: Option<String>,
Expand Down
11 changes: 9 additions & 2 deletions src/find_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ use crate::config::{Config, Rule};
fn url_matches_param(url_data: &Option<&str>, param: &Option<String>) -> bool {
match (param, url_data) {
(None, _) => true,
(Some(param), Some(url_data)) if param == url_data => true,
(Some(param), Some(url_data)) => {
if let Some(re) = regex::Regex::new(&param).ok() {
re.is_match(&url_data)
} else { false }
},
_ => false
}
}

/// Returns true if `rule` matches `url`
fn url_match_rule(url: &Url, rule: &Rule) -> bool {
[
(Some(url.as_str()), &rule.url),
(Some(url.scheme()), &rule.scheme),
(url.host_str(), &rule.host),
(Some(url.path()), &rule.path),
(url.query(), &rule.query),
(url.fragment(), &rule.fragment)
].iter().all(|(url_data, param)| url_matches_param(url_data, param))
}

Expand Down Expand Up @@ -54,7 +61,7 @@ mod test {
let url = url::Url::parse("https://example.com").unwrap();
let config = super::Config{rules: vec![
super::Rule {
scheme: Some("https".to_string()),
scheme: Some("https?".to_string()),
..Default::default()
}
]};
Expand Down
2 changes: 2 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ fn get_variable<'a>(url: &'a Url, key: &str) -> Option<&'a str> {
"scheme" => Some(url.scheme()),
"host" => url.host_str(),
"path" => Some(url.path()),
"query" => url.query(),
"fragment" => url.fragment(),
_ => None,
}
}
Expand Down

0 comments on commit 8d32f80

Please sign in to comment.