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
//! Contains a [`Tree`] type that is used to process the dotted package names into
//! directory structured files.

use std::{
    collections::{BTreeSet, HashMap},
    ffi::{OsStr, OsString},
    fmt::{Debug, Display},
    iter::FromIterator,
    path::{Path, PathBuf},
};

use anyhow::{bail, Context, Result};
use fs_err::OpenOptions;

#[derive(Default, Debug, PartialEq)]
pub struct Tree(pub(crate) HashMap<PathBuf, Tree>);

impl Extend<PathBuf> for Tree {
    fn extend<T: IntoIterator<Item = PathBuf>>(&mut self, iter: T) {
        for path in iter {
            self.insert_path(path)
        }
    }
}

impl FromIterator<PathBuf> for Tree {
    fn from_iter<T: IntoIterator<Item = PathBuf>>(iter: T) -> Self {
        let mut tree = Tree::default();
        tree.extend(iter);
        tree
    }
}

impl Tree {
    /// Given a file path that is `.` separated, it loads it into the tree.
    pub fn insert_path(mut self: &mut Self, path: PathBuf) {
        for comp in path.file_stem().unwrap().to_str().unwrap().split('.') {
            self = self.0.entry(PathBuf::from(comp)).or_default()
        }
    }

    /// Generates the module at the root level of the tree
    pub fn generate_module(&self) -> String {
        let mut module = String::from("// Module generated with `grpc_build`\n");
        let sorted: BTreeSet<_> = self.0.keys().collect();
        for k in sorted {
            module.push_str(&format!("pub mod {};\n", k.display()));
        }

        module.push('\n');
        module
    }

    /// Loop through the tree, determining where all the files should be
    /// and moving them there
    pub fn move_paths(&self, root: &Path, filename: OsString, output: PathBuf) -> Result<()> {
        if self.0.is_empty() {
            fs_err::create_dir_all(root.join(&output).parent().unwrap())
                .with_context(|| format!("could not create dir for file {}", output.display()))?;

            let from = root.join(filename.add("rs"));
            let to = root.join(output.with_extension("rs"));
            fs_err::rename(&from, &to).with_context(|| {
                format!("could not move {} to {}", from.display(), to.display())
            })?;
        } else {
            for (k, tree) in &self.0 {
                tree.move_paths(root, filename.add(k), output.join(k))?;
            }

            if !filename.is_empty() {
                self.create_module_file(root, filename, output)?;
            }
        }
        Ok(())
    }

    fn create_module_file(
        &self,
        root: &Path,
        filename: OsString,
        output: PathBuf,
    ) -> Result<(), anyhow::Error> {
        let maybe_proto_file_name = root.join(filename.add("rs"));
        let dest_tmp_file_name = root.join(output.with_extension("tmp"));
        let final_dest_name = root.join(output.with_extension("rs"));

        // Write a temporary file with the module contents
        let modules = self.generate_module();
        fs_err::write(&dest_tmp_file_name, modules)
            .with_context(|| format!("could not write to file {}", final_dest_name.display()))?;

        // If there is a proto file in this directory, we append its contents to the already written temporary module file
        if fs_err::metadata(&maybe_proto_file_name)
            .map(|m| m.is_file())
            .unwrap_or(false)
        {
            merge_file_into(&maybe_proto_file_name, &dest_tmp_file_name)?;
        }

        // Finally, move the temporary file to the final destination
        fs_err::rename(&dest_tmp_file_name, &final_dest_name).with_context(|| {
            format!(
                "could not move {} to {}",
                dest_tmp_file_name.display(),
                final_dest_name.display()
            )
        })?;

        Ok(())
    }
}

fn merge_file_into(from: &PathBuf, to: &PathBuf) -> Result<(), anyhow::Error> {
    if from == to {
        bail!("Merging files, source and destination files are the same");
    }

    let mut source = OpenOptions::new()
        .read(true)
        .open(from)
        .with_context(|| format!("Failed to open not source file {}", to.display()))?;

    let mut dest = OpenOptions::new()
        .create_new(false)
        .write(true)
        .append(true)
        .open(to)
        .with_context(|| format!("Failed to open the destination file {}", from.display()))?;

    std::io::copy(&mut source, &mut dest).with_context(|| {
        format!(
            "could not copy contents from {} to {}",
            from.display(),
            to.display()
        )
    })?;

    fs_err::remove_file(from)
        .with_context(|| format!("could not remove file {}", from.display()))?;
    Ok(())
}

// private helper trait
trait OsStrExt {
    fn add(&self, add: impl AsRef<OsStr>) -> OsString;
}

impl OsStrExt for OsStr {
    /// Adds `add` to the [`OsStr`], returning a new [`OsString`]. If there already exists data in the string,
    /// this puts a `.` separator inbetween
    fn add(&self, add: impl AsRef<OsStr>) -> OsString {
        let mut _self = self.to_owned();
        if !_self.is_empty() {
            _self.push(".");
        }
        _self.push(add);
        _self
    }
}

impl Display for Tree {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (k, tree) in &self.0 {
            write!(f, "pub mod {}", k.display())?;
            if tree.0.is_empty() {
                write!(f, ";")?;
            } else {
                write!(f, "{{{}}}", tree)?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::PathBuf;

    use super::Tree;

    macro_rules! tree {
        ($($key:literal : $val:expr,)*) => {
            Tree(HashMap::from_iter([
                $(
                    (PathBuf::from($key), $val)
                ),*
            ]))
        };
    }

    #[test]
    fn build_tree() {
        let tree: Tree = [
            "grpc_build.client.helloworld.rs",
            "grpc_build.request.helloworld.rs",
            "grpc_build.response.helloworld.rs",
            "google.protobuf.foo.rs",
            "google.protobuf.bar.rs",
        ]
        .into_iter()
        .map(PathBuf::from)
        .collect();

        let expected = tree! {
            "grpc_build": tree! {
                "client": tree! {
                    "helloworld": tree!{},
                },
                "request": tree! {
                    "helloworld": tree!{},
                },
                "response": tree! {
                    "helloworld": tree!{},
                },
            },
            "google": tree! {
                "protobuf": tree! {
                    "foo": tree!{},
                    "bar": tree!{},
                },
            },
        };

        assert_eq!(tree, expected);
    }

    #[test]
    fn generate_module_returns_at_current_level() {
        let tree: Tree = [
            "grpc_build.client.helloworld.rs",
            "grpc_build.request.helloworld.rs",
            "grpc_build.response.helloworld.rs",
            "google.protobuf.foo.rs",
            "google.protobuf.bar.rs",
            "alphabet.foo.rs",
            "hello.rs",
        ]
        .into_iter()
        .map(PathBuf::from)
        .collect();

        let expected = "// Module generated with `grpc_build`
pub mod alphabet;
pub mod google;
pub mod grpc_build;
pub mod hello;

";

        assert_eq!(tree.generate_module(), expected);
    }

    #[test]
    fn generate_module_returns_at_current_level_nested() {
        let tree: Tree = [
            "grpc_build.client.helloworld.rs",
            "grpc_build.request.helloworld.rs",
            "grpc_build.response.helloworld.rs",
            "google.protobuf.foo.rs",
            "google.protobuf.bar.rs",
            "alphabet.foo.rs",
            "hello.rs",
        ]
        .into_iter()
        .map(PathBuf::from)
        .collect();

        let inner_tree = tree.0.get(&PathBuf::from("grpc_build")).unwrap();
        let expected = "// Module generated with `grpc_build`
pub mod client;
pub mod request;
pub mod response;

";

        assert_eq!(inner_tree.generate_module(), expected);
    }
}