Skip to content

Commit

Permalink
add validator compilation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenDC committed Apr 18, 2024
1 parent f69dd87 commit 98aaf50
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Example:

```rust
#[derive(Debug, VennDB)]
#[venndb(name = "MyDB", validatator = "my_validator_fn")]
#[venndb(name = "MyDB", validator = my_validator_fn)]
pub struct Value {
pub foo: String,
pub bar: u32,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ Example:

```rust,ignore
#[derive(Debug, VennDB)]
#[venndb(validatator = "my_validator_fn")]
#[venndb(validator = my_validator_fn)]
pub struct Value {
pub foo: String,
pub bar: u32,
Expand Down
76 changes: 76 additions & 0 deletions venndb-macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ impl Errors {
),
];

pub fn expect_path<'a>(&self, e: &'a syn::Expr) -> Option<&'a syn::Path> {
if let syn::Expr::Path(path) = e {
Some(&path.path)
} else {
self.unexpected_value("path", e);
None
}
}

fn unexpected_lit(&self, expected: &str, found: &syn::Expr) {
fn lit_kind(lit: &syn::Lit) -> &'static str {
use syn::Lit::{Bool, Byte, ByteStr, Char, Float, Int, Str, Verbatim};
Expand Down Expand Up @@ -126,6 +135,73 @@ impl Errors {
)
}

fn unexpected_value(&self, expected: &str, found: &syn::Expr) {
fn expr_kind(expr: &syn::Expr) -> &'static str {
use syn::Expr::{
Array, Assign, Async, Await, Binary, Block, Break, Call, Cast, Closure, Const,
Continue, Field, ForLoop, Group, If, Index, Infer, Let, Lit, Loop, Macro, Match,
MethodCall, Paren, Path, Range, Reference, Repeat, Return, Struct, Try, TryBlock,
Tuple, Unary, Unsafe, Verbatim, While, Yield,
};
match expr {
Array(_) => "array",
Assign(_) => "assignment",
Async(_) => "async block",
Await(_) => "await",
Binary(_) => "binary operation",
Block(_) => "block",
Break(_) => "break",
Call(_) => "function call",
Cast(_) => "cast",
Closure(_) => "closure",
Const(_) => "const",
Continue(_) => "continue",
Field(_) => "field access",
ForLoop(_) => "for loop",
Group(_) => "group",
If(_) => "if",
Index(_) => "index",
Infer(_) => "inferred type",
Let(_) => "let",
Lit(_) => "literal",
Loop(_) => "loop",
Macro(_) => "macro",
Match(_) => "match",
MethodCall(_) => "method call",
Paren(_) => "parentheses",
Path(_) => "path",
Range(_) => "range",
Reference(_) => "reference",
Repeat(_) => "repeat",
Return(_) => "return",
Struct(_) => "struct",
Try(_) => "try",
TryBlock(_) => "try block",
Tuple(_) => "tuple",
Unary(_) => "unary operation",
Unsafe(_) => "unsafe block",
Verbatim(_) => "verbatim",
While(_) => "while",
Yield(_) => "yield",
_ => "unknown expression kind",
}
}

self.err(
found,
&[
"Expected ",
expected,
" attribute, found ",
found.to_token_stream().to_string().as_str(),
" attribute (",
expr_kind(found),
")",
]
.concat(),
)
}

/// 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);
Expand Down
5 changes: 5 additions & 0 deletions venndb-macros/src/parse_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ fn is_bool(ty: &syn::Type) -> bool {
#[derive(Default)]
pub struct TypeAttrs {
pub name: Option<syn::LitStr>,
pub validator: Option<syn::Path>,
}

impl TypeAttrs {
Expand All @@ -177,6 +178,10 @@ impl TypeAttrs {
if let Some(m) = errors.expect_meta_name_value(&meta) {
this.name = errors.expect_lit_str(&m.value).cloned();
}
} else if name.is_ident("validator") {
if let Some(m) = errors.expect_meta_name_value(&meta) {
this.validator = errors.expect_path(&m.value).cloned();
}
} else {
errors.err(
&meta,
Expand Down
6 changes: 5 additions & 1 deletion venndb-usage/tests/compiles/derive_struct_all_the_things.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use venndb::{Any, VennDB};

#[derive(Debug, VennDB)]
#[venndb(name = "EmployeeSheet")]
#[venndb(name = "EmployeeSheet", validator = employee_validator)]
struct Employee {
#[venndb(key)]
id: u32,
Expand All @@ -27,6 +27,10 @@ pub enum Department {
HR,
}

fn employee_validator(employee: &Employee) -> bool {
employee.id > 0 && !employee.name.is_empty()
}

impl Any for Department {
fn is_any(&self) -> bool {
self == &Department::Any
Expand Down
32 changes: 32 additions & 0 deletions venndb-usage/tests/compiles/derive_struct_with_validator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use venndb::VennDB;

#[derive(Debug, VennDB)]
#[venndb(validator = sealed::employee_validator)]
struct Employee {
pub id: u32,
pub name: String,
pub is_manager: bool,
pub is_admin: bool,
pub is_active: bool,
pub department: Department,
}

#[derive(Debug)]
pub enum Department {
Engineering,
Sales,
Marketing,
HR,
}

mod sealed {
use super::Employee;

pub(super) fn employee_validator(employee: &Employee) -> bool {
employee.id > 0 && !employee.name.is_empty()
}
}

fn main() {
let _ = EmployeeDB::new();
}
28 changes: 28 additions & 0 deletions venndb-usage/tests/fails/derive_struct_with_validator_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use venndb::VennDB;

#[derive(Debug, VennDB)]
#[venndb(validator = "employee_validator")]
struct Employee {
pub id: u32,
pub name: String,
pub is_manager: bool,
pub is_admin: bool,
pub is_active: bool,
pub department: Department,
}

#[derive(Debug)]
pub enum Department {
Engineering,
Sales,
Marketing,
HR,
}

fn employee_validator(employee: &Employee) -> bool {
employee.id > 0 && !employee.name.is_empty()
}

fn main() {
let _ = EmployeeDB::new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Expected path attribute, found "employee_validator" attribute (literal)
--> tests/fails/derive_struct_with_validator_str.rs:4:22
|
4 | #[venndb(validator = "employee_validator")]
| ^^^^^^^^^^^^^^^^^^^^

0 comments on commit 98aaf50

Please sign in to comment.