iota_rest_kv/
extractors.rs1use core::str;
8
9use axum::{
10 extract::{FromRequestParts, Path},
11 http::request::Parts,
12};
13use iota_storage::http_key_value_store::{ItemType, Key};
14use serde::Deserialize;
15
16use crate::errors::ApiError;
17
18#[derive(Deserialize, Debug)]
20struct RequestParams {
21 item_type: ItemType,
23 key: String,
26}
27
28pub struct ExtractPath(pub Key);
34
35impl<S> FromRequestParts<S> for ExtractPath
36where
37 S: Send + Sync,
38{
39 type Rejection = ApiError;
40
41 async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
42 match Path::<RequestParams>::from_request_parts(parts, state).await {
43 Ok(value) => {
44 let key = Key::new(value.item_type.to_string().as_str(), value.key.as_str())
46 .map_err(|err| ApiError::BadRequest(format!("invalid input: {err}")))?;
47 Ok(ExtractPath(key))
48 }
49 Err(e) => Err(ApiError::BadRequest(format!(
50 "invalid path parameter provided: {e}",
51 ))),
52 }
53 }
54}