#![allow(non_ascii_idents)]
#[allow(dead_code)]
mod test;
mod generator;
mod data;
use std::fs;
use std::io::Error;
use std::path::PathBuf;
pub enum TemplateSource {
Text(String),
File(PathBuf)
}
pub struct Configuration {
pub template: TemplateSource,
pub cargo_lock_path: PathBuf,
pub target_path: Option<PathBuf>,
pub post_template_search: Option<String>,
pub post_template_replace: String,
pub include_root: bool,
pub maximum_depth: Option<usize>,
}
impl Default for Configuration {
fn default() -> Self {
Self {
template: TemplateSource::File("src/deps.template.rs".into()),
cargo_lock_path: "Cargo.lock".into(),
target_path: None,
post_template_search: Some("//{}".into()),
post_template_replace: "".into(),
include_root: false,
maximum_depth: None,
}
}
}
impl Configuration {
pub fn target_path(&self) -> PathBuf {
if let Some(target_path) = &self.target_path {
target_path.clone()
} else if let TemplateSource::File(template_file) = &self.template {
let template_file_path = template_file.to_str().unwrap();
if !template_file_path.contains(".template.") {
panic!("When output is not specified, the input file must contain the pattern β.template.β");
}
let target_path = template_file_path.replace(".template.", ".");
target_path.into()
} else {
panic!("Canβt guess output file!")
}
}
pub fn template_text(&self) -> String {
match &self.template {
TemplateSource::Text(text) => text.clone(),
TemplateSource::File(template_file) => {
fs::read_to_string(template_file).expect("Problem reading template file")
},
}
}
}
pub fn gen_deps() -> Result<(), Error> {
gen_deps_with_conf(Configuration::default())
}
pub fn gen_deps_with_conf(configuration: Configuration) -> Result<(), Error> {
if let TemplateSource::File(source_path) = &configuration.template {
println!("cargo:rerun-if-changed={}", fs::canonicalize(source_path).unwrap().to_str().unwrap());
}
generator::Generator::gen_with_configuration(configuration)
}