aboutsummaryrefslogtreecommitdiffstats
path: root/crates/api-core/src/version.rs
diff options
context:
space:
mode:
authorrtkay123 <dev@kanjala.com>2026-04-04 10:51:18 +0200
committerrtkay123 <dev@kanjala.com>2026-04-04 10:51:18 +0200
commit19c25138f88acf19c9a959a58de4f58e54026ebc (patch)
treebd854f20c539770a92fb451503b4c6d132c110a6 /crates/api-core/src/version.rs
parent41d90f42c37df06dabfd717d19f3dc72b5ba2d11 (diff)
downloadsellershut-19c25138f88acf19c9a959a58de4f58e54026ebc.tar.bz2
sellershut-19c25138f88acf19c9a959a58de4f58e54026ebc.zip
feat: connect to db
Diffstat (limited to 'crates/api-core/src/version.rs')
-rw-r--r--crates/api-core/src/version.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/crates/api-core/src/version.rs b/crates/api-core/src/version.rs
new file mode 100644
index 0000000..5f84f3e
--- /dev/null
+++ b/crates/api-core/src/version.rs
@@ -0,0 +1,96 @@
+#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
+#[cfg_attr(
+ feature = "utoipa",
+ derive(utoipa::ToSchema, serde::Deserialize, serde::Serialize),
+ schema(example = "v0"),
+ serde(rename_all = "camelCase")
+)]
+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()),
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use axum::{
+ Router,
+ body::Body,
+ http::{Request, StatusCode},
+ routing::get,
+ };
+ use tower::ServiceExt;
+
+ async fn handler(version: Version) -> &'static str {
+ match version {
+ Version::V0 => "ok",
+ }
+ }
+
+ async fn check(endpoint: &str, expected: StatusCode) {
+ let app = app();
+ let response = app
+ .oneshot(
+ Request::builder()
+ .uri(endpoint)
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(expected, response.status());
+ }
+
+ fn app() -> Router {
+ Router::new().route("/{apiVersion}/test", get(handler))
+ }
+
+ #[tokio::test]
+ async fn valid_version_v0() {
+ check("/v0/test", StatusCode::OK).await
+ }
+
+ #[tokio::test]
+ async fn unknown_version() {
+ check("/v1/test", StatusCode::NOT_FOUND).await
+ }
+
+ #[tokio::test]
+ async fn missing_version_param() {
+ check("/test", StatusCode::NOT_FOUND).await
+ }
+}