|
| 1 | +use color_eyre::{ |
| 2 | + eyre::{bail, eyre}, |
| 3 | + Report, |
| 4 | +}; |
1 | 5 | use serde::{Deserialize, Serialize}; |
2 | 6 |
|
3 | 7 | use std::collections::HashMap; |
4 | 8 | use time::{format_description::FormatItem, macros::format_description, OffsetDateTime}; |
5 | 9 |
|
6 | | -use thiserror::Error; |
7 | | - |
8 | 10 | pub const DATETIME_FMT: &[FormatItem] = format_description!("[year]-[month]-[day] [hour]:[minute]"); |
9 | 11 |
|
10 | | -#[allow(clippy::large_enum_variant)] |
11 | | -#[derive(Debug, Error)] |
12 | | -pub enum Error { |
13 | | - #[error("could not build {object_name:?}, missing field {field_name:?}")] |
14 | | - MissingFieldOnBuilderError { |
15 | | - object_name: String, |
16 | | - field_name: String, |
17 | | - }, |
18 | | - |
19 | | - #[error("CreateMessage should contain at least one ContentSettings entry")] |
20 | | - EmptyContentSettingsError, |
21 | | -} |
22 | | - |
23 | 12 | pub type MultiLanguageContent = HashMap<String, String>; |
24 | 13 |
|
25 | 14 | #[derive(Serialize, Deserialize)] |
@@ -123,13 +112,12 @@ impl ContentSettingsBuilder { |
123 | 112 | self |
124 | 113 | } |
125 | 114 |
|
126 | | - pub fn build(self) -> Result<ContentSettings, Error> { |
| 115 | + pub fn build(self) -> Result<ContentSettings, Report> { |
| 116 | + let content = self.content.ok_or_else(|| eyre!("missing field content"))?; |
| 117 | + |
127 | 118 | Ok(ContentSettings { |
128 | 119 | send_date: self.send_date, |
129 | | - content: self.content.ok_or(Error::MissingFieldOnBuilderError { |
130 | | - object_name: "ContentSettings".to_string(), |
131 | | - field_name: "content".to_string(), |
132 | | - })?, |
| 120 | + content, |
133 | 121 | ignore_user_timezones: self.ignore_user_timezones, |
134 | 122 | timezone: self.timezone, |
135 | 123 | campaign: self.campaign, |
@@ -167,19 +155,19 @@ impl CreateMessageBuilder { |
167 | 155 | self |
168 | 156 | } |
169 | 157 |
|
170 | | - pub fn build(self) -> Result<CreateMessage, Error> { |
| 158 | + pub fn build(self) -> Result<CreateMessage, Report> { |
171 | 159 | if self.notifications.is_empty() { |
172 | | - return Err(Error::EmptyContentSettingsError); |
| 160 | + bail!("empty content settings"); |
173 | 161 | } |
| 162 | + |
| 163 | + let auth = self.auth.ok_or_else(|| eyre!("missing field auth"))?; |
| 164 | + let application = self |
| 165 | + .application |
| 166 | + .ok_or_else(|| eyre!("missing field application"))?; |
| 167 | + |
174 | 168 | Ok(CreateMessage { |
175 | | - auth: self.auth.ok_or(Error::MissingFieldOnBuilderError { |
176 | | - object_name: "CreateMessage".to_string(), |
177 | | - field_name: "auth".to_string(), |
178 | | - })?, |
179 | | - application: self.application.ok_or(Error::MissingFieldOnBuilderError { |
180 | | - object_name: "CreateMessage".to_string(), |
181 | | - field_name: "application".to_string(), |
182 | | - })?, |
| 169 | + auth, |
| 170 | + application, |
183 | 171 | notifications: self.notifications, |
184 | 172 | }) |
185 | 173 | } |
|
0 commit comments