use std::fs;
use std::path::{PathBuf};
use std::str::FromStr;
pub fn root_path(rootdir: &PathBuf) -> PathBuf {
rootdir.join("_meta/src/seeds/")
}
pub fn db_list(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
Ok(
fs::read_dir(root_path(rootdir))?
.map(|res| res.expect("path should be a path").path())
.filter(|e| e.is_dir())
.map(|e| format!("{}", e.file_name().unwrap().to_str().unwrap().to_string()))
.collect()
)
}
pub fn dbs(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
Ok(
db_list(rootdir)?
.iter()
.map(|db| {
format!(
include_str!("../../../static/templates/db_seeds.tpl"),
db,
{
let mut c = db.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}
)
})
.collect()
)
}
pub fn available_seeds(rootdir: &PathBuf) -> Result<Vec<Vec<PathBuf>>, Box<dyn std::error::Error>> {
db_list(rootdir)?
.iter()
.map(|db| {
let seeds_path: PathBuf = PathBuf::from_str(&format!(
"{}/_meta/src/seeds/{}",
rootdir.to_str().unwrap(),
db
)).unwrap();
Ok(
fs::read_dir(&seeds_path)
.map(|res| res.map(|e| e.as_ref().expect("could not read reference").path()))
.expect("Error in read_dir: seeds_path")
)
})
.map(|a| {
a.map(|r|
r.filter(|e| e.file_name().unwrap() != "mod.rs")
.collect()
)
}).collect()
}
pub fn available_seeds_enum(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
Ok(
available_seeds(rootdir)?
.iter()
.map(|a| {
a.iter()
.map(|e| {
let mut s = e
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string()
.replace(".rs", ",");
s.push_str("\n");
s
})
.collect()
})
.collect()
)
}
pub fn db_actions(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let available_seeds_enum: Vec<String> = available_seeds_enum(&rootdir)?;
Ok(
db_list(&rootdir)?
.iter()
.enumerate()
.map(|(index, db)| {
format!(
include_str!("../../../static/templates/seeds_actions.tpl"),
db, available_seeds_enum[index]
)
})
.collect()
)
}
pub fn db_match(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
Ok(
available_seeds(rootdir)?
.iter()
.map(|a| {
a.iter()
.enumerate()
.map(|(index, file)| {
format!(
include_str!("../../../static/templates/seeds_match.tpl"),
index,
file.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string()
.replace(".rs", "")
)
})
.collect()
})
.collect()
)
}
pub fn db_matches(rootdir: &PathBuf) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let db_match = db_match(&rootdir)?;
Ok(
db_list(&rootdir)?
.iter()
.enumerate()
.map(|(index, db)| {
format!(
include_str!("../../../static/templates/seeds_db_matches.tpl"),
index,
{
let mut c = db.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
},
db_match[index]
)
})
.collect()
)
}