#[cfg(feature="release")]
pub mod release;
use serde::{Serialize, Deserialize};
use toml::{from_str, to_string_pretty};
use std::collections::{HashMap, hash_map::IntoIter};
use std::iter::IntoIterator;
use std::path::PathBuf;
use reqwest::Client;
use texcore::template::{Template, Version};
use tokio::io::{Result, AsyncWriteExt};
use tokio::fs::File;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Repo{
current_vers: u64,
texcreate: Version,
num: u64,
info: HashMap<String, String>
}
impl Repo{
pub fn new(current_vers: u64, templates: &Vec<Template>, texcreate: Version) -> Self{
let num = templates.len() as u64;
let mut map = HashMap::new();
for t in templates{
let name = t.name.to_string();
let desc = t.description.to_string();
map.insert(name, desc);
}
Self{current_vers, texcreate, num, info: map}
}
pub fn version(&self) -> u64{
self.current_vers
}
pub fn texc_vers(&self) -> Version{
self.texcreate
}
pub fn num(&self) -> u64{
self.num
}
pub fn info(&self) -> HashMap<String, String>{
self.info.clone()
}
pub fn from_string(s: &str) -> Self{
from_str(s).unwrap()
}
pub fn to_string(&self) -> String{
to_string_pretty(&self).unwrap()
}
pub async fn get_repo(url: &str) -> Self{
let client = Client::new();
let resp = client.get(url)
.send()
.await
.unwrap();
let text = resp.text().await.unwrap();
Self::from_string(&text)
}
pub fn display(&self){
println!("TexCreate Repo: v{}", self.current_vers);
println!("Number of Templates: {}", self.num);
println!("======TEMPLATES======");
for (n, d) in self.clone().into_iter(){
println!("=> {n}: {d}")
}
println!("=====================");
}
pub fn template_exist(&self, name: &str) -> bool{
self.info.contains_key(name)
}
}
pub async fn download_repo(url: &str, out_path: PathBuf) -> Result<()>{
let client = Client::new();
let resp = client.get(url)
.send().await.unwrap();
let data = resp.bytes().await.unwrap();
let path = out_path.join("repo.toml");
let mut file = File::create(&path).await?;
file.write_all(&data).await?;
Ok(())
}
impl IntoIterator for Repo{
type Item = (String, String);
type IntoIter = IntoIter<String, String>;
fn into_iter(self) -> Self::IntoIter {
self.info.into_iter()
}
}