aboutsummaryrefslogtreecommitdiffstats
path: root/crates/api-base/src/version.rs
blob: 0652c6e3d50c14d090642626e5f7ecb41353b06e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#[derive(Debug)]
#[cfg_attr(
    feature = "utoipa",
    derive(utoipa::ToSchema, serde::Deserialize, serde::Serialize),
    schema(example = "v0"),
    serde(rename_all = "lowercase")
)]
pub enum Version {
    V0,
}

#[cfg(feature = "axum")]
mod request {
    use super::*;
    use axum::RequestPartsExt;
    use axum::extract::{FromRequestParts, Path};
    use axum::http::StatusCode;
    use axum::http::request::Parts;
    use axum::response::{IntoResponse, Response};
    use std::collections::HashMap;

    impl<S> FromRequestParts<S> for Version
    where
        S: Send + Sync,
    {
        type Rejection = Response;

        async fn from_request_parts(
            parts: &mut Parts,
            _state: &S,
        ) -> Result<Self, Self::Rejection> {
            let params: Path<HashMap<String, String>> =
                parts.extract().await.map_err(IntoResponse::into_response)?;

            let version = params
                .get("apiVersion")
                .ok_or_else(|| (StatusCode::NOT_FOUND, "version param missing").into_response())?;

            match version.as_str() {
                "v0" => Ok(Version::V0),
                _ => Err((StatusCode::NOT_FOUND, "unknown version").into_response()),
            }
        }
    }
}