Skip to content
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
245 changes: 245 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ version = "0.0.1"

[workspace.dependencies]
fortifier = { path = "./packages/fortifier", version = "0.0.1" }
fortifier-macros = { path = "./packages/fortifier-macros", version = "0.0.1" }
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ multiple-versions = "allow"
wildcards = "deny"

[licenses]
allow = ["MIT"]
allow = ["MIT", "Unicode-3.0"]
confidence-threshold = 1.0

[sources]
Expand Down
21 changes: 21 additions & 0 deletions packages/fortifier-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "fortifier-macros"
description = "Macros for Fortifier."

authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.103"
quote = "1.0.42"
syn = "2.0.110"

[dev-dependencies]
fortifier.workspace = true
trybuild = "1.0.114"
13 changes: 13 additions & 0 deletions packages/fortifier-macros/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1 align="center">Fortifier Macros</h1>

Macros for Fortifier.

## Documentation

See [the Fortifier book](https://fortifier.rustforweb.org/) for documentation.

## Rust for Web

The Fortifier project is part of [Rust for Web](https://github.com/RustForWeb).

[Rust for Web](https://github.com/RustForWeb) creates and ports web libraries for Rust. All projects are free and open source.
47 changes: 47 additions & 0 deletions packages/fortifier-macros/src/derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{Data, DeriveInput, Fields};

pub fn validate_tokens(input: DeriveInput) -> TokenStream {
match input.data {
Data::Struct(data_struct) => match data_struct.fields {
Fields::Named(_fields_named) => {
// TODO
}
Fields::Unnamed(_fields_unnamed) => todo!("fields unamed"),
Fields::Unit => todo!("fields unit"),
},
Data::Enum(_data_enum) => todo!("data enum"),
Data::Union(_data_union) => todo!("data union"),
}

let ident = input.ident;
let error_ident = format_ident!("{ident}ValidationError");

quote! {
#[derive(Debug)]
struct #error_ident {}

impl ::std::fmt::Display for #error_ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "")
}
}

impl ::std::error::Error for #error_ident {}

impl Validate for #ident {
type Error = #error_ident;

fn validate_sync(&self) -> Result<(), Self::Error> {
Ok(())
}

fn validate_async(&self) -> ::std::pin::Pin<Box<impl Future<Output = Result<(), Self::Error>>>> {
Box::pin(async {
Ok(())
})
}
}
}
}
13 changes: 13 additions & 0 deletions packages/fortifier-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod derive;

use proc_macro::TokenStream;
use syn::{DeriveInput, parse_macro_input};

use crate::derive::validate_tokens;

#[proc_macro_derive(Validate, attributes(validate))]
pub fn derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);

validate_tokens(input).into()
}
8 changes: 8 additions & 0 deletions packages/fortifier-macros/tests/derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use trybuild::TestCases;

#[test]
fn derive() {
let t = TestCases::new();
t.pass("tests/derive/*_pass.rs");
t.compile_fail("tests/derive/*_fail.rs");
}
23 changes: 23 additions & 0 deletions packages/fortifier-macros/tests/derive/basic_pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::error::Error;

use fortifier::Validate;

#[derive(Validate)]
struct CreateUser {
#[validate(email)]
email: String,

#[validate(length(min = 1, max = 256))]
name: String,
}

fn main() -> Result<(), Box<dyn Error>> {
let data = CreateUser {
email: "john@doe.com".to_owned(),
name: "John Doe".to_owned(),
};

data.validate_sync()?;

Ok(())
}
Loading
Loading