lacoctelera/domain/ingredient.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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
// 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/.
use anyhow::bail;
use core::fmt;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::convert::{Into, TryFrom};
use std::error::Error;
use tracing::error;
use utoipa::ToSchema;
use uuid::Uuid;
use super::DataDomainError;
/// This value is set in the DB's schema definition (VARCHAR(40)).
const MAX_NAME_LENGTH: usize = 40;
/// This value is set in the DB's schema definition (VARCHAR(255)).
const MAX_DESC_LENGTH: usize = 255;
/// Types of ingredients of teh `Cocktail` data base.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, ToSchema)]
pub enum IngCategory {
/// Spirit ingredients, such as rum, liquors and so.
Spirit,
/// Bitter ingredients, such as Angostura.
Bitter,
/// Soft-drink ingredients, such as soda water, Fanta, Coke, etc.
#[serde(alias = "soft_drink")]
SoftDrink,
/// Garnish ingredients, such a lemon's peel.
Garnish,
/// Category for ingredients whose type does not match any of the other types.
Other,
}
/// Object that represents an Ingredient of the `Cocktail` data base.
///
/// # Description
///
/// An ingredient represents the elements that are contained by a Cocktail's recipe.
/// This object only represents the ingredients as a model for being included in
/// individual recipes. Hence no information related to quantities or any other information
/// that joins an ingredient with a recipe is included as an attribute of this object.
#[derive(Clone, Serialize, Deserialize, ToSchema)]
pub struct Ingredient {
id: Option<Uuid>,
name: String,
category: IngCategory,
description: Option<String>,
}
impl Ingredient {
/// Builds a new [Ingredient] performing checks over the input parameters.
///
/// # Description
///
/// The implementation checks that the given _name_ value meets the following requirements:
/// - The length of the name doesn't exceeds 40 characters.
/// - The name is composed of alphanumeric characters plus the special character `%`.
/// - The name does not contain the following forbidden characters: `[`, `<`, `>`, `;`,
/// `{`, `}`, `]`.
///
/// # Arguments
///
/// - _name_ will be used as [Ingredient::name].
/// - _category_ will be used as [Ingredient::category]. Use [IngCategory::Other] when no
/// needed.
/// - _desc_ will be used as [Ingredient::desc]. Pass `None` when no description was provided
/// along the Ingredient's name.
///
/// # Return
///
/// A new [Ingredient] when the input parameters comply the format rules, an error otherwise
/// that contains a message with information about the broken format rule.
pub fn parse(
id: Option<&str>,
name: &str,
category: &str,
description: Option<&str>,
) -> Result<Self, Box<dyn Error>> {
let id = if let Some(id) = id {
Some(Uuid::parse_str(id).map_err(|e| {
error!("Failed to parse an UUID from {e}");
Box::new(DataDomainError::InvalidId)
})?)
} else {
None
};
let name = match Ingredient::check_name(name) {
Ok(name) => name,
Err(e) => {
error!("Invalid name ({e}) given to parse an Ingredient");
return Err(Box::new(DataDomainError::InvalidFormData));
}
};
let category = category.try_into()?;
let description = match description {
Some(desc) => match Ingredient::check_desc(desc) {
Ok(desc) => Some(desc),
Err(e) => {
error!("Invalid description ({e}) given to parse an Ingredient");
return Err(Box::new(DataDomainError::InvalidFormData));
}
},
None => None,
};
Ok(Self {
name,
category,
description,
id,
})
}
/// Get the Ingredient's name.
pub fn name(&self) -> &str {
&self.name
}
/// Get the Ingredient's category as a value of the `Enum` [IngCategory].
pub fn category(&self) -> IngCategory {
self.category
}
/// Get the description of the Ingredient. Wrapped to allow empty descriptions.
pub fn desc(&self) -> Option<&str> {
self.description.as_deref()
}
/// Get the ingredient's ID in the `Cocktail` data base.
pub fn id(&self) -> Option<Uuid> {
self.id
}
/// Set the ID of the ingredient in the `Cocktail` data base.
pub fn set_id(&mut self, id: Uuid) {
self.id = Some(id);
}
/// Check that a string is valid as [Ingredient::name].
///
/// # Description
///
/// This internal method performs a series of checks against a given string in order
/// to detect the violation of some design rule for [Ingredient::name]. The following
/// is checked:
/// - The string does not exceed 40 characters.
/// - The string's format is a name that might includes numbers and/or the symbol `%`.
/// - The string does not contain the following forbidden characters: `[`, `<`, `>`, `;`,
/// `{`, `}`, `]`.
///
/// # Arguments
///
/// A string that contains the name to be checked.
///
/// # Return
///
/// A `Result` enum with:
/// - A `String` on success that is an owned copy of the string given as argument.
/// - Otherwise, an error that contains a message that informs what rule was violated.
fn check_name(name: &str) -> Result<String, anyhow::Error> {
// Avoid processing long strings that exceed the maximum allowed.
if name.len() > MAX_NAME_LENGTH {
bail!("The length of the given Ingredient's name exceeds {MAX_NAME_LENGTH} characters.")
}
// Regex for the validation of usual strings composed by words, numbers
// and the symbol %.
let validation_regex = [
Regex::new(r"^[[:alpha:]]{1,}? +[[:alpha:]]{0,}?").unwrap(),
Regex::new(r"^[[:alpha:]]{1,}?[[ :alpha:\d]%]{0,}").unwrap(),
];
// Apply the previous regex to the input value.
let validated = validation_regex
.iter()
.fold(false, |acc, r| acc | r.is_match(name));
if !validated {
bail!("The given Ingredient's name ({name}) has an invalid format.")
}
// Finally, look for forbidden characters in the input string.
let forbidden_chars = Regex::new(r"[;<>`\{\}]").unwrap();
if forbidden_chars.is_match(name) {
bail!("The given Ingredient's name ({name}) contains invalid characters.")
} else {
Ok(String::from(name))
}
}
/// Check that a string is valid as [Ingredient::desc].
///
/// # Description
///
/// A very basic check is performed: ensure that the length doesn't exceeds the
/// maximum allowed (255 characters).
///
/// # Arguments
///
/// A string that contains the description of an `Ingredient`.
///
/// # Return
///
/// A `Result` enum with:
/// - A `String` on success that contains an owned version of the string given as
/// argument.
/// - Otherwise, an error that contains a message that informs about the violated rule.
fn check_desc(desc: &str) -> Result<String, anyhow::Error> {
// Avoid processing long strings that exceed the maximum allowed.
if desc.len() > MAX_DESC_LENGTH {
bail!("The length of the given string exceeds {MAX_DESC_LENGTH} characters.")
}
Ok(String::from(desc))
}
}
impl PartialEq for Ingredient {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.category == other.category
&& self.description == other.description
}
}
impl TryFrom<String> for IngCategory {
type Error = anyhow::Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
value.as_str().try_into()
}
}
impl TryFrom<&str> for IngCategory {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Spirit" | "spirit" => Ok(IngCategory::Spirit),
"Bitter" | "bitter" => Ok(IngCategory::Bitter),
"SofDrink" | "softdrink" | "soft_drink" => Ok(IngCategory::SoftDrink),
"Garnish" | "garnish" => Ok(IngCategory::Garnish),
"Other" | "other" => Ok(IngCategory::Other),
_ => bail!("Invalid ingredient category."),
}
}
}
impl fmt::Display for IngCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_str())
}
}
impl IngCategory {
pub fn to_str(&self) -> &str {
match self {
IngCategory::Bitter => "bitter",
IngCategory::Garnish => "garnish",
IngCategory::SoftDrink => "soft_drink",
IngCategory::Spirit => "spirit",
IngCategory::Other => "other",
}
}
}
impl fmt::Debug for Ingredient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"'name': '{}', 'category': '{}', 'desc': '{}'",
self.name,
self.category,
self.description.as_deref().unwrap_or("")
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::*;
#[rstest]
#[case("vodka", true)]
#[case("white rum", true)]
#[case("vodka 80", true)]
#[case("liquor 20%", true)]
#[case("liquor 40% coconut", true)]
#[case("my new fancy rum 100", true)]
#[case("<insert>", false)]
#[case("name;`another string`", false)]
#[case("very long text string should not be accepted", false)]
fn convert_names_to_ingredients(#[case] input: &str, #[case] expected: bool) {
assert_eq!(Ingredient::check_name(input).is_ok(), expected);
}
}