use std::io::{stdin, stdout, Write}; use std::fs; use serde::{Serialize, Deserialize}; use chrono::Local; #[derive(Serialize, Deserialize)] struct Emoji { name: String, category: String, aliases: Vec, } #[derive(Serialize, Deserialize)] struct File { downloaded: bool, file_name: String, emoji: Emoji, } #[derive(Serialize, Deserialize)] struct MetaJson { meta_version: i32, host: String, exported_at: String, emojis: Vec, } fn main() { let version_string = request_input(String::new(), "What is the version of the emoji set?"); let meta_json = MetaJson { meta_version: version_string.parse::().unwrap(), host: request_input(String::new(), "Where can this emoji set be downloaded?"), exported_at: Local::now().to_string(), emojis: get_files(), }; let final_json = serde_json::to_string_pretty(&meta_json).expect("Error: Could not turn JSON into string"); let _ = fs::write("./meta.json", final_json); } fn request_input(mut input: String, request: &str) -> String { println!("{}", request); let _= stdout().flush(); stdin().read_line(&mut input).expect("Error: could not read line"); return input.trim().to_string(); } fn get_files() -> Vec { let mut files: Vec = vec![]; let category = request_input(String::new(), "What category would you like the emoji's to be in?"); let folder = fs::read_dir(".").unwrap(); for file in folder { let file_name = file.as_ref().expect("Error: could not read file").file_name().into_string().expect("Error: could not read filename as String"); let file_name_parts: Vec<&str> = file_name.split('.').collect(); let name = file_name_parts[0]; let extension = file_name_parts[1]; if file.as_ref().expect("Error: could not read file").path().with_extension(extension).exists() && extension == "png" { let file_name = file.expect("Error: could not read file").file_name().into_string().expect("Error: could not read filename as String"); let emoji_data = Emoji { name: name.to_string(), category: category.clone(), aliases: vec![], }; let new_file = File { downloaded: true, file_name: file_name, emoji: emoji_data, }; files.push(new_file); } } files }