Skip to content

Commit

Permalink
style: fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Becher authored and Armin Becher committed Feb 4, 2025
1 parent 300f2f3 commit e57b6f0
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 42 deletions.
4 changes: 2 additions & 2 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use mlc::markup::MarkupType;
use mlc::{Config, OptionalConfig};
use std::fs;

fn end_to_end_benchmark() {
async fn end_to_end_benchmark() {
let config = Config {
directory: fs::canonicalize("./benches/benchmark/markdown/ignore_me_dir").unwrap(),
optional: OptionalConfig {
markup_types: Some(vec![MarkupType::Markdown]),
..Default::default()
},
};
let _ = mlc::run(&config);
mlc::run(&config).await.unwrap();
}

fn criterion_benchmark(c: &mut Criterion) {
Expand Down
6 changes: 2 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ pub fn parse_args() -> Config {
.get_one::<String>("directory")
.unwrap_or(&default_dir);
let directory = dir_string
.replace('/', &MAIN_SEPARATOR.to_string())
.replace('\\', &MAIN_SEPARATOR.to_string())
.replace(['/', '\\'], std::path::MAIN_SEPARATOR_STR)
.parse()
.expect("failed to parse path");

Expand Down Expand Up @@ -191,8 +190,7 @@ pub fn parse_args() -> Config {
if let Some(root_dir) = matches.get_one::<String>("root-dir") {
let root_path = Path::new(
&root_dir
.replace('/', &MAIN_SEPARATOR.to_string())
.replace('\\', &MAIN_SEPARATOR.to_string()),
.replace(['/', '\\'], std::path::MAIN_SEPARATOR_STR),
)
.to_path_buf();
if !root_path.is_dir() {
Expand Down
2 changes: 1 addition & 1 deletion src/file_traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn find(config: &Config, result: &mut Vec<MarkupFile>) {
let f_name = entry.file_name().to_string_lossy();
debug!("Check file: '{f_name}'");

if let Some(markup_type) = markup_type(&f_name, &markup_types) {
if let Some(markup_type) = markup_type(&f_name, markup_types) {
let path = entry.path();

let abs_path = match fs::canonicalize(path) {
Expand Down
21 changes: 8 additions & 13 deletions src/link_extractors/markdown_link_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,14 @@ impl LinkExtractor for MarkdownLinkExtractor {
let mut result: Vec<MarkupLink> = Vec::new();
for (evt, range) in parser.into_offset_iter() {
match evt {
Event::Start(tag) => {
match tag {
Tag::Link { dest_url, .. } | Tag::Image { dest_url, .. } => {
let line_col = line_column_from_idx(range.start);
result.push(MarkupLink {
line: line_col.0,
column: line_col.1,
source: String::new(),
target: dest_url.to_string(),
});
}
_ => (),
};
Event::Start(Tag::Link { dest_url, .. } | Tag::Image { dest_url, .. }) => {
let line_col = line_column_from_idx(range.start);
result.push(MarkupLink {
line: line_col.0,
column: line_col.1,
source: String::new(),
target: dest_url.to_string(),
});
}
Event::Html(html) | Event::InlineHtml(html) => {
let line_col = line_column_from_idx(range.start);
Expand Down
7 changes: 3 additions & 4 deletions src/link_validator/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ pub async fn check_filesystem(target: &str, config: &Config) -> LinkCheckResult

pub async fn resolve_target_link(source: &str, target: &str, config: &Config) -> String {
let mut normalized_link = target
.replace('/', &MAIN_SEPARATOR.to_string())
.replace('\\', &MAIN_SEPARATOR.to_string());
.replace(['/', '\\'], std::path::MAIN_SEPARATOR_STR);
if let Some(idx) = normalized_link.find('#') {
warn!(
"Strip everything after #. The chapter part '{}' is not checked.",
Expand All @@ -88,10 +87,10 @@ pub async fn resolve_target_link(source: &str, target: &str, config: &Config) ->
.expect("Could not resolve target path")
.to_string();
// Remove verbatim path identifier which causes trouble on windows when using ../../ in paths
return abs_path
abs_path
.strip_prefix("\\\\?\\")
.unwrap_or(&abs_path)
.to_string();
.to_string()
}

async fn absolute_target_path(source: &str, target: &PathBuf) -> PathBuf {
Expand Down
24 changes: 12 additions & 12 deletions src/link_validator/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use wildmatch::WildMatch;

pub async fn check_http(
target: &str,
do_not_warn_for_redirect_to: &Vec<WildMatch>,
do_not_warn_for_redirect_to: &[WildMatch],
) -> LinkCheckResult {
debug!("Check http link target {:?}", target);
let url = reqwest::Url::parse(target).expect("URL of unknown type");
Expand All @@ -31,7 +31,7 @@ fn new_request(method: Method, url: &reqwest::Url) -> Request {

async fn http_request(
url: &reqwest::Url,
do_not_warn_for_redirect_to: &Vec<WildMatch>,
do_not_warn_for_redirect_to: &[WildMatch],
) -> reqwest::Result<LinkCheckResult> {
lazy_static! {
static ref CLIENT: Client = reqwest::Client::builder()
Expand Down Expand Up @@ -88,13 +88,13 @@ mod test {

#[tokio::test]
async fn check_http_is_available() {
let result = check_http("https://gitlab.com/becheran/mlc", &vec![]).await;
let result = check_http("https://gitlab.com/becheran/mlc", &[]).await;
assert_eq!(result, LinkCheckResult::Ok);
}

#[tokio::test]
async fn check_http_is_redirection() {
let result = check_http("http://gitlab.com/becheran/mlc", &vec![]).await;
let result = check_http("http://gitlab.com/becheran/mlc", &[]).await;
assert_eq!(
result,
LinkCheckResult::Warning(
Expand All @@ -108,23 +108,23 @@ mod test {
// we ignore redirections to the 'https'-version
let result = check_http(
"http://gitlab.com/becheran/mlc",
&vec![WildMatch::new("https://gitlab.com/becheran/mlc")],
&[WildMatch::new("https://gitlab.com/becheran/mlc")],
)
.await;
assert_eq!(result, LinkCheckResult::Ok);
}

#[tokio::test]
async fn check_http_redirection_do_not_warn_if_ignored_star_pattern() {
let result = check_http("http://gitlab.com/becheran/mlc", &vec![WildMatch::new("*")]).await;
let result = check_http("http://gitlab.com/becheran/mlc", &[WildMatch::new("*")]).await;
assert_eq!(result, LinkCheckResult::Ok);
}

#[tokio::test]
async fn check_http_redirection_do_warn_if_ignored_mismatch() {
let result = check_http(
"http://gitlab.com/becheran/mlc",
&vec![WildMatch::new("http://www.google.com")],
&[WildMatch::new("http://www.google.com")],
)
.await;
assert_eq!(
Expand All @@ -137,7 +137,7 @@ mod test {

#[tokio::test]
async fn check_http_is_redirection_failure() {
let result = check_http("http://gitlab.com/fake-page/does/not/exist/ever", &vec![]).await;
let result = check_http("http://gitlab.com/fake-page/does/not/exist/ever", &[]).await;
assert_eq!(
result,
LinkCheckResult::Failed("403 - Forbidden".to_string())
Expand All @@ -146,19 +146,19 @@ mod test {

#[tokio::test]
async fn check_https_crates_io_available() {
let result = check_http("https://crates.io", &vec![]).await;
let result = check_http("https://crates.io", &[]).await;
assert_eq!(result, LinkCheckResult::Ok);
}

#[tokio::test]
async fn check_http_request_with_hash() {
let result = check_http("https://gitlab.com/becheran/mlc#bla", &vec![]).await;
let result = check_http("https://gitlab.com/becheran/mlc#bla", &[]).await;
assert_eq!(result, LinkCheckResult::Ok);
}

#[tokio::test]
async fn check_http_request_redirection_with_hash() {
let result = check_http("http://gitlab.com/becheran/mlc#bla", &vec![]).await;
let result = check_http("http://gitlab.com/becheran/mlc#bla", &[]).await;
assert_eq!(
result,
LinkCheckResult::Warning(
Expand All @@ -169,7 +169,7 @@ mod test {

#[tokio::test]
async fn check_wrong_http_request() {
let result = check_http("https://doesNotExist.me/even/less/likelly", &vec![]).await;
let result = check_http("https://doesNotExist.me/even/less/likelly", &[]).await;
assert!(result != LinkCheckResult::Ok);
}
}
2 changes: 1 addition & 1 deletion src/link_validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn check(
link_target: &str,
link_type: &LinkType,
config: &Config,
do_not_warn_for_redirect_to: &Vec<WildMatch>,
do_not_warn_for_redirect_to: &[WildMatch],
) -> LinkCheckResult {
info!("Check link {}.", &link_target);
match link_type {
Expand Down
7 changes: 2 additions & 5 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ use serde::Deserialize;
use simplelog::*;

#[derive(Debug, Clone, Copy, Deserialize)]
#[derive(Default)]
pub enum LogLevel {
Info,
#[default]
Warn,

Check warning on line 11 in src/logger.rs

View workflow job for this annotation

GitHub Actions / formatting

Diff in /home/runner/work/mlc/mlc/src/logger.rs
Debug,
}

impl Default for LogLevel {
fn default() -> Self {
LogLevel::Warn
}
}

pub fn init(log_level: &LogLevel) {
let level_filter = match log_level {
Expand Down

0 comments on commit e57b6f0

Please sign in to comment.