WIP for laptop

This commit is contained in:
Julia Lange 2025-08-29 16:54:32 -07:00
parent 55b583b6e6
commit 3b98eb4a95
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
5 changed files with 93 additions and 14 deletions

View file

@ -52,8 +52,11 @@ pub fn response(code: StatusCode, message: &str) -> Response {
error(code, "", message)
}
pub struct QueryInput {
pub struct QueryInput<S = ()>
where S: Clone + Send + Sync + 'static,
{
pub parameters: HashMap<String, String>,
pub state: S,
}
impl<S> FromRequestParts<S> for QueryInput
where
@ -61,23 +64,26 @@ where
{
type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, _state: &S)
async fn from_request_parts(parts: &mut Parts, state: &S)
-> Result<Self, Self::Rejection> {
let query_params: Result<Query<HashMap<String, String>>, QueryRejection> = Query::try_from_uri(&parts.uri);
match query_params {
Ok(p) => Ok(QueryInput { parameters: p.0 }),
Ok(p) => Ok(QueryInput { parameters: p.0, state }),
Err(e) => Err(error(StatusCode::BAD_REQUEST, "Bad Parameters", &e.body_text())),
}
}
}
#[derive(Debug)]
pub struct ProcedureInput<J> {
pub struct ProcedureInput<J, S = ()>
where S: Clone + Send + Sync + 'static,
{
pub parameters: HashMap<String, String>,
pub input: J,
pub state: S,
}
impl<J, S> FromRequest<S> for ProcedureInput<J>
impl<J, S> FromRequest<S> for ProcedureInput<J, S>
where
J: for<'de> serde::Deserialize<'de> + Send + 'static,
Bytes: FromRequest<S>,
@ -95,7 +101,7 @@ where
.map(|Json(v)| v)
.map_err(|e| error(StatusCode::BAD_REQUEST, "Bad Parameters", &e.body_text()))?;
Ok(ProcedureInput { parameters, input })
Ok(ProcedureInput { parameters, input, state })
}
}
@ -125,14 +131,14 @@ where
}
impl XrpcEndpoint {
pub fn new_query<Q>(nsid: Nsid, query: Q) -> Self
pub fn new_query<Q, S>(nsid: Nsid, query: Q) -> Self
where
Q: XrpcHandler<QueryInput> + Clone
{
XrpcEndpoint {
path: Path::Nsid(nsid),
resolver: get(async move | mut parts: Parts | -> Response {
match QueryInput::from_request_parts(&mut parts, &()).await {
resolver: get(async move | mut parts: Parts, state: &S | -> Response {
match QueryInput<S>::from_request_parts(&mut parts, state).await {
Ok(qi) => query.call(qi).await,
Err(e) => e
}
@ -140,15 +146,15 @@ impl XrpcEndpoint {
}
}
pub fn new_procedure<P, J>(nsid: Nsid, procedure: P) -> Self
pub fn new_procedure<P, J, S>(nsid: Nsid, procedure: P) -> Self
where
P: XrpcHandler<ProcedureInput<J>> + Clone,
P: XrpcHandler<ProcedureInput<J, S>> + Clone,
J: for<'de> serde::Deserialize<'de> + Send + 'static,
{
XrpcEndpoint {
path: Path::Nsid(nsid),
resolver: post(async move | req: Request | -> Response {
match ProcedureInput::<J>::from_request(req, &()).await {
resolver: post(async move | req: Request, state: &S | -> Response {
match ProcedureInput::<J, S>::from_request(req, &state).await {
Ok(pi) => procedure.call(pi).await,
Err(e) => e
}