use crate::errors::TektonError;
use crate::utils::{hash2ordered_string, read_lines};
use std::fs;
use super::tektons::friendly_tekton::{
compose_friendly_snippets, read_in_json_snippets, sort_friendly_snippets,
};
use super::tektons::multiprefix_tekton::dynamic_prefix_combinator;
use super::tektons::snipmate_tekton::compose_snipmate_snippets;
pub fn composer(
fname: &String,
types: (&str, &str),
interactive: bool,
) -> Result<String, TektonError> {
match types {
("snippet", "json") => match read_lines(fname) {
Ok(lines) => compose_friendly_snippets(lines),
Err(e) => Err(TektonError::Reason(e.to_string())),
},
("json", "snippet") => {
compose_snipmate_snippets(read_in_json_snippets(fname, interactive)?)
}
("json", "tekton-sort") => {
sort_friendly_snippets(read_in_json_snippets(fname, interactive)?)
}
_ => Err(TektonError::Reason(
"Unsupported mapping attempted in the composer function".to_string(),
)),
}
}
pub fn multiprefix_composer(fname: &str) -> Result<String, TektonError> {
match fs::read_to_string(fname) {
Ok(file_content) => {
let snippets = dynamic_prefix_combinator(&file_content)?;
hash2ordered_string(&snippets.snippets)
}
Err(e) => Err(TektonError::Reason(e.to_string())),
}
}