use core::panic;
use std::collections::{BTreeMap, HashMap};
use std::ffi::OsStr;
use std::fs::File;
use std::io::{self, prelude::*, BufReader, Error};
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
use crate::errors::TektonError;
pub fn get_input() -> String {
let mut input = String::new();
while input == String::new() {
io::stdin()
.read_line(&mut input)
.expect("Error with reading input");
}
return input.trim().to_string();
}
pub fn clear_terminal() {
print!("\x1B[2J\x1B[1;1H"); }
pub fn read_lines(fname: &String) -> Result<Vec<String>, Error> {
let file = File::open(fname)?;
let buf = BufReader::new(file);
Ok(buf
.lines()
.map(|line| line.expect("Could not parse line"))
.collect())
}
pub fn write_to_file(output_name: String, finished: String) {
let mut outfile = File::create(Path::new("./").join(output_name))
.unwrap_or_else(|err| panic!("Could not create the file {}", err));
outfile
.write_all(finished.as_bytes())
.unwrap_or_else(|err| panic!("Could not write the snippets\n>>> Error >>>{}", err));
}
pub fn crawl_files(path: String, crawl: Option<String>) -> Vec<PathBuf> {
let mut files: Vec<PathBuf> = Vec::new();
if crawl.is_some() {
for file in WalkDir::new(path).into_iter().filter_map(|file| file.ok()) {
if file.metadata().unwrap().is_file() {
files.push(file.path().to_path_buf());
}
}
} else {
files.push(PathBuf::from(path));
}
files
}
pub fn get_filetype(filename: &str) -> Option<&str> {
Path::new(filename).extension().and_then(OsStr::to_str)
}
pub fn hash2ordered_string<T>(table: &HashMap<String, T>) -> Result<String, TektonError>
where
T: Serialize + for<'a> Deserialize<'a>,
{
match table.len() {
0 => Err(TektonError::Reason(
"Refusing to build string for 0 snippets".to_string(),
)),
_ => {
let mut keys: Vec<String> = table.iter().map(|(k, _)| k.to_string()).collect();
keys.sort_by_key(|a| a.to_lowercase());
let ordered: BTreeMap<String, _> = keys
.iter()
.map(|key| {
let snippet = table.get(key).unwrap();
(key.clone(), snippet)
})
.collect();
match serde_json::to_string_pretty(&ordered) {
Ok(string) => Ok(string),
Err(e) => Err(TektonError::Reason(e.to_string())),
}
}
}
}
#[cfg(test)]
mod tests {
use super::get_filetype;
#[test]
fn test_empty_string_on_extension() {
let filename = String::from("");
let result = get_filetype(&filename);
assert_eq!(result, None);
}
#[test]
fn test_extension() {
let filename = String::from("example.json");
let result = get_filetype(&filename);
assert_ne!(result, None);
assert_eq!(result.unwrap(), "json");
}
#[test]
fn test_long_filename_extension() {
let filename = String::from("long_file_name_example.json");
let result = get_filetype(&filename);
assert_ne!(result, None);
assert_eq!(result.unwrap(), "json");
}
#[test]
fn test_extension_on_snippet() {
let filename = String::from("example.snippet");
let result = get_filetype(&filename);
assert_ne!(result, None);
assert_eq!(result.unwrap(), "snippet");
}
#[test]
fn test_filename_with_parens_extension() {
let filename = String::from("exam(file)ple.json");
let result = get_filetype(&filename);
assert_ne!(result, None);
assert_eq!(result.unwrap(), "json");
}
#[test]
fn test_filename_with_braces_extension() {
let filename = String::from("exam{file}ple.json");
let result = get_filetype(&filename);
assert_ne!(result, None);
assert_eq!(result.unwrap(), "json");
}
#[test]
fn test_filename_with_brackets_extension() {
let filename = String::from("exam[file]ple.json");
let result = get_filetype(&filename);
assert_ne!(result, None);
assert_eq!(result.unwrap(), "json");
}
}