Skip to content

Commit

Permalink
fix selection macro
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Mar 5, 2023
1 parent b14bb7c commit 60a4fa9
Showing 1 changed file with 76 additions and 66 deletions.
142 changes: 76 additions & 66 deletions src/selection.rs
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 }[]");
}
}

1 comment on commit 60a4fa9

@vercel
Copy link

@vercel vercel bot commented on 60a4fa9 Mar 17, 2023

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

Please sign in to comment.