Skip to content

Commit

Permalink
refactor: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 27, 2024
1 parent f611cce commit b8d4c06
Show file tree
Hide file tree
Showing 40 changed files with 172 additions and 132 deletions.
5 changes: 4 additions & 1 deletion miette-derive/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ impl Parse for Code {
Ok(Code(input.parse::<syn::LitStr>()?.value()))
}
} else {
Err(syn::Error::new(ident.span(), "diagnostic code is required. Use #[diagnostic(code = ...)] or #[diagnostic(code(...))] to define one."))
Err(syn::Error::new(
ident.span(),
"diagnostic code is required. Use #[diagnostic(code = ...)] or #[diagnostic(code(...))] to define one.",
))
}
}
}
Expand Down
24 changes: 13 additions & 11 deletions miette-derive/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::{punctuated::Punctuated, DeriveInput, Token};

use crate::code::Code;
use crate::diagnostic_arg::DiagnosticArg;
use crate::diagnostic_source::DiagnosticSource;
use crate::forward::{Forward, WhichFn};
use crate::help::Help;
use crate::label::Labels;
use crate::related::Related;
use crate::severity::Severity;
use crate::source_code::SourceCode;
use crate::url::Url;
use crate::{
code::Code,
diagnostic_arg::DiagnosticArg,
diagnostic_source::DiagnosticSource,
forward::{Forward, WhichFn},
help::Help,
label::Labels,
related::Related,
severity::Severity,
source_code::SourceCode,
url::Url,
};

pub enum Diagnostic {
Struct {
Expand Down Expand Up @@ -248,7 +250,7 @@ impl Diagnostic {
return Err(syn::Error::new(
input.ident.span(),
"Can't derive Diagnostic for Unions",
))
));
}
})
}
Expand Down
6 changes: 1 addition & 5 deletions miette-derive/src/diagnostic_arg.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use syn::parse::{Parse, ParseStream};

use crate::code::Code;
use crate::forward::Forward;
use crate::help::Help;
use crate::severity::Severity;
use crate::url::Url;
use crate::{code::Code, forward::Forward, help::Help, severity::Severity, url::Url};

pub enum DiagnosticArg {
Transparent,
Expand Down
2 changes: 1 addition & 1 deletion miette-derive/src/diagnostic_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::spanned::Spanned;

use crate::forward::WhichFn;
use crate::{
diagnostic::{DiagnosticConcreteArgs, DiagnosticDef},
forward::WhichFn,
utils::{display_pat_members, gen_all_variants_with},
};

Expand Down
13 changes: 8 additions & 5 deletions miette-derive/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// NOTE: Most code in this file is taken straight from `thiserror`.
use std::collections::HashSet as Set;
use std::iter::FromIterator;
use std::{collections::HashSet as Set, iter::FromIterator};

use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use syn::ext::IdentExt;
use syn::parse::{ParseStream, Parser};
use syn::{braced, bracketed, parenthesized, Ident, Index, LitStr, Member, Result, Token};
use syn::{
braced, bracketed,
ext::IdentExt,
parenthesized,
parse::{ParseStream, Parser},
Ident, Index, LitStr, Member, Result, Token,
};

#[derive(Clone)]
pub struct Display {
Expand Down
5 changes: 2 additions & 3 deletions miette-derive/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use syn::{

use crate::{
diagnostic::{DiagnosticConcreteArgs, DiagnosticDef},
utils::{display_pat_members, gen_all_variants_with},
};
use crate::{
fmt::{self, Display},
forward::WhichFn,
utils::{display_pat_members, gen_all_variants_with},
};

pub enum Help {
Expand Down Expand Up @@ -77,6 +75,7 @@ impl Help {
}
Ok(None)
}

pub(crate) fn gen_enum(variants: &[DiagnosticDef]) -> Option<TokenStream> {
gen_all_variants_with(
variants,
Expand Down
10 changes: 8 additions & 2 deletions miette-derive/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ impl Parse for LabelAttr {
LabelType::Collection
}
Some(_) => {
return Err(syn::Error::new(input.span(), "Invalid argument to label() attribute. The argument must be a literal string or either the keyword `primary` or `collection`."));
return Err(syn::Error::new(
input.span(),
"Invalid argument to label() attribute. The argument must be a literal string or either the keyword `primary` or `collection`.",
));
}
_ => LabelType::Default,
};
Expand All @@ -79,7 +82,10 @@ impl Parse for LabelAttr {
let display = Display { fmt, args, has_bonus_display: false };
(attr, Some(display))
} else if !content.is_empty() {
return Err(syn::Error::new(input.span(), "Invalid argument to label() attribute. The argument must be a literal string or either the keyword `primary` or `collection`."));
return Err(syn::Error::new(
input.span(),
"Invalid argument to label() attribute. The argument must be a literal string or either the keyword `primary` or `collection`.",
));
} else {
(attr, None)
}
Expand Down
3 changes: 1 addition & 2 deletions miette-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use diagnostic::Diagnostic;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

use diagnostic::Diagnostic;

mod code;
mod diagnostic;
mod diagnostic_arg;
Expand Down
9 changes: 5 additions & 4 deletions miette-derive/src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ use syn::{

use crate::{
diagnostic::{DiagnosticConcreteArgs, DiagnosticDef},
utils::{display_pat_members, gen_all_variants_with, gen_unused_pat},
};
use crate::{
fmt::{self, Display},
forward::WhichFn,
utils::{display_pat_members, gen_all_variants_with, gen_unused_pat},
};

pub enum Url {
Expand Down Expand Up @@ -42,7 +40,10 @@ impl Parse for Url {
if option == "docsrs" {
Ok(Url::DocsRs)
} else {
Err(syn::Error::new(option.span(), "Invalid argument to url() sub-attribute. It must be either a string or a plain `docsrs` identifier"))
Err(syn::Error::new(
option.span(),
"Invalid argument to url() sub-attribute. It must be either a string or a plain `docsrs` identifier",
))
}
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion miette-derive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ pub(crate) fn gen_all_variants_with(
})
}

use crate::fmt::Display;
use std::collections::HashSet;

use crate::fmt::Display;

pub(crate) fn gen_unused_pat(fields: &syn::Fields) -> TokenStream {
match fields {
syn::Fields::Named(_) => quote! { { .. } },
Expand Down
3 changes: 1 addition & 2 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ Iterate over error `.source()` chains.
NOTE: This module is taken wholesale from <https://crates.io/crates/eyre>.
*/
use std::error::Error as StdError;
use std::vec;
use std::{error::Error as StdError, vec};

use ChainState::*;

Expand Down
7 changes: 4 additions & 3 deletions src/eyreish/context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::error::{ContextError, ErrorImpl};
use super::{Report, WrapErr};
use core::fmt::{self, Debug, Display, Write};

use std::error::Error as StdError;

use super::{
error::{ContextError, ErrorImpl},
Report, WrapErr,
};
use crate::{Diagnostic, LabeledSpan};

mod ext {
Expand Down
23 changes: 12 additions & 11 deletions src/eyreish/error.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use core::any::TypeId;
use core::fmt::{self, Debug, Display};
use core::mem::ManuallyDrop;
use core::ptr::{self, NonNull};
use core::{
any::TypeId,
fmt::{self, Debug, Display},
mem::ManuallyDrop,
ops::{Deref, DerefMut},
ptr::{self, NonNull},
};
use std::error::Error as StdError;

use super::ptr::{Mut, Own, Ref};
use super::Report;
use super::ReportHandler;
use crate::chain::Chain;
use crate::eyreish::wrapper::WithSourceCode;
use crate::{Diagnostic, SourceCode};
use core::ops::{Deref, DerefMut};
use super::{
ptr::{Mut, Own, Ref},
Report, ReportHandler,
};
use crate::{chain::Chain, eyreish::wrapper::WithSourceCode, Diagnostic, SourceCode};

impl Report {
/// Create a new error object from any error type.
Expand Down
3 changes: 2 additions & 1 deletion src/eyreish/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{error::ErrorImpl, ptr::Ref};
use core::fmt;

use super::{error::ErrorImpl, ptr::Ref};

impl ErrorImpl<()> {
pub(crate) unsafe fn display(this: Ref<'_, Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
this.deref()
Expand Down
2 changes: 1 addition & 1 deletion src/eyreish/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
// let error = $msg;
// (&error).miette_kind().new(error)

use super::Report;
use core::fmt::{Debug, Display};

use super::Report;
use crate::Diagnostic;

pub struct Adhoc;
Expand Down
18 changes: 6 additions & 12 deletions src/eyreish/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![allow(clippy::needless_doctest_main, clippy::new_ret_no_self, clippy::wrong_self_convention)]
use core::fmt::Display;
use std::{error::Error as StdError, sync::OnceLock};

use std::error::Error as StdError;
use std::sync::OnceLock;

use error::ErrorImpl;
#[allow(unreachable_pub)]
pub use into_diagnostic::*;
#[doc(hidden)]
Expand All @@ -19,16 +18,13 @@ pub use ReportHandler as EyreContext;
#[allow(unreachable_pub)]
pub use WrapErr as Context;

use self::ptr::Own;
#[cfg(not(feature = "fancy-base"))]
use crate::DebugReportHandler;
use crate::Diagnostic;
#[cfg(feature = "fancy-base")]
use crate::MietteHandler;

use error::ErrorImpl;

use self::ptr::Own;

mod context;
mod error;
mod fmt;
Expand Down Expand Up @@ -446,16 +442,14 @@ pub trait WrapErr<T, E>: context::private::Sealed {
// Private API. Referenced by macro-generated code.
#[doc(hidden)]
pub mod private {
use super::Report;
use core::fmt::{Debug, Display};

pub use core::result::Result::Err;

use super::Report;

#[doc(hidden)]
pub mod kind {
pub use super::super::kind::{AdhocKind, TraitKind};

pub use super::super::kind::BoxedKind;
pub use super::super::kind::{AdhocKind, BoxedKind, TraitKind};
}

#[track_caller]
Expand Down
4 changes: 1 addition & 3 deletions src/eyreish/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use core::fmt::{self, Debug, Display};

use std::error::Error as StdError;

use crate::{Diagnostic, LabeledSpan, Report, SourceCode};

use crate as miette;
use crate::{Diagnostic, LabeledSpan, Report, SourceCode};

#[repr(transparent)]
pub(crate) struct MessageError<M>(pub(crate) M);
Expand Down
17 changes: 8 additions & 9 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::fmt;

use crate::highlighters::Highlighter;
use crate::highlighters::MietteHighlighter;
use crate::protocol::Diagnostic;
use crate::GraphicalReportHandler;
use crate::GraphicalTheme;
use crate::NarratableReportHandler;
use crate::ReportHandler;
use crate::ThemeCharacters;
use crate::ThemeStyles;
use crate::{
highlighters::{Highlighter, MietteHighlighter},
protocol::Diagnostic,
GraphicalReportHandler, GraphicalTheme, NarratableReportHandler, ReportHandler,
ThemeCharacters, ThemeStyles,
};

/// Settings to control the color format used for graphical rendering.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -149,6 +146,7 @@ impl MietteHandlerOpts {
self.break_words = Some(break_words);
self
}

/// Sets the `textwrap::WordSeparator` to use when determining wrap points.
pub fn word_separator(mut self, word_separator: textwrap::WordSeparator) -> Self {
self.word_separator = Some(word_separator);
Expand All @@ -160,6 +158,7 @@ impl MietteHandlerOpts {
self.word_splitter = Some(word_splitter);
self
}

/// Include the cause chain of the top-level error in the report.
pub fn with_cause_chain(mut self) -> Self {
self.with_cause_chain = Some(true);
Expand Down
5 changes: 4 additions & 1 deletion src/handlers/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ impl DebugReportHandler {
}
diag.finish()?;
writeln!(f)?;
writeln!(f, "NOTE: If you're looking for the fancy error reports, install miette with the `fancy` feature, or write your own and hook it up with miette::set_hook().")
writeln!(
f,
"NOTE: If you're looking for the fancy error reports, install miette with the `fancy` feature, or write your own and hook it up with miette::set_hook()."
)
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fmt::{self, Write};
use std::io::IsTerminal;
use std::{
fmt::{self, Write},
io::IsTerminal,
};

use owo_colors::{OwoColorize, Style};
use unicode_width::UnicodeWidthChar;
Expand Down
8 changes: 5 additions & 3 deletions src/handlers/narratable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::fmt;

use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

use crate::diagnostic_chain::DiagnosticChain;
use crate::protocol::{Diagnostic, Severity};
use crate::{LabeledSpan, MietteError, ReportHandler, SourceCode, SourceSpan, SpanContents};
use crate::{
diagnostic_chain::DiagnosticChain,
protocol::{Diagnostic, Severity},
LabeledSpan, MietteError, ReportHandler, SourceCode, SourceSpan, SpanContents,
};

/**
[`ReportHandler`] that renders plain text and avoids extraneous graphics.
Expand Down
Loading

0 comments on commit b8d4c06

Please sign in to comment.