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
use crate::error::{ApiError, ApiErrorKind};
use crate::extractors::routers::RouterType;
use actix_web::{dev::Payload, FromRequest, HttpRequest};
use futures::future;

/// Extracts and validates the `router_type` and `app_id` path arguments
pub struct RegistrationPathArgs {
    pub router_type: RouterType,
    pub app_id: String,
}

impl FromRequest for RegistrationPathArgs {
    type Error = ApiError;
    type Future = future::Ready<Result<Self, Self::Error>>;

    fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
        let match_info = req.match_info();
        let router_type = match match_info
            .get("router_type")
            .expect("{router_type} must be part of the path")
            .parse::<RouterType>()
        {
            Ok(router_type) => router_type,
            Err(_) => return future::err(ApiErrorKind::InvalidRouterType.into()),
        };
        let app_id = match_info
            .get("app_id")
            .expect("{app_id} must be part of the path")
            .to_string();

        future::ok(Self {
            router_type,
            app_id,
        })
    }
}