Skip to content

Commit

Permalink
feat(issues): issue counter
Browse files Browse the repository at this point in the history
This allows initial compat to flaw ids.
  • Loading branch information
fiji-flo committed Oct 28, 2024
1 parent d98e0a8 commit bf9984e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
38 changes: 27 additions & 11 deletions crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use url::Url;

use crate::error::DocError;
use crate::helpers::l10n::l10n_json_data;
use crate::issues::get_issue_couter;
use crate::pages::page::{Page, PageLike};
use crate::pages::types::curriculum::CurriculumPage;
use crate::redirects::resolve_redirect;
Expand Down Expand Up @@ -102,24 +103,33 @@ pub fn post_process_html<T: PageLike>(
.map(|height| format!("{:.0}", height)),
),
Err(e) => {
let ic = get_issue_couter();
warn!(
source = "image-check",
ic = ic,
"Error parsing {}: {e}",
file.display()
);
el.set_attribute("data-flaw", &ic.to_string())?;
(None, None)
}
}
} else if let Ok(dim) = imagesize::size(&file).inspect_err(|e| {
warn!(
source = "image-check",
"Error opening {}: {e}",
file.display()
)
}) {
(Some(dim.width.to_string()), Some(dim.height.to_string()))
} else {
(None, None)
match imagesize::size(&file) {
Ok(dim) => (Some(dim.width.to_string()), Some(dim.height.to_string())),
Err(e) => {
let ic = get_issue_couter();
warn!(
source = "image-check",
ic = ic,
"Error opening {}: {e}",
file.display()
);
el.set_attribute("data-flaw", &ic.to_string())?;

(None, None)
}
}
};
if let Some(width) = width {
el.set_attribute("width", &width)?;
Expand Down Expand Up @@ -185,21 +195,27 @@ pub fn post_process_html<T: PageLike>(
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(line));
let line = line.as_ref();
let ic = get_issue_couter();
tracing::warn!(
source = "redirected-link",
ic = ic,
line = line,
col = col,
url = original_href,
redirect = resolved_href.as_ref()
)
);
el.set_attribute("data-flaw", &ic.to_string())?;
}
}
} else {
let ic = get_issue_couter();
tracing::warn!(
source = "redirected-link",
ic = ic,
url = original_href,
redirect = resolved_href.as_ref()
)
);
el.set_attribute("data-flaw", &ic.to_string())?;
}
}
el.set_attribute(
Expand Down
30 changes: 27 additions & 3 deletions crates/rari-doc/src/issues.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::{BTreeMap, HashMap};
use std::fmt;
use std::sync::atomic::AtomicU64;
use std::sync::{Arc, Mutex};

use serde::Serialize;
Expand All @@ -9,16 +10,24 @@ use tracing::{Event, Subscriber};
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::Layer;

static ISSUE_COUNTER: AtomicU64 = AtomicU64::new(1);

pub(crate) fn get_issue_couter() -> u64 {
ISSUE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}

#[derive(Debug, Default, Clone)]
pub struct Issue {
pub req: u64,
pub ic: u64,
pub fields: Vec<(&'static str, String)>,
pub spans: Vec<(&'static str, String)>,
}

#[derive(Debug, Default)]
pub struct IssueEntries {
req: u64,
ic: u64,
entries: Vec<(&'static str, String)>,
}

Expand All @@ -32,6 +41,7 @@ pub struct Issues<'a> {
#[derive(Clone, Debug, Serialize)]
pub struct TemplIssue<'a> {
pub req: u64,
pub ic: u64,
pub source: &'a str,
pub file: &'a str,
pub slug: &'a str,
Expand All @@ -44,6 +54,7 @@ pub struct TemplIssue<'a> {
static UNKNOWN: &str = "unknown";
static DEFAULT_TEMPL_ISSUE: TemplIssue<'static> = TemplIssue {
req: 0,
ic: 0,
source: UNKNOWN,
file: UNKNOWN,
slug: UNKNOWN,
Expand Down Expand Up @@ -126,6 +137,8 @@ impl Visit for IssueEntries {
fn record_u64(&mut self, field: &Field, value: u64) {
if field.name() == "req" {
self.req = value;
} else if field.name() == "ic" {
self.ic = value;
}
}
}
Expand All @@ -139,6 +152,8 @@ impl Visit for Issue {
fn record_u64(&mut self, field: &Field, value: u64) {
if field.name() == "req" {
self.req = value;
} else if field.name() == "ic" {
self.ic = value;
}
}
}
Expand All @@ -164,6 +179,7 @@ where
fn on_event(&self, event: &Event, ctx: tracing_subscriber::layer::Context<S>) {
let mut issue = Issue {
req: 0,
ic: 0,
fields: vec![],
spans: vec![],
};
Expand All @@ -173,7 +189,12 @@ where
let ext = span.extensions();
if let Some(entries) = ext.get::<IssueEntries>() {
if entries.req != 0 {
issue.req = entries.req
issue.req = entries.req;
}
if entries.ic != 0 {
issue.ic = entries.ic;
} else {
issue.ic = get_issue_couter();
}
issue.spans.extend(entries.entries.iter().rev().cloned());
}
Expand All @@ -197,7 +218,7 @@ pub enum Additional {

#[derive(Serialize, Debug, Default, Clone)]
pub struct DisplayIssue {
pub id: usize,
pub id: u64,
pub explanation: Option<String>,
pub suggestion: Option<String>,
pub fixable: Option<bool>,
Expand All @@ -213,7 +234,10 @@ pub type DisplayIssues = BTreeMap<&'static str, Vec<DisplayIssue>>;

impl From<Issue> for DisplayIssue {
fn from(value: Issue) -> Self {
let mut di = DisplayIssue::default();
let mut di = DisplayIssue {
id: value.ic,
..Default::default()
};
let mut additional = HashMap::new();
for (key, value) in value.spans.into_iter().chain(value.fields.into_iter()) {
match key {
Expand Down

0 comments on commit bf9984e

Please sign in to comment.