#[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")
;
}
}
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)]
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 {
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;
}