1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use serde::{Deserialize, Serialize};
use uuid::Uuid;
const MAXIMUM_TEXT_LENGTH: usize = 66;
const MAXIMUM_ALT_TEXT_LENGTH: usize = 150;
/// The text and its bullet-point image to include in a retention message's bulleted list.
///
/// [BulletPoint](https://developer.apple.com/documentation/retentionmessaging/bulletpoint)
#[derive(Debug, Clone, Deserialize, Serialize, Hash, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct BulletPoint {
/// The text of the individual bullet point.
///
/// [text](https://developer.apple.com/documentation/retentionmessaging/text)
pub text: String,
/// The identifier of the image to use as the bullet point.
///
/// [imageIdentifier](https://developer.apple.com/documentation/retentionmessaging/imageidentifier)
pub image_identifier: Uuid,
/// The alternative text you provide for the corresponding image of the bullet point.
///
/// [altText](https://developer.apple.com/documentation/retentionmessaging/alttext)
pub alt_text: String,
}
impl BulletPoint {
/// Creates a new `BulletPoint`, validating the text lengths.
///
/// # Errors
///
/// Returns `BulletPointValidationError::TextTooLong` if `text` exceeds 66 characters.
/// Returns `BulletPointValidationError::AltTextTooLong` if `alt_text` exceeds 150 characters.
pub fn new(text: String, image_identifier: Uuid, alt_text: String) -> Result<Self, BulletPointValidationError> {
if text.chars().count() > MAXIMUM_TEXT_LENGTH {
return Err(BulletPointValidationError::TextTooLong);
}
if alt_text.chars().count() > MAXIMUM_ALT_TEXT_LENGTH {
return Err(BulletPointValidationError::AltTextTooLong);
}
Ok(Self {
text,
image_identifier,
alt_text,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BulletPointValidationError {
TextTooLong,
AltTextTooLong,
}
impl std::fmt::Display for BulletPointValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BulletPointValidationError::TextTooLong => write!(
f,
"Text exceeds maximum length of {} characters",
MAXIMUM_TEXT_LENGTH
),
BulletPointValidationError::AltTextTooLong => write!(
f,
"Alt text exceeds maximum length of {} characters",
MAXIMUM_ALT_TEXT_LENGTH
),
}
}
}
impl std::error::Error for BulletPointValidationError {}