prompts_cli/
lib.rs

1use serde::{Deserialize, Serialize};
2use std::fs;
3use std::io;
4use async_trait::async_trait;
5use clap::ValueEnum;
6
7#[derive(Debug, Deserialize, Serialize, Clone)]
8pub struct Prompt {
9    pub name: String,
10    pub text: String,
11    pub tags: Vec<String>,
12    pub categories: Vec<String>,
13}
14
15#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
16pub enum GeneratorType {
17    /// Use a mock text generator
18    Mock,
19    /// Use a large language model (LLM) text generator
20    Llm,
21}
22
23#[async_trait]
24pub trait TextGenerator {
25    async fn generate(&self, prompt_text: &str) -> String;
26}
27
28pub struct MockTextGenerator;
29
30#[async_trait]
31impl TextGenerator for MockTextGenerator {
32    async fn generate(&self, prompt_text: &str) -> String {
33        format!("Generated text for '{}': {}\n(This is a mock generation)", prompt_text, prompt_text)
34    }
35}
36
37pub struct LLMTextGenerator;
38
39#[async_trait]
40impl TextGenerator for LLMTextGenerator {
41    async fn generate(&self, prompt_text: &str) -> String {
42        format!("Generated text for '{}' using LLM: {}\n(This is a placeholder for LLM API call)", prompt_text, prompt_text)
43    }
44}
45
46pub fn load_prompts(file_path: &str) -> Result<Vec<Prompt>, io::Error> {
47    let data = fs::read_to_string(file_path)?;
48    let prompts: Vec<Prompt> = serde_json::from_str(&data)?;
49    Ok(prompts)
50}
51
52pub fn save_prompts(file_path: &str, prompts: &[Prompt]) -> Result<(), io::Error> {
53    let data = serde_json::to_string_pretty(prompts)?;
54    fs::write(file_path, data)?;
55    Ok(())
56}
57
58pub fn search_prompts(prompts: &[Prompt], query: &str, tags: &[String], categories: &[String]) -> Vec<Prompt> {
59    let query_lower = query.to_lowercase();
60    prompts.iter().filter(|p| {
61        let name_lower = p.name.to_lowercase();
62        let text_lower = p.text.to_lowercase();
63
64        let matches_query = query.is_empty() || name_lower.contains(&query_lower) || text_lower.contains(&query_lower);
65        let matches_tags = tags.is_empty() || tags.iter().all(|t| p.tags.contains(t));
66        let matches_categories = categories.is_empty() || categories.iter().all(|c| p.categories.contains(c));
67
68        matches_query && matches_tags && matches_categories
69    }).cloned().collect()
70}