Skip to content

Commit 6dc0944

Browse files
committed
Move diagnostics to a separate library
This will make it possible to share it across crates.
1 parent 39239d0 commit 6dc0944

File tree

7 files changed

+69
-54
lines changed

7 files changed

+69
-54
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ resolver = "2"
1010
[workspace.package]
1111
edition = "2024"
1212
license = "MIT OR Apache-2.0"
13+
14+
[workspace.dependencies]
15+
diagnostics = { path = "tools/diagnostics" }

tools/diagnostics/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "diagnostics"
3+
edition.workspace = true
4+
license.workspace = true
5+
6+
[dependencies]

tools/diagnostics/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! Very basic diagnostics output support.
2+
3+
use std::fmt;
4+
5+
/// Handler for errors and warnings.
6+
pub struct Diagnostics {
7+
/// Whether or not warnings should be errors (set by SPEC_DENY_WARNINGS
8+
/// environment variable).
9+
pub deny_warnings: bool,
10+
/// Number of messages generated.
11+
pub count: u32,
12+
}
13+
14+
impl Diagnostics {
15+
pub fn new() -> Diagnostics {
16+
let deny_warnings = std::env::var("SPEC_DENY_WARNINGS").as_deref() == Ok("1");
17+
Diagnostics {
18+
deny_warnings,
19+
count: 0,
20+
}
21+
}
22+
23+
/// Displays a warning or error (depending on whether warnings are denied).
24+
///
25+
/// Usually you want the [`warn_or_err!`] macro.
26+
pub fn warn_or_err(&mut self, args: fmt::Arguments<'_>) {
27+
if self.deny_warnings {
28+
eprintln!("error: {args}");
29+
} else {
30+
eprintln!("warning: {args}");
31+
}
32+
self.count += 1;
33+
}
34+
}
35+
36+
/// Displays a warning or error (depending on whether warnings are denied).
37+
#[macro_export]
38+
macro_rules! warn_or_err {
39+
($diag:expr, $($arg:tt)*) => {
40+
$diag.warn_or_err(format_args!($($arg)*));
41+
};
42+
}
43+
44+
/// Displays a message for an internal error, and immediately exits.
45+
#[macro_export]
46+
macro_rules! bug {
47+
($($arg:tt)*) => {
48+
eprintln!("mdbook-spec internal error: {}", format_args!($($arg)*));
49+
std::process::exit(1);
50+
};
51+
}
52+

tools/mdbook-spec/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ default-run = "mdbook-spec"
1010

1111
[dependencies]
1212
anyhow = "1.0.79"
13+
diagnostics.workspace = true
1314
mdbook-markdown = "0.5.1"
1415
mdbook-preprocessor = "0.5.1"
1516
once_cell = "1.19.0"

tools/mdbook-spec/src/lib.rs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
use crate::rules::Rules;
44
use anyhow::{Context, Result, bail};
5-
use mdbook_preprocessor::book::BookItem;
6-
use mdbook_preprocessor::book::{Book, Chapter};
7-
use mdbook_preprocessor::errors::Error;
8-
use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
5+
use diagnostics::{Diagnostics, warn_or_err};
96
use once_cell::sync::Lazy;
107
use regex::{Captures, Regex};
118
use semver::{Version, VersionReq};
12-
use std::fmt;
139
use std::io;
1410
use std::ops::Range;
1511
use std::path::PathBuf;
@@ -47,54 +43,6 @@ pub fn handle_preprocessing() -> Result<(), Error> {
4743
Ok(())
4844
}
4945

50-
/// Handler for errors and warnings.
51-
pub struct Diagnostics {
52-
/// Whether or not warnings should be errors (set by SPEC_DENY_WARNINGS
53-
/// environment variable).
54-
deny_warnings: bool,
55-
/// Number of messages generated.
56-
count: u32,
57-
}
58-
59-
impl Diagnostics {
60-
fn new() -> Diagnostics {
61-
let deny_warnings = std::env::var("SPEC_DENY_WARNINGS").as_deref() == Ok("1");
62-
Diagnostics {
63-
deny_warnings,
64-
count: 0,
65-
}
66-
}
67-
68-
/// Displays a warning or error (depending on whether warnings are denied).
69-
///
70-
/// Usually you want the [`warn_or_err!`] macro.
71-
fn warn_or_err(&mut self, args: fmt::Arguments<'_>) {
72-
if self.deny_warnings {
73-
eprintln!("error: {args}");
74-
} else {
75-
eprintln!("warning: {args}");
76-
}
77-
self.count += 1;
78-
}
79-
}
80-
81-
/// Displays a warning or error (depending on whether warnings are denied).
82-
#[macro_export]
83-
macro_rules! warn_or_err {
84-
($diag:expr, $($arg:tt)*) => {
85-
$diag.warn_or_err(format_args!($($arg)*));
86-
};
87-
}
88-
89-
/// Displays a message for an internal error, and immediately exits.
90-
#[macro_export]
91-
macro_rules! bug {
92-
($($arg:tt)*) => {
93-
eprintln!("mdbook-spec internal error: {}", format_args!($($arg)*));
94-
std::process::exit(1);
95-
};
96-
}
97-
9846
pub struct Spec {
9947
/// Path to the rust-lang/rust git repository (set by SPEC_RUST_ROOT
10048
/// environment variable).

tools/mdbook-spec/src/std_links.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Support for translating links to the standard library.
22
3-
use crate::{Diagnostics, bug, warn_or_err};
43
use anyhow::{Result, bail};
4+
use diagnostics::{Diagnostics, bug, warn_or_err};
55
use mdbook_markdown::pulldown_cmark::{BrokenLink, CowStr, Event, LinkType, Options, Parser, Tag};
66
use mdbook_preprocessor::book::BookItem;
77
use mdbook_preprocessor::book::{Book, Chapter};

0 commit comments

Comments
 (0)