-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b14bb7c
commit 60a4fa9
Showing
1 changed file
with
76 additions
and
66 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,72 +1,82 @@ | ||
// #[macro_export] | ||
// macro_rules! selection { | ||
// ( $s:expr, { $($n:ident),+ } ) => {{ | ||
// #[allow(non_camel_case_types)] | ||
// mod selection { | ||
// use $crate::Type; | ||
// #[derive(serde::Serialize, Type)] | ||
// #[specta(inline)] | ||
// pub struct Selection<$($n,)*> { | ||
// $(pub $n: $n,)* | ||
// } | ||
// } | ||
// use selection::Selection; | ||
// #[allow(non_camel_case_types)] | ||
// Selection { $($n: $s.$n,)* } | ||
// }}; | ||
// ( $s:expr, [{ $($n:ident),+ }] ) => {{ | ||
// #[allow(non_camel_case_types)] | ||
// mod selection { | ||
// use $crate::Type; | ||
// #[derive(serde::Serialize, Type)] | ||
// #[specta(inline, crate = $crate)] | ||
// pub struct Selection<$($n,)*> { | ||
// $(pub $n: $n,)* | ||
// } | ||
// } | ||
// use selection::Selection; | ||
// #[allow(non_camel_case_types)] | ||
// $s.into_iter().map(|v| Selection { $($n: v.$n,)* }).collect::<Vec<_>>() | ||
// }}; | ||
// } | ||
#[macro_export] | ||
macro_rules! selection { | ||
( $s:expr, { $($n:ident),+ } ) => {{ | ||
#[allow(non_camel_case_types)] | ||
mod selection { | ||
// Due to macro expansion order `$crate` can't go in the Specta attribute but when hardcoding `rspc` macro hygiene get's angry. | ||
// This hack is to avoid both issues. | ||
mod _rspc { | ||
pub use $crate::*; | ||
} | ||
|
||
// #[cfg(test)] | ||
// mod tests { | ||
// use specta::{ts::inline, Type}; | ||
#[derive(serde::Serialize, _rspc::Type)] | ||
#[specta(inline, crate = "_rspc::internal::specta")] | ||
pub struct Selection<$($n,)*> { | ||
$(pub $n: $n),* | ||
} | ||
} | ||
use selection::Selection; | ||
#[allow(non_camel_case_types)] | ||
Selection { $($n: $s.$n,)* } | ||
}}; | ||
( $s:expr, [{ $($n:ident),+ }] ) => {{ | ||
#[allow(non_camel_case_types)] | ||
mod selection { | ||
// Due to macro expansion order `$crate` can't go in the Specta attribute but when hardcoding `rspc` macro hygiene get's angry. | ||
// This hack is to avoid both issues. | ||
mod _rspc { | ||
pub use $crate::*; | ||
} | ||
|
||
// fn ts_export_ref<T: Type>(_t: &T) -> String { | ||
// inline::<T>(&Default::default()).unwrap() | ||
// } | ||
#[derive(serde::Serialize, _rspc::Type)] | ||
#[specta(inline, crate = "_rspc::internal::specta")] | ||
pub struct Selection<$($n,)*> { | ||
$(pub $n: $n,)* | ||
} | ||
} | ||
use selection::Selection; | ||
#[allow(non_camel_case_types)] | ||
$s.into_iter().map(|v| Selection { $($n: v.$n,)* }).collect::<Vec<_>>() | ||
}}; | ||
} | ||
|
||
// #[derive(Clone)] | ||
// #[allow(dead_code)] | ||
// struct User { | ||
// pub id: i32, | ||
// pub name: String, | ||
// pub email: String, | ||
// pub age: i32, | ||
// pub password: String, | ||
// } | ||
#[cfg(test)] | ||
mod tests { | ||
use specta::{ts::inline, Type}; | ||
|
||
// #[test] | ||
// fn test_selection_macros() { | ||
// let user = User { | ||
// id: 1, | ||
// name: "Monty Beaumont".into(), | ||
// email: "[email protected]".into(), | ||
// age: 7, | ||
// password: "password123".into(), | ||
// }; | ||
fn ts_export_ref<T: Type>(_t: &T) -> String { | ||
inline::<T>(&Default::default()).unwrap() | ||
} | ||
|
||
// let s1 = selection!(user.clone(), { name, age }); | ||
// assert_eq!(s1.name, "Monty Beaumont".to_string()); | ||
// assert_eq!(s1.age, 7); | ||
// assert_eq!(ts_export_ref(&s1), "{ name: string; age: number }"); | ||
#[derive(Clone)] | ||
#[allow(dead_code)] | ||
struct User { | ||
pub id: i32, | ||
pub name: String, | ||
pub email: String, | ||
pub age: i32, | ||
pub password: String, | ||
} | ||
|
||
// let users = vec![user; 3]; | ||
// let s2 = selection!(users, [{ name, age }]); | ||
// assert_eq!(s2[0].name, "Monty Beaumont".to_string()); | ||
// assert_eq!(s2[0].age, 7); | ||
// assert_eq!(ts_export_ref(&s2), "{ name: string; age: number }[]"); | ||
// } | ||
// } | ||
#[test] | ||
fn test_selection_macros() { | ||
let user = User { | ||
id: 1, | ||
name: "Monty Beaumont".into(), | ||
email: "[email protected]".into(), | ||
age: 7, | ||
password: "password123".into(), | ||
}; | ||
|
||
let s1 = selection!(user.clone(), { name, age }); | ||
assert_eq!(s1.name, "Monty Beaumont".to_string()); | ||
assert_eq!(s1.age, 7); | ||
assert_eq!(ts_export_ref(&s1), "{ name: string; age: number }"); | ||
|
||
let users = vec![user; 3]; | ||
let s2 = selection!(users, [{ name, age }]); | ||
assert_eq!(s2[0].name, "Monty Beaumont".to_string()); | ||
assert_eq!(s2[0].age, 7); | ||
assert_eq!(ts_export_ref(&s2), "{ name: string; age: number }[]"); | ||
} | ||
} |
60a4fa9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
rspc-docs – ./examples
rspc-docs-git-main-otbeaumont.vercel.app
rspc-docs-kappa.vercel.app
rspc-docs-otbeaumont.vercel.app