From bf9984e374b9c34183e009b15e119c0418f9badb Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Mon, 28 Oct 2024 12:47:14 +0100 Subject: [PATCH] feat(issues): issue counter This allows initial compat to flaw ids. --- crates/rari-doc/src/html/rewriter.rs | 38 ++++++++++++++++++++-------- crates/rari-doc/src/issues.rs | 30 +++++++++++++++++++--- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/crates/rari-doc/src/html/rewriter.rs b/crates/rari-doc/src/html/rewriter.rs index 6c56d133..fd41d0fb 100644 --- a/crates/rari-doc/src/html/rewriter.rs +++ b/crates/rari-doc/src/html/rewriter.rs @@ -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; @@ -102,24 +103,33 @@ pub fn post_process_html( .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)?; @@ -185,21 +195,27 @@ pub fn post_process_html( .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( diff --git a/crates/rari-doc/src/issues.rs b/crates/rari-doc/src/issues.rs index 185a625f..ed5e45dc 100644 --- a/crates/rari-doc/src/issues.rs +++ b/crates/rari-doc/src/issues.rs @@ -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; @@ -9,9 +10,16 @@ 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)>, } @@ -19,6 +27,7 @@ pub struct Issue { #[derive(Debug, Default)] pub struct IssueEntries { req: u64, + ic: u64, entries: Vec<(&'static str, String)>, } @@ -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, @@ -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, @@ -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; } } } @@ -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; } } } @@ -164,6 +179,7 @@ where fn on_event(&self, event: &Event, ctx: tracing_subscriber::layer::Context) { let mut issue = Issue { req: 0, + ic: 0, fields: vec![], spans: vec![], }; @@ -173,7 +189,12 @@ where let ext = span.extensions(); if let Some(entries) = ext.get::() { 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()); } @@ -197,7 +218,7 @@ pub enum Additional { #[derive(Serialize, Debug, Default, Clone)] pub struct DisplayIssue { - pub id: usize, + pub id: u64, pub explanation: Option, pub suggestion: Option, pub fixable: Option, @@ -213,7 +234,10 @@ pub type DisplayIssues = BTreeMap<&'static str, Vec>; impl From 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 {