use crate::models::snippet_model::Snippet;
use anyhow::Context;
use prettytable::{row, Table};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
pub fn show_snippet(file_path: &str, key: String) -> anyhow::Result<String> {
let path = Path::new(file_path);
let snippets: HashMap<String, Snippet> = if path.exists() && path.metadata()?.len() > 0 {
let file_contents =
fs::read_to_string(file_path).context("Failed to read the snippets file")?;
serde_json::from_str(&file_contents).context("Failed to parse the snippets file")?
} else {
return Err(anyhow::anyhow!("Snippet file not found or is empty"));
};
if let Some(snippet) = snippets.get(&key) {
let mut table = Table::new();
table.add_row(row!["Key", key]);
table.add_row(row!["Prefix", snippet.prefix]);
table.add_row(row!["Description", snippet.description]);
table.add_row(row!["Body", snippet.body.join("\n")]);
Ok(table.to_string())
} else {
Err(anyhow::anyhow!("Snippet with key '{}' not found", key))
}
}
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
use std::io::{Seek, SeekFrom, Write};
use tempfile::NamedTempFile;
#[test]
fn test_show_snippet() -> Result<()> {
let mut temp_file = NamedTempFile::new()?;
let mut test_snippets = HashMap::new();
test_snippets.insert(
"test_key".to_string(),
Snippet {
prefix: "test_prefix".to_string(),
body: vec!["test_body".to_string()],
description: "test_description".to_string(),
},
);
let test_data = serde_json::to_string(&test_snippets)?;
write!(temp_file, "{}", test_data)?;
temp_file.flush()?; temp_file.seek(SeekFrom::Start(0))?;
let file_path = temp_file.path().to_str().unwrap();
let result = show_snippet(file_path, "test_key".to_string())?;
let mut expected_table = Table::new();
expected_table.add_row(row!["Key", "test_key"]);
expected_table.add_row(row!["Prefix", "test_prefix"]);
expected_table.add_row(row!["Description", "test_description"]);
expected_table.add_row(row!["Body", "test_body"]);
let expected_output = expected_table.to_string();
assert_eq!(result, expected_output);
Ok(())
}
}