twilight_http/request/template/
update_template.rs

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