5 releases

new 0.1.0 Dec 7, 2024
0.0.4 Dec 2, 2024
0.0.3 Nov 29, 2024
0.0.2 Nov 29, 2024
0.0.1 Nov 27, 2024

#103 in WebSocket

Download history 219/week @ 2024-11-23 274/week @ 2024-11-30

494 downloads per month

MIT license

31KB
751 lines

NextDoor

nextdoor = "0.0.*"
serde = { version = "1.0.215", features = ["derive"] }
tokio = { version = "1.41.1", features = ["full"] }
use nextdoor::{
    extract::{Binary, Close, Json, Ping, Pong, State},
    request::{Frames, Request},
    response::{IntoResponse, Response, Status},
    NextDoor,
};
use serde::{Deserialize, Serialize};
// with state
// use std::sync::Arc;

#[tokio::main]
async fn main() {
    let mut router = NextDoor::new();
    // with_state
    // let app_state = AppState {
    //     id: "test id".to_string(),
    //     secret: "test secret".to_string(),
    // };
    // let mut router = NextDoor::with_state(Arc::new(app_state));

    router
        .text(param_empty_return_unit)
        .text(param_string_return_unit)
        .text(param_json_return_unit)
        .text(param_string_return_str)
        .text(param_string_return_string)
        .text(param_string_return_option_some)
        .text(param_string_return_option_none)
        .text(param_string_return_ok)
        .text(param_string_return_err)
        .text(param_json_return_json)
        // .text(param_app_store_return_unit)
        // .text(param_json_app_store_return_unit);
        .binary(binary)
        .ping(ping)
        .pong(pong)
        .close(close);

    let req = Request::new(Frames::Text, Bytes::from("test text"));
    let response = router.handler(req).await;

    // Features = "client"
    // nextdoor::connect(router, "url").run().await.unwrap();
}


#[derive(Deserialize, Serialize)]
struct User {
    name: String,
    id: String,
}

struct AppState {
    id: String,
    secret: String,
}

async fn param_empty_return_unit() {}
async fn param_string_return_unit(param: String) {}
async fn param_json_return_unit(Json(param): Json<User>) {}

async fn param_string_return_str(param: String) -> String {
    "send to server".to_string()
}

async fn param_string_return_string(param: String) -> &'static str {
    "send to server"
}

async fn param_string_return_option_some(param: String) -> Option<String> {
    Some("send to server".to_string())
}

async fn param_string_return_option_none(param: String) -> Option<String> {
    None
}

async fn param_string_return_ok(param: String) -> Result<String, Error> {
    Ok("send to server".to_string())
}

async fn param_string_return_err(param: String) -> Result<String, Error> {
    Err(Error::JsonError)
}

async fn param_json_return_result(Json(param): Json<User>) -> Result<String, Error> {
    Ok("send to server".to_string())
}

async fn param_json_return_json(Json(param): Json<User>) -> Result<Json<User>, Error> {
    let user = User {
        name: "test name".to_string(),
        id: "test id".to_string(),
    };
    Ok(Json::new(user))
}

async fn param_app_store_return_unit(State(app_state): State<Arc<AppState>>) {}
async fn param_json_app_store_return_unit(
    Json(json): Json<User>,
    State(app_state): State<Arc<AppState>>,
) {
}

async fn binary(Binary(param): Binary) {}
async fn ping(Ping(param): Ping) {}
async fn pong(Pong(param): Pong) {}
async fn close(Close(param): Close) {}

enum Error {
    JsonError,
}

impl IntoResponse for Error {
    fn into_response(self) -> Response {
        match self {
            Self::JsonError => Response::error(Status::JsonError, "Failed parse Json"),
        }
    }
}

Dependencies

~5–14MB
~157K SLoC