freedom_api/api/post/
sat_config.rs

1use reqwest::Response;
2use serde::Serialize;
3
4use crate::{api::Api, error::Error};
5
6#[derive(Debug, Clone, PartialEq, Serialize)]
7#[serde(rename_all = "camelCase")]
8pub struct SatelliteConfiguration {
9    name: String,
10    doppler: Option<bool>,
11    notes: Option<String>,
12    band_details: Vec<String>,
13}
14
15pub struct NoName;
16
17pub struct SatelliteConfigurationBuilder<'a, C, S> {
18    pub(crate) client: &'a C,
19    state: S,
20}
21
22pub(crate) fn new<C>(client: &C) -> SatelliteConfigurationBuilder<'_, C, NoName> {
23    SatelliteConfigurationBuilder {
24        client,
25        state: NoName,
26    }
27}
28
29impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoName> {
30    pub fn name(self, name: impl Into<String>) -> SatelliteConfigurationBuilder<'a, C, NoBand> {
31        SatelliteConfigurationBuilder {
32            client: self.client,
33            state: NoBand { name: name.into() },
34        }
35    }
36}
37
38pub struct NoBand {
39    name: String,
40}
41
42impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoBand> {
43    pub fn band_urls(
44        self,
45        urls: impl IntoIterator<Item = String>,
46    ) -> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration> {
47        let band_details: Vec<_> = urls.into_iter().collect();
48
49        let state = SatelliteConfiguration {
50            name: self.state.name,
51            doppler: None,
52            notes: None,
53            band_details,
54        };
55
56        SatelliteConfigurationBuilder {
57            client: self.client,
58            state,
59        }
60    }
61}
62
63impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoBand>
64where
65    C: Api,
66{
67    pub fn band_ids(
68        self,
69        ids: impl IntoIterator<Item = i32>,
70    ) -> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration> {
71        let client = self.client;
72        let bands = ids.into_iter().map(|id| {
73            client
74                .path_to_url(format!("satellite_bands/{}", id))
75                .to_string()
76        });
77
78        self.band_urls(bands)
79    }
80}
81
82impl<C> SatelliteConfigurationBuilder<'_, C, SatelliteConfiguration> {
83    pub fn doppler(mut self, doppler: bool) -> Self {
84        self.state.doppler = Some(doppler);
85        self
86    }
87
88    pub fn notes(mut self, notes: impl Into<String>) -> Self {
89        self.state.notes = Some(notes.into());
90        self
91    }
92}
93
94impl<C> SatelliteConfigurationBuilder<'_, C, SatelliteConfiguration>
95where
96    C: Api,
97{
98    pub async fn send(self) -> Result<Response, Error> {
99        let client = self.client;
100
101        let url = client.path_to_url("satellite_configurations");
102        client.post(url, self.state).await
103    }
104}