Skip to content

Commit

Permalink
feat: removed Option<T> weirdness on header setting
Browse files Browse the repository at this point in the history
BREAKING CHANGE: header setting functions now take either a usual state
argument or no state (no more `Option<T>`s)
  • Loading branch information
arctic-hen7 committed Jul 13, 2022
1 parent 11ed444 commit 869000b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/core/set_headers/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWi
// to be fully qualified, or you have to import it with a server-only
// target-gate
#[perseus::set_headers]
pub fn set_headers(state: Option<PageState>) -> perseus::http::header::HeaderMap {
pub fn set_headers(state: PageState) -> perseus::http::header::HeaderMap {
// These imports are only available on the server-side, which this function is
// automatically gated to
use perseus::http::header::{HeaderMap, HeaderName};

let mut map = HeaderMap::new();
map.insert(
HeaderName::from_lowercase(b"x-greeting").unwrap(),
state.unwrap().greeting.parse().unwrap(),
state.greeting.parse().unwrap(),
);
map
}
51 changes: 36 additions & 15 deletions packages/perseus-macro/src/state_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,44 @@ pub fn state_fn_impl(input: StateFn, fn_type: StateFnType) -> TokenStream {
}
},
// Always synchronous
StateFnType::SetHeaders => quote! {
// We create a normal version of the function and one to appease the handlers in Wasm (which expect functions that take no arguments, etc.)
#[cfg(target_arch = "wasm32")]
#vis fn #name() {}
#[cfg(not(target_arch = "wasm32"))]
#vis fn #name(props: ::std::option::Option<::std::string::String>) -> ::perseus::http::header::HeaderMap {
// The user's function
// We can assume the return type to be `HeaderMap`
#(#attrs)*
fn #name #generics(#args) -> #return_type {
#block
StateFnType::SetHeaders => {
if args.len() == 1 {
quote! {
// We create a normal version of the function and one to appease the handlers in Wasm (which expect functions that take no arguments, etc.)
#[cfg(target_arch = "wasm32")]
#vis fn #name() {}
#[cfg(not(target_arch = "wasm32"))]
#vis fn #name(props: ::std::option::Option<::std::string::String>) -> ::perseus::http::header::HeaderMap {
// The user's function
// We can assume the return type to be `HeaderMap`
#(#attrs)*
fn #name #generics(#args) -> #return_type {
#block
}
// Deserialize the props and then call the user's function
// Their function is taking state, so this must be defined
let props_de = ::serde_json::from_str(&props.unwrap()).unwrap();
#name(props_de)
}
}
} else {
quote! {
// We create a normal version of the function and one to appease the handlers in Wasm (which expect functions that take no arguments, etc.)
#[cfg(target_arch = "wasm32")]
#vis fn #name() {}
#[cfg(not(target_arch = "wasm32"))]
#vis fn #name(props: ::std::option::Option<::std::string::String>) -> ::perseus::http::header::HeaderMap {
// The user's function
// We can assume the return type to be `HeaderMap`
#(#attrs)*
fn #name #generics() -> #return_type {
#block
}
#name()
}
}
// Deserialize the props and then call the user's function
let props_de = props.map(|val| ::serde_json::from_str(&val).unwrap());
#name(props_de)
}
},
}
// Always synchronous
StateFnType::AmalgamateStates => quote! {
// We create a normal version of the function and one to appease the handlers in Wasm (which expect functions that take no arguments, etc.)
Expand Down

0 comments on commit 869000b

Please sign in to comment.