lacoctelera/domain/
tag.rs

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2024 Felipe Torres González
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Data objects related to tags.

use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use validator::{Validate, ValidationError};

// Regex to validate an Uuid.
static RE_TAG: Lazy<Regex> = Lazy::new(|| Regex::new(r"[a-z_]{2,}$").unwrap());

/// Tag data object.
///
/// # Description
///
/// Tags allow a fine-grain organisation of the recipes in the DB. They allow to stablish relationships between
/// different recipes, and they can be used to offer recipe suggestions to clients.
///
/// Tags are identified by a single word identifier with a minimum length of 2 and maximum of 20 characters.
/// Tags are not case sensitive, and capital letters will be converted to lower case when using any of the provided
/// methods.
///
/// The only special character that is allowed to identify a tag is: `_`.
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate, PartialEq)]
pub struct Tag {
    #[validate(custom(function = "validate_identifier"), length(min = 2, max = 20))]
    pub identifier: String,
}

impl Tag {
    pub fn new(tagname: &str) -> Result<Self, ValidationError> {
        let tag = Tag {
            identifier: tagname.to_ascii_lowercase(),
        };

        match tag.validate() {
            Ok(_) => Ok(tag),
            Err(_) => Err(ValidationError::new("2")),
        }
    }
}

impl std::fmt::Display for Tag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.identifier)
    }
}

/// Custom function to validate a string used to build a [Tag].
fn validate_identifier(value: &str) -> Result<(), ValidationError> {
    let haystack = value.to_lowercase();
    if RE_TAG.is_match(&haystack) {
        Result::Ok(())
    } else {
        Err(ValidationError::new("2"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use rstest::*;

    #[rstest]
    #[case("A(tag)")]
    #[case("")]
    #[case("Averylongtagnamethatshallprovokeanerror")]
    #[case("anemoji❌")]
    fn wrong_string_fail_to_build_a_tag(#[case] input: &str) {
        assert!(Tag::new(input).is_err())
    }

    #[rstest]
    #[case("A_tag")]
    #[case("Ta")]
    #[case("customTag")]
    fn valid_string_succeed_to_build_a_tag(#[case] input: &str) {
        assert!(Tag::new(input).is_ok())
    }

    #[rstest]
    #[case("customTag")]
    #[case("ALLCAPITALLETTERSTAG")]
    fn tags_get_converted_to_lowercase(#[case] input: &str) {
        assert_eq!(
            Tag::new(input).expect("Failed to build a tag").identifier,
            input.to_lowercase()
        )
    }
}