httpfile_build/
lib.rs

1mod models;
2mod parser;
3
4use std::env;
5use std::fs::File;
6use std::io::{Write};
7use std::path::{Path, PathBuf};
8
9#[derive(Debug, Clone)]
10pub struct Builder {
11    pub httpfile_path: String,
12    pub http_client: String,
13    out_dir: Option<PathBuf>,
14}
15
16pub fn configure() -> Builder {
17    Builder {
18        httpfile_path: "index.http".to_string(),
19        http_client: "reqwest".to_string(),
20        out_dir: Some(PathBuf::from(env::var("OUT_DIR").unwrap())),
21    }
22}
23
24impl Builder {
25    pub fn httpfile(mut self, httpfile_path: impl AsRef<Path>) -> Self {
26        self.httpfile_path = httpfile_path.as_ref().to_str().unwrap().to_string();
27        self
28    }
29    pub fn out_dir(mut self, out_dir: impl AsRef<Path>) -> Self {
30        self.out_dir = Some(out_dir.as_ref().to_path_buf());
31        self
32    }
33
34    pub fn compile(self) -> std::io::Result<()> {
35        let http_file_path = Path::new(&self.httpfile_path);
36        let httpfile_text = std::fs::read_to_string(http_file_path)?;
37        let request_targets = parser::parse_http_code(&httpfile_text);
38        let mut file_lines: Vec<String> = Vec::new();
39        file_lines.push("use std::collections::HashMap;".to_owned());
40        file_lines.push("use reqwest::{Client, Response, Result};".to_owned());
41        file_lines.push("use reqwest::header::HeaderMap;".to_owned());
42        file_lines.push("use handlebars::Handlebars;".to_owned());
43        file_lines.push("".to_owned());
44        // lazy_static block
45        file_lines.push("lazy_static::lazy_static! {".to_owned());
46        file_lines.push("  static ref CLIENT: Client = reqwest::Client::new();".to_owned());
47        file_lines.push("  static ref HANDLEBARS: Handlebars<'static> = {".to_owned());
48        file_lines.push("    let mut reg = Handlebars::new();".to_owned());
49        // include http body template
50        for request_target in &request_targets {
51            if let Some(body) = &request_target.body {
52                if body.contains("{{") {
53                    let template_name = format!("{}_body", request_target.name);
54                    file_lines.push(format!("    reg.register_template_string(\"{}\", r#\"{}\"#).unwrap();", template_name, body));
55                }
56            }
57        }
58        file_lines.push("    reg".to_owned());
59        file_lines.push("    };".to_owned());
60        file_lines.push("  }".to_owned());
61        for request_target in &request_targets {
62            file_lines.push("".to_owned());
63            file_lines.push(request_target.to_rust_code());
64        }
65        let rust_file_code = file_lines.join("\n");
66        let rust_file_name = http_file_path.file_name().unwrap().to_str().unwrap().replace(".http", ".rs");
67        let dest_path = self.out_dir.unwrap().join(rust_file_name);
68        println!("dest_path = {:?}", dest_path);
69        let mut file = File::create(dest_path)?;
70        file.write_all(rust_file_code.as_bytes())
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_parser() {
80        let path = env::current_dir().unwrap().join("temp");
81        env::set_var("OUT_DIR", path.to_str().unwrap());
82        configure()
83            .httpfile("index.http")
84            .compile()
85            .unwrap();
86    }
87}
88