Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename functions #10

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let warning = Warning::new_deprecated("OldStuffUsed")
.new("my_macro::new()")
.help_link("https:://example.com")
.span(proc_macro2::Span::call_site())
.build();
.build_or_panic();

// Use the warning in a proc macro
let tokens = quote::quote!(#warning);
Expand Down
2 changes: 1 addition & 1 deletion proc-macro-warning/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "proc-macro-warning"
version = "1.0.0-rc.1"
version = "1.0.0"
edition = "2021"
license = "GPL-3.0 OR Apache-2.0"
authors = ["Oliver Tale-Yazdi <oliver@tasty.limo>"]
Expand Down
17 changes: 15 additions & 2 deletions proc-macro-warning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl FormattedWarning {
/// .help_link("https:://example.com")
/// // Normally you use the input span, but this is an example:
/// .span(proc_macro2::Span::call_site())
/// .build();
/// .build_or_panic();
///
/// let mut warnings = vec![warning];
/// // When adding more, you will need to build each with `.index`.
Expand Down Expand Up @@ -161,7 +161,13 @@ impl DeprecatedWarningBuilder {
}

/// Fallibly build a warning.
#[deprecated(note = "Use `try_build` instead; Will be removed after Q1 2024")]
pub fn maybe_build(self) -> Result<Warning, String> {
self.try_build()
}

/// Try to build the warning.
pub fn try_build(self) -> Result<Warning, String> {
let span = self.span.unwrap_or_else(Span::call_site);
let title = self.title;
let old = self.old.ok_or("Missing old")?;
Expand All @@ -173,8 +179,15 @@ impl DeprecatedWarningBuilder {

/// Unwraps [`Self::maybe_build`] for convenience.
#[must_use]
#[deprecated(note = "Use `build_or_panic` instead; Will be removed after Q1 2024")]
pub fn build(self) -> Warning {
self.maybe_build().expect("maybe_build failed")
self.build_or_panic()
}

/// Build the warning or panic if it fails.
#[must_use]
pub fn build_or_panic(self) -> Warning {
self.try_build().expect("maybe_build failed")
}
}

Expand Down
4 changes: 2 additions & 2 deletions proc-macro-warning/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn example_works() {
.new("my_macro::new()")
.help_link("https:://example.com")
.span(proc_macro2::Span::call_site())
.build();
.build_or_panic();
let got_tokens = quote!(#warning);

let want_tokens = quote!(
Expand Down Expand Up @@ -77,7 +77,7 @@ fn warning_debug_works() {
.new("my_macro::new()")
.help_link("https:://example.com")
.span(proc_macro2::Span::call_site())
.build();
.build_or_panic();
let _ = format!("{:?}", warning);
}

Expand Down
2 changes: 1 addition & 1 deletion ui-tests/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn impl_dep(input: TokenStream, span: bool) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);

let warning = proc_macro_warning::Warning::new_deprecated("test").old("foo").new("bar");
let warning = if span { warning.span(input.span()) } else { warning }.build();
let warning = if span { warning.span(input.span()) } else { warning }.build_or_panic();

warning.into_token_stream().into()
}
Expand Down