#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]
pub mod generated;
mod route;
use serde::Serialize;
use std::collections::HashMap;
pub use route::Method;
#[cfg(feature = "guest")]
pub use generated::Handlers;
pub use generated::{deserialize, serialize, Request, Response};
use std::str::FromStr;
impl Request {
pub fn path_segments(&self) -> Vec<&str> {
self.path.split('/').skip(1).collect::<Vec<_>>()
}
pub fn method(&self) -> Method {
Method::from_str(&self.method).unwrap()
}
}
impl Response {
pub fn json<T>(payload: T, status_code: u32, status: &str) -> Response
where
T: Serialize,
{
Response {
body: serde_json::to_string(&payload).unwrap().into_bytes(),
header: HashMap::new(),
status: status.to_string(),
status_code,
}
}
pub fn not_found() -> Response {
Response {
status: "Not Found".to_string(),
status_code: 404,
..Default::default()
}
}
pub fn ok() -> Response {
Response {
status: "OK".to_string(),
status_code: 200,
..Default::default()
}
}
pub fn internal_server_error(msg: &str) -> Response {
Response {
status: "Internal Server Error".to_string(),
status_code: 500,
body: msg.to_string().as_bytes().into(),
..Default::default()
}
}
pub fn bad_request() -> Response {
Response {
status: "Bad Request".to_string(),
status_code: 400,
..Default::default()
}
}
}
#[cfg(test)]
#[cfg(feature = "guest")]
mod test {
extern crate wapc_guest;
use crate::{Handlers, Request, Response};
use wapc_guest::HandlerResult;
#[test]
fn it_works() {
Handlers::register_handle_request(hr);
assert!(true);
}
fn hr(_req: Request) -> HandlerResult<Response> {
Ok(Response::ok())
}
}