Skip to main content

kellnr_common/
normalized_name.rs

1use std::fmt;
2
3use sea_orm::Value;
4
5use crate::original_name::OriginalName;
6
7/// Index name is a lowercase version of the crate name
8
9#[derive(Debug, PartialEq, Eq, Clone)]
10pub struct NormalizedName(String);
11
12impl NormalizedName {
13    pub fn from_unchecked(name: String) -> Self {
14        NormalizedName(name)
15    }
16
17    pub fn from_unchecked_str(name: &str) -> Self {
18        NormalizedName(name.to_owned())
19    }
20
21    pub fn into_inner(self) -> String {
22        self.0
23    }
24}
25
26impl From<OriginalName> for NormalizedName {
27    fn from(name: OriginalName) -> Self {
28        NormalizedName(name.to_lowercase())
29    }
30}
31
32impl From<&OriginalName> for NormalizedName {
33    fn from(name: &OriginalName) -> Self {
34        NormalizedName(name.to_lowercase())
35    }
36}
37
38impl From<NormalizedName> for Value {
39    fn from(value: NormalizedName) -> Self {
40        Value::String(Some(value.0))
41    }
42}
43
44impl From<&NormalizedName> for Value {
45    fn from(value: &NormalizedName) -> Self {
46        Value::String(Some(value.0.clone()))
47    }
48}
49
50impl fmt::Display for NormalizedName {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(f, "{}", self.0)
53    }
54}
55
56impl std::ops::Deref for NormalizedName {
57    type Target = String;
58
59    fn deref(&self) -> &Self::Target {
60        &self.0
61    }
62}