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
#[cfg(test)]
mod tests {
    use crate::builder::CargoConf;
    use crate::builder::DepVersion;
    #[test]
    fn it_works() {
        let  cargo = CargoConf::new()
        .author(
            &["Jakob Nikolaus Neufeld"]
        ).edition("2018")
        .name("truck")
        .version("0.0.1")
        .license("MIT")
        .dependencie_section()
        .dependencie("toml", DepVersion::Version("0.5.6"))
        .dependencie("crates-io", "0.32.0")
        ;
        
       //panic!(cargo.finalize()) ;      
        
    }
}







pub mod builder {
    use crates_io;
    use std::collections::HashMap;
    use std::fs::File;
    use std::io::{Read, Write};
    fn default_cargo_package() -> HashMap<String,Value> {
        let mut map = HashMap::default();
        map.insert(String::from("name"), toml::Value::String(String::from("default")));
        map.insert(String::from("version"), toml::Value::String(String::from("0.1.0")));
        map.insert(String::from("authors"), toml::Value::String(String::from("ME")));
        map.insert(String::from("edition"), toml::Value::String(String::from("2018")));
        map.insert(String::from("license"), toml::Value::String(String::from("MIT")));
        map.insert(String::from("build.rs"), toml::Value::String(String::from("truck.rs")));
        map
        
    }
    pub type CargoValue = HashMap<String, Value> ;
    pub use toml::Value;
    use toml::to_string;
    #[derive(Clone)]
    /// # The type to use for creating the cargo file
    pub struct CargoConf {
        package: HashMap<String, Value> 
    }
    pub enum DepVersion {
        Version(&'static str),
        Path(&'static str),
        Git(&'static str),

    }
    impl Into<String> for DepVersion  {
        fn into(self) -> String {
            return match self {
                DepVersion::Version(v) => v.to_string(),
                DepVersion::Path(v) => {
                    let mut f =  String::from("{ path = \" ");
                    f += &v;
                    f += " \"} ";
                    return f;
                }
                 DepVersion::Git(v) => {
                    let mut f =  String::from("{ git = \" ");
                    f += &v;
                    f += " \"} ";
                    return f;
                }
            }
        }
    }
    impl CargoConf {
        /// SELF EXPANATORY!
       pub fn new() -> CargoConf {
            CargoConf {
                package: default_cargo_package()
            }
        }
        pub fn custom(& mut self, key: &str, value : &str) {
             self.package.insert(String::from(key), toml::Value::String(String::from(value)));
        }
        pub fn name( mut  self, name: &str) ->  CargoConf {
            self.custom("name", name);
            self
        }
        pub fn license( mut self, license: &str) ->  CargoConf {
             self.custom("license", license);
             self
        }
         pub fn version( mut self, version: &str) -> CargoConf {
             self.custom("verion", version);
             self
        }
        
         pub fn author( mut self, author: &[&str]) ->  CargoConf {
             self.package.insert(String::from("author"), Value::Array(author.to_vec().iter().map(|s| Value::String(s.to_string())).collect()) );
             self
        }
        
         pub fn edition(mut self, edition: &str) ->  CargoConf {
             self.custom("edition", edition);
             self
        }
        
        

        pub fn dependencie_section(self) -> DependencieConf {
            DependencieConf {
               dependencies: HashMap::new(),
               package: self.package
           }
        }
    }
    #[doc(hidden)]
    pub struct DependencieConf {
        dependencies: CargoValue,
        package: CargoValue
    }
    impl DependencieConf {
        pub fn custom(& mut self, key: &str, value : &str) {
             self.dependencies.insert(String::from(key), toml::Value::String(String::from(value)));
        }
        pub fn dependencie<T: Into<String>>(mut self, name: &str, version: T) -> DependencieConf {
            self.custom(name, version.into().as_str());
            self
        }
        pub fn finalize(self) -> String {
            let mut cargo_toml = HashMap::new();
            cargo_toml.insert("package", self.package);
            cargo_toml.insert("dependencies", self.dependencies);
           return to_string(&cargo_toml).unwrap();
        }
        pub fn write_to_cargo(self) {
            let mut cargo_toml = File::open("Cargo.toml").expect("No Cargo.toml");
            cargo_toml.write(self.finalize().as_bytes());
        }
       
    }
}

pub mod prelude {
    pub use crate::builder;
}