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
use std::path::PathBuf;
use crate::io::RenderKotlin;
use crate::spec::{CodeBlock, Name};
use crate::tokens::DOT;
use crate::util::{yolo_from_str, SemanticConversionError};
use std::str::FromStr;

/// Fully qualified package name, may be parsed from [&str]
///
/// # Examples
///
/// ## Parse from string
/// ```rust
/// use std::str::FromStr;
/// use kotlin_poet_rs::io::RenderKotlin;
/// use kotlin_poet_rs::spec::Package;
///
/// let package = Package::from_str("io.github.lexadiky").unwrap();
///
/// assert_eq!(
///     package.render_string(),
///     "io.github.lexadiky"
/// );
/// ```
///
/// ## Create from [Vec<Name>]
/// ```rust
/// use std::str::FromStr;
/// use kotlin_poet_rs::io::RenderKotlin;
/// use kotlin_poet_rs::spec::{Name, Package};
///
/// let package = Package::from(vec![
///     Name::from("io"),
///     Name::from("github"),
///     Name::from("lexadiky"),
/// ]);
///
/// assert_eq!(
///     package.render_string(),
///     "io.github.lexadiky"
/// );
/// ```
///
/// ## Create root
/// ```rust
/// use std::str::FromStr;
/// use kotlin_poet_rs::io::RenderKotlin;
/// use kotlin_poet_rs::spec::{Name, Package};
///
/// let package = Package::root();
///
/// assert_eq!(
///     package.render_string(),
///     ""
/// );
/// ```
#[derive(Debug, PartialEq, Clone)]
pub struct Package {
    pub(crate) parts: Vec<Name>,
}

impl Package {
    /// Creates new package from [Name] parts
    pub fn from(names: Vec<Name>) -> Package {
        Package { parts: names }
    }

    /// Create root package
    pub fn root() -> Package {
        Package { parts: Vec::new() }
    }

    /// Converts package to Java-like folder structure path
    #[cfg(feature = "experimental")]
    pub fn to_path(&self) -> PathBuf {
        let mut buf = PathBuf::new();
        for part in &self.parts {
            let part_str: String = part.clone().into();
            buf.push(part_str)
        }

        buf
    }

    pub(crate) fn is_root(&self) -> bool {
        self.parts.is_empty()
    }
}

yolo_from_str!(Package);
impl FromStr for Package {
    type Err = SemanticConversionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts = s.split(DOT)
            .filter(|part| !part.is_empty())
            .map(Name::from_str)
            .collect::<Result<Vec<_>, SemanticConversionError>>()?;
        Ok(Package::from(parts))
    }
}

impl RenderKotlin for Package {
    fn render_into(&self, block: &mut CodeBlock) {
        for (index, part) in self.parts.iter().enumerate() {
            block.push_renderable(part);
            if index != self.parts.len() - 1 {
                block.push_atom(DOT);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::io::RenderKotlin;
    use crate::spec::{Name, Package};
    use std::path::PathBuf;
    use std::str::FromStr;

    #[test]
    fn parse_package() {
        let package: Package = "io.github.lexadiky".parse().unwrap();
        assert_eq!(package.parts, vec![
            Name::from_str("io").unwrap(),
            Name::from_str("github").unwrap(),
            Name::from_str("lexadiky").unwrap(),
        ]);
    }

    #[test]
    fn parse_empty_package() {
        let package: Package = "".parse().unwrap();
        assert_eq!(package.parts, vec![]);
    }

    #[test]
    fn render_kotlin() {
        let package: Package = "io.github.lexadiky".parse().unwrap();
        assert_eq!(package.render_string(), "io.github.lexadiky");
    }

    #[test]
    fn render_empty() {
        let package: Package = Package::from(vec![]);
        assert_eq!(package.render_string(), "");
    }

    #[test]
    #[cfg(feature = "experimental")]
    fn test_path_conversion() {
        let package = Package::from_str("a.b.c");
        let expected_path = PathBuf::from_str("a/b/c");
        assert_eq!(
            package.unwrap().to_path(),
            expected_path.unwrap()
        )
    }

    #[test]
    #[cfg(feature = "experimental")]
    fn test_path_empty_conversion() {
        let package = Package::root();
        let expected_path = PathBuf::from_str("");
        assert_eq!(
            package.to_path(),
            expected_path.unwrap()
        )
    }
}