Skip to content

Commit

Permalink
feat(issues): add initial support for macro issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Nov 13, 2024
1 parent 803c787 commit 5e23b0f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 41 deletions.
13 changes: 6 additions & 7 deletions crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,12 @@ pub fn post_process_html<T: PageLike>(
if let Some(pos) = el.get_attribute("data-sourcepos") {
if let Some((start, _)) = pos.split_once('-') {
if let Some((line, col)) = start.split_once(':') {
let line_n =
line.parse::<usize>().map(|l| l + page.fm_offset()).ok();
let line = line_n
.map(|i| i.to_string())
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(line));
let line = line.as_ref();
let line = line
.parse::<i64>()
.map(|l| l + i64::try_from(page.fm_offset()).unwrap_or(l - 1))
.ok()
.unwrap_or(-1);
let col = col.parse::<i64>().ok().unwrap_or(0);
let ic = get_issue_couter();
tracing::warn!(
source = "redirected-link",
Expand Down
91 changes: 70 additions & 21 deletions crates/rari-doc/src/issues.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{BTreeMap, HashMap};
use std::fmt;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::AtomicI64;
use std::sync::{Arc, Mutex};

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

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

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

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

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

Expand All @@ -41,26 +45,26 @@ pub struct Issues<'a> {
#[derive(Clone, Debug, Serialize)]
pub struct TemplIssue<'a> {
pub req: u64,
pub ic: u64,
pub ic: i64,
pub source: &'a str,
pub file: &'a str,
pub slug: &'a str,
pub locale: &'a str,
pub line: &'a str,
pub col: &'a str,
pub line: i64,
pub col: i64,
pub tail: Vec<(&'static str, &'a str)>,
}

static UNKNOWN: &str = "unknown";
static DEFAULT_TEMPL_ISSUE: TemplIssue<'static> = TemplIssue {
req: 0,
ic: 0,
ic: -1,
source: UNKNOWN,
file: UNKNOWN,
slug: UNKNOWN,
locale: UNKNOWN,
line: UNKNOWN,
col: UNKNOWN,
line: -1,
col: -1,
tail: vec![],
};

Expand All @@ -78,15 +82,15 @@ impl<'a> From<&'a Issue> for TemplIssue<'a> {
"locale" => {
tissue.locale = value.as_str();
}
"line" => tissue.line = value.as_str(),
"col" => tissue.col = value.as_str(),
"source" => {
tissue.source = value.as_str();
}
"message" => {}
_ => tissue.tail.push((key, value.as_str())),
}
}
tissue.col = value.col;
tissue.line = value.line;
tissue
}
}
Expand All @@ -103,7 +107,7 @@ pub fn issues_by(issues: &[Issue]) -> Issues {
.find_map(|(key, value)| if *key == "templ" { Some(value) } else { None })
{
templ.entry(templ_name).or_default().push(issue);
} else if issue.line != UNKNOWN {
} else if issue.line != -1 {
other.entry(issue.source).or_default().push(issue)
} else {
no_pos.entry(issue.source).or_default().push(issue);
Expand Down Expand Up @@ -137,8 +141,15 @@ 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" {
}
}
fn record_i64(&mut self, field: &Field, value: i64) {
if field.name() == "ic" {
self.ic = value;
} else if field.name() == "col" {
self.col = value;
} else if field.name() == "line" {
self.line = value;
}
}
}
Expand All @@ -152,8 +163,15 @@ 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" {
}
}
fn record_i64(&mut self, field: &Field, value: i64) {
if field.name() == "ic" {
self.ic = value;
} else if field.name() == "col" {
self.col = value;
} else if field.name() == "line" {
self.line = value;
}
}
}
Expand All @@ -180,6 +198,8 @@ where
let mut issue = Issue {
req: 0,
ic: 0,
col: 0,
line: 0,
fields: vec![],
spans: vec![],
};
Expand All @@ -191,6 +211,12 @@ where
if entries.req != 0 {
issue.req = entries.req;
}
if entries.col != 0 {
issue.col = entries.col;
}
if entries.line != 0 {
issue.line = entries.line;
}
if entries.ic != 0 {
issue.ic = entries.ic;
} else {
Expand All @@ -212,20 +238,23 @@ pub enum Additional {
BrokenLink {
href: String,
},
MacroBrokenLink {
href: String,
},
#[default]
None,
}

#[derive(Serialize, Debug, Default, Clone)]
pub struct DisplayIssue {
pub id: u64,
pub id: i64,
pub explanation: Option<String>,
pub suggestion: Option<String>,
pub fixable: Option<bool>,
pub fixed: bool,
pub name: String,
pub line: Option<usize>,
pub col: Option<usize>,
pub line: Option<i64>,
pub col: Option<i64>,
#[serde(flatten)]
pub additional: Additional,
}
Expand All @@ -236,13 +265,22 @@ impl From<Issue> for DisplayIssue {
fn from(value: Issue) -> Self {
let mut di = DisplayIssue {
id: value.ic,
col: if value.col == 0 {
None
} else {
Some(value.col)
},
line: if value.line == 0 {
None
} else {
Some(value.line)
},
..Default::default()
};
let mut additional = HashMap::new();
println!("{value:?}");
for (key, value) in value.spans.into_iter().chain(value.fields.into_iter()) {
match key {
"line" => di.line = value.parse().ok(),
"col" => di.col = value.parse().ok(),
"source" => {
di.name = value;
}
Expand All @@ -262,6 +300,13 @@ impl From<Issue> for DisplayIssue {
href: additional.remove("url").unwrap_or_default(),
}
}
"macro-redirected-link" => {
di.fixed = false;
di.fixable = Some(true);
Additional::MacroBrokenLink {
href: additional.remove("url").unwrap_or_default(),
}
}
_ => Additional::None,
};
di.additional = additional;
Expand All @@ -278,6 +323,10 @@ pub fn to_display_issues(issues: Vec<Issue>) -> DisplayIssues {
let entry: &mut Vec<_> = map.entry("broken_links").or_default();
entry.push(di);
}
Additional::MacroBrokenLink { .. } => {
let entry: &mut Vec<_> = map.entry("macros").or_default();
entry.push(di);
}
Additional::None => {
let entry: &mut Vec<_> = map.entry("unknown").or_default();
entry.push(di);
Expand Down
23 changes: 17 additions & 6 deletions crates/rari-doc/src/templ/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rari_types::locale::Locale;

use crate::error::DocError;
use crate::html::links::render_link_via_page;
use crate::issues::get_issue_couter;
use crate::pages::page::Page;
use crate::percent::PATH_SEGMENT;
use crate::redirects::resolve_redirect;
Expand All @@ -23,13 +24,23 @@ impl RariApi {
pub fn get_page(url: &str) -> Result<Page, DocError> {
let redirect = resolve_redirect(url);
let url = match redirect.as_ref() {
Some(redirect) if deny_warnings() => {
return Err(DocError::RedirectedLink {
from: url.to_string(),
to: redirect.to_string(),
})
Some(redirect) => {
let ic = get_issue_couter();
tracing::warn!(
source = "macro-redirected-link",
ic = ic,
url = url,
href = redirect.as_ref()
);
if deny_warnings() {
return Err(DocError::RedirectedLink {
from: url.to_string(),
to: redirect.to_string(),
});
} else {
redirect
}
}
Some(redirect) => redirect,
None => url,
};
Page::from_url_with_fallback(url).map_err(Into::into)
Expand Down
10 changes: 3 additions & 7 deletions crates/rari-doc/src/templ/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,9 @@ pub(crate) fn render(env: &RariEnv, input: &str, offset: usize) -> Result<Render
Token::Macro(mac) => {
let ident = &mac.ident;
let name = ident.to_ascii_lowercase();
let span = span!(
Level::ERROR,
"templ",
templ = name,
line = mac.pos.0 + offset,
col = mac.pos.1
);
let line = i64::try_from(mac.pos.0 + offset).unwrap_or(-1);
let col = i64::try_from(mac.pos.1).unwrap_or(-1);
let span = span!(Level::ERROR, "templ", templ = name, line = line, col = col);
let _enter = span.enter();
match invoke(env, &name, mac.args) {
Ok((rendered, is_sidebar)) => {
Expand Down
3 changes: 3 additions & 0 deletions crates/rari-doc/src/templ/templs/links/domxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::Cow;

use rari_templ_func::rari_f;
use rari_types::{AnyArg, ArgError};
use tracing::{span, Level};

use crate::error::DocError;
use crate::templ::api::RariApi;
Expand All @@ -13,6 +14,8 @@ pub fn domxref(
anchor: Option<String>,
no_code: Option<AnyArg>,
) -> Result<String, DocError> {
let span = span!(Level::ERROR, "domxref", basepath = "/docs/Web/API/");
let _enter = span.enter();
let display = display.as_deref().filter(|s| !s.is_empty());
let mut display_with_fallback = Cow::Borrowed(display.unwrap_or(api_name.as_str()));
let api = api_name
Expand Down

0 comments on commit 5e23b0f

Please sign in to comment.