Skip to content

Commit dac1647

Browse files
feat: add project setup (#1)
1 parent dd85c1a commit dac1647

12 files changed

Lines changed: 403 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 245 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ version = "0.0.1"
1111

1212
[workspace.dependencies]
1313
fortifier = { path = "./packages/fortifier", version = "0.0.1" }
14+
fortifier-macros = { path = "./packages/fortifier-macros", version = "0.0.1" }

deny.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ multiple-versions = "allow"
1010
wildcards = "deny"
1111

1212
[licenses]
13-
allow = ["MIT"]
13+
allow = ["MIT", "Unicode-3.0"]
1414
confidence-threshold = 1.0
1515

1616
[sources]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "fortifier-macros"
3+
description = "Macros for Fortifier."
4+
5+
authors.workspace = true
6+
edition.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
version.workspace = true
10+
11+
[lib]
12+
proc-macro = true
13+
14+
[dependencies]
15+
proc-macro2 = "1.0.103"
16+
quote = "1.0.42"
17+
syn = "2.0.110"
18+
19+
[dev-dependencies]
20+
fortifier.workspace = true
21+
trybuild = "1.0.114"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h1 align="center">Fortifier Macros</h1>
2+
3+
Macros for Fortifier.
4+
5+
## Documentation
6+
7+
See [the Fortifier book](https://fortifier.rustforweb.org/) for documentation.
8+
9+
## Rust for Web
10+
11+
The Fortifier project is part of [Rust for Web](https://github.com/RustForWeb).
12+
13+
[Rust for Web](https://github.com/RustForWeb) creates and ports web libraries for Rust. All projects are free and open source.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use proc_macro2::TokenStream;
2+
use quote::{format_ident, quote};
3+
use syn::{Data, DeriveInput, Fields};
4+
5+
pub fn validate_tokens(input: DeriveInput) -> TokenStream {
6+
match input.data {
7+
Data::Struct(data_struct) => match data_struct.fields {
8+
Fields::Named(_fields_named) => {
9+
// TODO
10+
}
11+
Fields::Unnamed(_fields_unnamed) => todo!("fields unamed"),
12+
Fields::Unit => todo!("fields unit"),
13+
},
14+
Data::Enum(_data_enum) => todo!("data enum"),
15+
Data::Union(_data_union) => todo!("data union"),
16+
}
17+
18+
let ident = input.ident;
19+
let error_ident = format_ident!("{ident}ValidationError");
20+
21+
quote! {
22+
#[derive(Debug)]
23+
struct #error_ident {}
24+
25+
impl ::std::fmt::Display for #error_ident {
26+
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
27+
write!(f, "")
28+
}
29+
}
30+
31+
impl ::std::error::Error for #error_ident {}
32+
33+
impl Validate for #ident {
34+
type Error = #error_ident;
35+
36+
fn validate_sync(&self) -> Result<(), Self::Error> {
37+
Ok(())
38+
}
39+
40+
fn validate_async(&self) -> ::std::pin::Pin<Box<impl Future<Output = Result<(), Self::Error>>>> {
41+
Box::pin(async {
42+
Ok(())
43+
})
44+
}
45+
}
46+
}
47+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod derive;
2+
3+
use proc_macro::TokenStream;
4+
use syn::{DeriveInput, parse_macro_input};
5+
6+
use crate::derive::validate_tokens;
7+
8+
#[proc_macro_derive(Validate, attributes(validate))]
9+
pub fn derive(input: TokenStream) -> TokenStream {
10+
let input = parse_macro_input!(input as DeriveInput);
11+
12+
validate_tokens(input).into()
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use trybuild::TestCases;
2+
3+
#[test]
4+
fn derive() {
5+
let t = TestCases::new();
6+
t.pass("tests/derive/*_pass.rs");
7+
t.compile_fail("tests/derive/*_fail.rs");
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::error::Error;
2+
3+
use fortifier::Validate;
4+
5+
#[derive(Validate)]
6+
struct CreateUser {
7+
#[validate(email)]
8+
email: String,
9+
10+
#[validate(length(min = 1, max = 256))]
11+
name: String,
12+
}
13+
14+
fn main() -> Result<(), Box<dyn Error>> {
15+
let data = CreateUser {
16+
email: "john@doe.com".to_owned(),
17+
name: "John Doe".to_owned(),
18+
};
19+
20+
data.validate_sync()?;
21+
22+
Ok(())
23+
}

0 commit comments

Comments
 (0)