cscart_rs/service/
config.rs

1use crate::{service::state::*, Resource, ServiceAuth};
2use std::marker::PhantomData;
3
4/*
5Share logic and utils for constructing service config
6Each actual service aliases this Service struct and optionally implements the appropriate methods
7*/
8pub struct ServiceConfig<T: ServiceState = Unauthenticated> {
9    pub(crate) _marker: PhantomData<T>,
10    pub(crate) auth: Option<ServiceAuth>,
11    pub(crate) resource: Option<Resource>,
12    pub(crate) host: Option<String>,
13}
14
15impl ServiceConfig<Unauthenticated> {
16    pub fn with_resource(resource: Resource) -> ServiceConfig<Unauthenticated> {
17        Self {
18            _marker: PhantomData,
19            auth: None,
20            resource: Some(resource),
21            host: None,
22        }
23    }
24
25    pub fn host(self, host: &str) -> ServiceConfig<OnlyHost> {
26        ServiceConfig::<OnlyHost> {
27            _marker: PhantomData,
28            host: Some(host.into()),
29            auth: None,
30            resource: self.resource,
31        }
32    }
33
34    pub fn auth(self, auth: ServiceAuth) -> ServiceConfig<OnlyAuth> {
35        ServiceConfig::<OnlyAuth> {
36            _marker: PhantomData,
37            host: None,
38            auth: Some(auth),
39            resource: self.resource,
40        }
41    }
42}
43
44impl ServiceConfig<OnlyHost> {
45    pub fn auth(self, auth: ServiceAuth) -> ServiceConfig<Authenticated> {
46        ServiceConfig::<Authenticated> {
47            _marker: PhantomData,
48            host: self.host,
49            auth: Some(auth),
50            resource: self.resource,
51        }
52    }
53}
54
55impl ServiceConfig<OnlyAuth> {
56    pub fn host(self, host: &str) -> ServiceConfig<Authenticated> {
57        ServiceConfig::<Authenticated> {
58            _marker: PhantomData,
59            host: Some(host.into()),
60            auth: self.auth,
61            resource: self.resource,
62        }
63    }
64}
65
66/*
67Blanket impl of getters for auth info. Any service that aliases the Service struct
68also gets these methods
69*/
70impl ConfiguredService for ServiceConfig<Authenticated> {
71    fn auth(&self) -> ServiceAuth {
72        self.auth.as_ref().unwrap().clone()
73    }
74
75    fn host(&self) -> String {
76        self.host.as_ref().unwrap().clone()
77    }
78}