-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
derive: Refactor to use
syn::Error::to_compile_error
- Loading branch information
Showing
4 changed files
with
32 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,41 @@ | ||
use core::fmt::Display; | ||
use std::mem; | ||
|
||
use proc_macro2::{Span, TokenStream}; | ||
use quote::ToTokens; | ||
|
||
use crate::util::error; | ||
use proc_macro2::Span; | ||
use syn::Error; | ||
|
||
pub struct Ctxt { | ||
errors: Option<TokenStream>, | ||
error: Option<Error>, | ||
} | ||
|
||
impl Ctxt { | ||
pub fn new() -> Self { | ||
Self { | ||
errors: Some(TokenStream::new()), | ||
Self { error: None } | ||
} | ||
|
||
pub fn add_error(&mut self, error: Error) { | ||
if let Some(ref mut e) = self.error { | ||
e.combine(error); | ||
} else { | ||
self.error = Some(error); | ||
} | ||
} | ||
|
||
pub fn error(&mut self, msg: &str, span: Span) { | ||
error(msg, span).to_tokens(self.errors.as_mut().unwrap()); | ||
pub fn add_error_message<T: Display>(&mut self, span: Span, msg: T) { | ||
self.add_error(Error::new(span, msg)); | ||
} | ||
|
||
pub fn emit_errors(mut self) -> Option<TokenStream> { | ||
let errors = self.errors.take().unwrap(); | ||
pub fn take_error(mut self) -> Option<Error> { | ||
let error = self.error.take(); | ||
mem::forget(self); | ||
if errors.is_empty() { | ||
None | ||
} else { | ||
Some(errors) | ||
} | ||
error | ||
} | ||
} | ||
|
||
impl Drop for Ctxt { | ||
fn drop(&mut self) { | ||
if !std::thread::panicking() { | ||
panic!("must call `Ctxt::emit_errors`"); | ||
panic!("must call `Ctxt::take_error`"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters