-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start to structure code a bit better
- Loading branch information
Showing
5 changed files
with
117 additions
and
28 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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#![allow(dead_code)] | ||
|
||
use { | ||
proc_macro2::{Span, TokenStream}, | ||
quote::ToTokens, | ||
std::cell::RefCell, | ||
}; | ||
|
||
/// A type for collecting procedural macro errors. | ||
#[derive(Default)] | ||
pub struct Errors { | ||
errors: RefCell<Vec<syn::Error>>, | ||
} | ||
|
||
impl Errors { | ||
/// Issue an error relating to a particular `Spanned` structure. | ||
pub fn err(&self, spanned: &impl syn::spanned::Spanned, msg: &str) { | ||
self.err_span(spanned.span(), msg); | ||
} | ||
|
||
/// Issue an error relating to a particular `Span`. | ||
pub fn err_span(&self, span: Span, msg: &str) { | ||
self.push(syn::Error::new(span, msg)); | ||
} | ||
|
||
/// Issue an error spanning over the given syntax tree node. | ||
pub fn err_span_tokens<T: ToTokens>(&self, tokens: T, msg: &str) { | ||
self.push(syn::Error::new_spanned(tokens, msg)); | ||
} | ||
|
||
/// Push a `syn::Error` onto the list of errors to issue. | ||
pub fn push(&self, err: syn::Error) { | ||
self.errors.borrow_mut().push(err); | ||
} | ||
|
||
/// Convert a `syn::Result` to an `Option`, logging the error if present. | ||
pub fn ok<T>(&self, r: syn::Result<T>) -> Option<T> { | ||
match r { | ||
Ok(v) => Some(v), | ||
Err(e) => { | ||
self.push(e); | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl ToTokens for Errors { | ||
/// Convert the errors into tokens that, when emit, will cause | ||
/// the user of the macro to receive compiler errors. | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
tokens.extend(self.errors.borrow().iter().map(|e| e.to_compile_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
error: Only Structs with named fields are supported | ||
error: `#[derive(VennDB)]` cannot be applied to enums | ||
--> tests/fails/derive_enum.rs:4:1 | ||
| | ||
4 | / enum MyEnum { | ||
5 | | A, | ||
6 | | B, | ||
7 | | } | ||
| |_^ | ||
4 | enum MyEnum { | ||
| ^^^^ |
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,5 +1,5 @@ | ||
error: Only Structs with named fields are supported | ||
error: `#![derive(VennDB)]` is not currently supported on tuple structs | ||
--> tests/fails/derive_tuple_struct.rs:4:1 | ||
| | ||
4 | struct MyStruct(u32); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^ |