Skip to main content

twilight_http/request/template/
update_template.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use serde::Serialize;
10use std::future::IntoFuture;
11use twilight_model::{
12    guild::template::Template,
13    id::{Id, marker::GuildMarker},
14};
15use twilight_validate::request::{
16    ValidationError, template_description as validate_template_description,
17    template_name as validate_template_name,
18};
19
20#[derive(Serialize)]
21struct UpdateTemplateFields<'a> {
22    name: Option<&'a str>,
23    description: Option<&'a str>,
24}
25
26/// Update the template's metadata, by ID and code.
27#[must_use = "requests must be configured and executed"]
28pub struct UpdateTemplate<'a> {
29    fields: Result<UpdateTemplateFields<'a>, ValidationError>,
30    guild_id: Id<GuildMarker>,
31    http: &'a Client,
32    template_code: &'a str,
33}
34
35impl<'a> UpdateTemplate<'a> {
36    pub(crate) const fn new(
37        http: &'a Client,
38        guild_id: Id<GuildMarker>,
39        template_code: &'a str,
40    ) -> Self {
41        Self {
42            fields: Ok(UpdateTemplateFields {
43                name: None,
44                description: None,
45            }),
46            guild_id,
47            http,
48            template_code,
49        }
50    }
51
52    /// Set the description.
53    ///
54    /// This must be at most 120 characters in length.
55    ///
56    /// # Errors
57    ///
58    /// Returns an error of type [`TemplateDescription`] if the name length is
59    /// too short or too long.
60    ///
61    /// [`TemplateDescription`]: twilight_validate::request::ValidationErrorType::TemplateDescription
62    pub fn description(mut self, description: &'a str) -> Self {
63        self.fields = self.fields.and_then(|mut fields| {
64            validate_template_description(description)?;
65            fields.description.replace(description);
66
67            Ok(fields)
68        });
69
70        self
71    }
72
73    /// Set the name.
74    ///
75    /// This must be at least 1, and at most 100 characters in length.
76    ///
77    /// # Errors
78    ///
79    /// Returns an error of type [`TemplateName`] if the name length is too
80    /// short or too long.
81    ///
82    /// [`TemplateName`]: twilight_validate::request::ValidationErrorType::TemplateName
83    pub fn name(mut self, name: &'a str) -> Self {
84        self.fields = self.fields.and_then(|mut fields| {
85            validate_template_name(name)?;
86            fields.name.replace(name);
87
88            Ok(fields)
89        });
90
91        self
92    }
93}
94
95#[cfg(not(target_os = "wasi"))]
96impl IntoFuture for UpdateTemplate<'_> {
97    type Output = Result<Response<Template>, Error>;
98
99    type IntoFuture = ResponseFuture<Template>;
100
101    fn into_future(self) -> Self::IntoFuture {
102        let http = self.http;
103
104        match self.try_into_request() {
105            Ok(request) => http.request(request),
106            Err(source) => ResponseFuture::error(source),
107        }
108    }
109}
110
111impl TryIntoRequest for UpdateTemplate<'_> {
112    fn try_into_request(self) -> Result<Request, Error> {
113        let fields = self.fields.map_err(Error::validation)?;
114
115        Request::builder(&Route::UpdateTemplate {
116            guild_id: self.guild_id.get(),
117            template_code: self.template_code,
118        })
119        .json(&fields)
120        .build()
121    }
122}