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
use std::str::FromStr;

use crate::io::RenderKotlin;
use crate::spec::CodeBlock;
use crate::tokens;
use crate::util::{SemanticConversionError, yolo_from_str};

/// Kotlin identifier name, automatically escaped with backticks if it contains escapable tokens
///
/// # Examples
/// ```rust
/// use std::str::FromStr;
/// use kotlin_poet_rs::io::RenderKotlin;
/// use kotlin_poet_rs::spec::Name;
///
/// let name = Name::from("Foo");
/// assert_eq!(name.render_string(), "Foo");
///
/// let escaped_name = Name::from("Foo Bar");
/// assert_eq!(escaped_name.render_string(), "`Foo Bar`")
/// ```
#[derive(Debug, PartialEq, Clone)]
pub struct Name {
    value: String,
    should_be_escaped: bool
}

yolo_from_str!(Name);
/// Creates new [Name] from [&str]
impl FromStr for Name {
    type Err = SemanticConversionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.is_empty() {
            return Err(
                SemanticConversionError::new(
                    "Name cannot be empty"
                )
            )
        }

        if s.chars().any(|ch| tokens::NAME_DISALLOWED_TOKENS.contains(ch)) {
            return Err(
                SemanticConversionError::new(
                    format!("`{} contains tokens not allowed in kotlin identifier names`", s)
                        .as_str()
                )
            )
        }

        let should_be_escaped = s.chars().any(
            |ch| tokens::NAME_ESCAPED_TOKENS.contains(ch)
        );
        
        Ok(
            Name {
                value: s.to_string(),
                should_be_escaped
            }
        )
    }
}

impl RenderKotlin for Name {
    fn render_into(&self, block: &mut CodeBlock) {
        if self.should_be_escaped {
            block.push_atom(format!("`{}`", self.value).as_str());
            return;
        }

        block.push_atom(self.value.as_str());
    }
}

impl Into<String> for Name {
    fn into(self) -> String {
        self.value
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use super::*;

    #[test]
    fn test_name() {
        let name = Name::from_str("Foo").unwrap();
        assert_eq!(name.render_string(), "Foo");
    }

    #[test]
    fn test_name_with_space() {
        let name = Name::from_str("Foo Bar").unwrap();
        assert_eq!(name.render_string(), "`Foo Bar`");
    }

    #[test]
    fn test_name_with_parentheses() {
        let name = Name::from_str("Foo()Bar").unwrap();
        assert_eq!(name.render_string(), "`Foo()Bar`");
    }

    #[test]
    fn test_name_with_disallowed_characters() {
        let name = Name::from_str("Foo/Bar");
        assert!(matches!(name, Err(_)));
    }

    #[test]
    fn test_empty_name() {
        let name = Name::from_str("");
        assert!(matches!(name, Err(_)));
    }
}