Skip to content

Commit

Permalink
calling console::error/warn directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcz committed May 9, 2022
1 parent 0073908 commit 34c5e5c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 47 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion components/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ include = ["src/**/*"]
[dependencies]
serde = {version = "1.0", features = ["derive"] }

console = { path = "../console" }
errors = { path = "../errors" }
utils = { path = "../utils" }
libs = { path = "../libs" }
20 changes: 0 additions & 20 deletions components/config/src/config/link_checker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use console;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -15,25 +14,6 @@ impl Default for LinkCheckerLevel {
}
}

impl LinkCheckerLevel {
pub fn log(&self, msg: String) {
match self {
LinkCheckerLevel::Error => {
console::error(format!("{}{}", self.log_prefix(), msg).as_str())
}
LinkCheckerLevel::Warn => {
console::warn(format!("{}{}", self.log_prefix(), msg).as_str())
}
}
}
pub fn log_prefix(&self) -> &str {
match self {
LinkCheckerLevel::Error => "Error: ",
LinkCheckerLevel::Warn => "Warning: ",
}
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct LinkChecker {
Expand Down
4 changes: 2 additions & 2 deletions components/console/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn info(message: &str) {

pub fn warn(message: &str) {
colorize(
message,
&format!("{}{}", "Warning: ", message),
ColorSpec::new().set_bold(true).set_fg(Some(Color::Yellow)),
StandardStream::stdout(*COLOR_CHOICE),
);
Expand All @@ -33,7 +33,7 @@ pub fn success(message: &str) {

pub fn error(message: &str) {
colorize(
message,
&format!("{}{}", "Error: ", message),
ColorSpec::new().set_bold(true).set_fg(Some(Color::Red)),
StandardStream::stderr(*COLOR_CHOICE),
);
Expand Down
24 changes: 5 additions & 19 deletions components/markdown/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,16 @@ fn fix_link(
resolved.permalink
}
Err(_) => {
context.config.link_checker.internal_level.log(format!(
let msg = format!(
"Dead relative link `{}` in {}",
link,
context.current_page_path.unwrap_or("unknown"),
));
);
match context.config.link_checker.internal_level {
config::LinkCheckerLevel::Error => {
return Err(anyhow!(
"Dead relative link `{}` in {}",
link,
context.current_page_path.unwrap_or("unknown"),
))
}
config::LinkCheckerLevel::Error => return Err(anyhow!(msg)),
config::LinkCheckerLevel::Warn => {
console::warn(
format!(
"{}Dead relative link `{}` in {}",
config::LinkCheckerLevel::Warn.log_prefix(),
link,
context.current_page_path.unwrap_or("unknown"),
)
.as_str(),
);
link.to_string() // TODO rendering the broken internal relative link may not be optimal, what href should be rendered to HTML when relative links are invalid but we're in warn mode?
console::error(&msg);
link.to_string() // TODO document that broken internal relative links will show up in the HTML href as "@/path"
}
}
}
Expand Down
19 changes: 15 additions & 4 deletions components/site/src/link_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ pub fn check_internal_links_with_anchors(site: &Site) -> Result<()> {
);

for err in errors.into_iter() {
site.config.link_checker.internal_level.log(err);
match site.config.link_checker.internal_level {
LinkCheckerLevel::Error => console::error(&err),
LinkCheckerLevel::Warn => console::warn(&err),
}
}

match site.config.link_checker.internal_level {
Expand Down Expand Up @@ -183,7 +186,10 @@ pub fn check_external_links(site: &Site) -> Result<()> {
if !invalid_url_links.is_empty() {
for err in invalid_url_links.into_iter() {
let msg = err.domain.as_ref().unwrap_err();
site.config.link_checker.external_level.log(msg.to_string());
match site.config.link_checker.internal_level {
LinkCheckerLevel::Error => console::error(&msg.to_string()),
LinkCheckerLevel::Warn => console::warn(&msg.to_string()),
}
}
}

Expand Down Expand Up @@ -249,12 +255,17 @@ pub fn check_external_links(site: &Site) -> Result<()> {
}

for (page_path, link, check_res) in errors.iter() {
site.config.link_checker.external_level.log(format!(
let msg = format!(
"Dead link in {} to {}: {}",
page_path.to_string_lossy(),
link,
link_checker::message(check_res)
));
);

match site.config.link_checker.external_level {
LinkCheckerLevel::Error => todo!(),
LinkCheckerLevel::Warn => todo!(),
}
}

match site.config.link_checker.external_level {
Expand Down

0 comments on commit 34c5e5c

Please sign in to comment.