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