Skip to content

Commit

Permalink
feat(templates): ✨ added autoserde macro to improve ergonomics
Browse files Browse the repository at this point in the history
Further addresses #57.
  • Loading branch information
arctic-hen7 committed Nov 1, 2021
1 parent 810ae1b commit eb21299
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 89 deletions.
17 changes: 11 additions & 6 deletions examples/basic/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.set_headers_fn(set_headers)
}

pub async fn get_build_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Ok(serde_json::to_string(&IndexPageProps {
#[perseus::autoserde(build_state)]
pub async fn get_build_props(
_path: String,
_locale: String,
) -> RenderFnResultWithCause<IndexPageProps> {
Ok(IndexPageProps {
greeting: "Hello World!".to_string(),
})?) // This `?` declares the default, that the server is the cause of the error
})
}

pub fn set_headers(_props: Option<String>) -> HeaderMap {
#[perseus::autoserde(set_headers)]
pub fn set_headers(props: Option<IndexPageProps>) -> HeaderMap {
let mut map = HeaderMap::new();
map.insert(
HeaderName::from_lowercase(b"x-test").unwrap(),
"custom value".parse().unwrap(),
HeaderName::from_lowercase(b"x-greeting").unwrap(),
props.unwrap().greeting.parse().unwrap(),
);
map
}
10 changes: 7 additions & 3 deletions examples/i18n/src/templates/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.template(post_page)
}

pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
#[perseus::autoserde(build_state)]
pub async fn get_static_props(
path: String,
_locale: String,
) -> RenderFnResultWithCause<PostPageProps> {
// This is just an example
let title = urlencoding::decode(&path).unwrap();
let content = format!(
"This is a post entitled '{}'. Its original slug was '{}'.",
title, path
);

Ok(serde_json::to_string(&PostPageProps {
Ok(PostPageProps {
title: title.to_string(),
content,
})?) // This `?` declares the default, that the server is the cause of the error
}) // This `?` declares the default, that the server is the cause of the error
}

pub async fn get_static_paths() -> RenderFnResult<Vec<String>> {
Expand Down
26 changes: 17 additions & 9 deletions examples/showcase/src/templates/amalgamation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,43 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.template(amalgamation_page)
}

pub fn amalgamate_states(states: States) -> RenderFnResultWithCause<Option<String>> {
#[perseus::autoserde(amalgamate_states)]
pub fn amalgamate_states(
states: States,
) -> RenderFnResultWithCause<Option<AmalagamationPageProps>> {
// We know they'll both be defined
let build_state = serde_json::from_str::<AmalagamationPageProps>(&states.build_state.unwrap())?;
let req_state = serde_json::from_str::<AmalagamationPageProps>(&states.request_state.unwrap())?;

Ok(Some(serde_json::to_string(&AmalagamationPageProps {
Ok(Some(AmalagamationPageProps {
message: format!(
"Hello from the amalgamation! (Build says: '{}', server says: '{}'.)",
build_state.message, req_state.message
),
})?))
}))
}

pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Ok(serde_json::to_string(&AmalagamationPageProps {
#[perseus::autoserde(build_state)]
pub async fn get_build_state(
_path: String,
_locale: String,
) -> RenderFnResultWithCause<AmalagamationPageProps> {
Ok(AmalagamationPageProps {
message: "Hello from the build process!".to_string(),
})?)
})
}

#[perseus::autoserde(request_state)]
pub async fn get_request_state(
_path: String,
_locale: String,
_req: Request,
) -> RenderFnResultWithCause<String> {
) -> RenderFnResultWithCause<AmalagamationPageProps> {
// Err(perseus::GenericErrorWithCause {
// error: "this is a test error!".into(),
// cause: perseus::ErrorCause::Client(None)
// })
Ok(serde_json::to_string(&AmalagamationPageProps {
Ok(AmalagamationPageProps {
message: "Hello from the server!".to_string(),
})?)
})
}
1 change: 1 addition & 0 deletions examples/showcase/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.template(index_page)
}

#[perseus::autoserde(build_state)]
pub async fn get_static_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Ok(serde_json::to_string(&IndexPageProps {
greeting: "Hello World!".to_string(),
Expand Down
7 changes: 4 additions & 3 deletions examples/showcase/src/templates/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.template(ip_page)
}

#[perseus::autoserde(request_state)]
pub async fn get_request_state(
_path: String,
_locale: String,
req: Request,
) -> RenderFnResultWithCause<String> {
) -> RenderFnResultWithCause<IpPageProps> {
// Err(perseus::GenericErrorWithCause {
// error: "this is a test error!".into(),
// cause: perseus::ErrorCause::Client(None)
// })
Ok(serde_json::to_string(&IpPageProps {
Ok(IpPageProps {
// Gets the client's IP address
ip: format!(
"{:?}",
req.headers()
.get("X-Forwarded-For")
.unwrap_or(&perseus::http::HeaderValue::from_str("hidden from view!").unwrap())
),
})?)
})
}
10 changes: 7 additions & 3 deletions examples/showcase/src/templates/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.template(post_page)
}

pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
#[perseus::autoserde(build_state)]
pub async fn get_static_props(
path: String,
_locale: String,
) -> RenderFnResultWithCause<PostPageProps> {
// This path is illegal, and can't be rendered
if path == "post/tests" {
return Err(GenericErrorWithCause {
Expand All @@ -48,10 +52,10 @@ pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWi
title, path
);

Ok(serde_json::to_string(&PostPageProps {
Ok(PostPageProps {
title: title.to_string(),
content,
})?)
})
}

pub async fn get_static_paths() -> RenderFnResult<Vec<String>> {
Expand Down
10 changes: 7 additions & 3 deletions examples/showcase/src/templates/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.build_paths_fn(get_build_paths)
}

pub async fn get_build_state(path: String, _locale: String) -> RenderFnResultWithCause<String> {
#[perseus::autoserde(build_state)]
pub async fn get_build_state(
path: String,
_locale: String,
) -> RenderFnResultWithCause<TimePageProps> {
// This path is illegal, and can't be rendered
if path == "timeisr/tests" {
return Err(GenericErrorWithCause {
error: "illegal page".into(),
cause: ErrorCause::Client(Some(404)),
});
}
Ok(serde_json::to_string(&TimePageProps {
Ok(TimePageProps {
time: format!("{:?}", std::time::SystemTime::now()),
})?)
})
}

pub async fn get_build_paths() -> RenderFnResult<Vec<String>> {
Expand Down
10 changes: 7 additions & 3 deletions examples/showcase/src/templates/time_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.build_state_fn(get_build_state)
}

pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Ok(serde_json::to_string(&TimePageProps {
#[perseus::autoserde(build_state)]
pub async fn get_build_state(
_path: String,
_locale: String,
) -> RenderFnResultWithCause<TimePageProps> {
Ok(TimePageProps {
time: format!("{:?}", std::time::SystemTime::now()),
})?)
})
}
Loading

0 comments on commit eb21299

Please sign in to comment.