Skip to main content

app_store_server_library/models/
bullet_point.rs

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