freedom_api/api/post/
sat_config.rs1use serde::Serialize;
2
3use crate::{api::Api, error::Error};
4
5use super::{Post, UrlResult};
6
7#[derive(Debug, Clone, PartialEq, Serialize)]
8#[serde(rename_all = "camelCase")]
9struct SatelliteConfigurationInner {
10 name: String,
11 doppler: Option<bool>,
12 notes: Option<String>,
13 band_details: Vec<String>,
14}
15
16#[derive(Debug, Clone, PartialEq)]
17pub struct SatelliteConfiguration {
18 name: String,
19 doppler: Option<bool>,
20 notes: Option<String>,
21 band_details: Vec<UrlResult>,
22}
23
24pub struct NoName;
25
26pub struct SatelliteConfigurationBuilder<'a, C, S> {
27 pub(crate) client: &'a C,
28 state: S,
29}
30
31pub(crate) fn new<C>(client: &C) -> SatelliteConfigurationBuilder<'_, C, NoName> {
32 SatelliteConfigurationBuilder {
33 client,
34 state: NoName,
35 }
36}
37
38impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoName> {
39 pub fn name(self, name: impl Into<String>) -> SatelliteConfigurationBuilder<'a, C, NoBand> {
40 SatelliteConfigurationBuilder {
41 client: self.client,
42 state: NoBand { name: name.into() },
43 }
44 }
45}
46
47pub struct NoBand {
48 name: String,
49}
50
51impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoBand> {
52 pub fn band_urls(
53 self,
54 urls: impl IntoIterator<Item = String>,
55 ) -> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration> {
56 let band_details: Vec<_> = urls.into_iter().map(UrlResult::Checked).collect();
57 self.band_results(band_details)
58 }
59
60 fn band_results(
61 self,
62 ids: impl IntoIterator<Item = UrlResult>,
63 ) -> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration> {
64 let state = SatelliteConfiguration {
65 name: self.state.name,
66 doppler: None,
67 notes: None,
68 band_details: ids.into_iter().collect(),
69 };
70
71 SatelliteConfigurationBuilder {
72 client: self.client,
73 state,
74 }
75 }
76}
77
78impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoBand>
79where
80 C: Api,
81{
82 pub fn band_ids(
83 self,
84 ids: impl IntoIterator<Item = i32>,
85 ) -> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration> {
86 let client = self.client;
87 let results = ids
88 .into_iter()
89 .map(|id| UrlResult::Unchecked(client.path_to_url(format!("satellite_bands/{}", id))))
90 .collect::<Vec<_>>();
91 self.band_results(results)
92 }
93}
94
95impl<C> SatelliteConfigurationBuilder<'_, C, SatelliteConfiguration> {
96 pub fn doppler(mut self, doppler: bool) -> Self {
97 self.state.doppler = Some(doppler);
98 self
99 }
100
101 pub fn notes(mut self, notes: impl Into<String>) -> Self {
102 self.state.notes = Some(notes.into());
103 self
104 }
105}
106
107impl<C> Post for SatelliteConfigurationBuilder<'_, C, SatelliteConfiguration>
108where
109 C: Api,
110{
111 type Response = freedom_models::satellite_configuration::SatelliteConfiguration;
112
113 async fn send(self) -> Result<Self::Response, Error> {
114 let client = self.client;
115
116 let mut band_details = Vec::new();
117 for result in self.state.band_details {
118 let band = result.try_convert()?;
119 band_details.push(band);
120 }
121
122 let dto = SatelliteConfigurationInner {
123 name: self.state.name,
124 doppler: self.state.doppler,
125 notes: self.state.notes,
126 band_details,
127 };
128
129 let url = client.path_to_url("satellite_configurations")?;
130 client.post_json_map(url, dto).await
131 }
132}