Skip to main content

kimetsu_core/
memory.rs

1use std::fmt::{Display, Formatter};
2use std::str::FromStr;
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7#[serde(rename_all = "snake_case")]
8pub enum MemoryScope {
9    GlobalUser,
10    Project,
11    Repo,
12    Run,
13}
14
15impl Display for MemoryScope {
16    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17        let value = match self {
18            Self::GlobalUser => "global_user",
19            Self::Project => "project",
20            Self::Repo => "repo",
21            Self::Run => "run",
22        };
23        f.write_str(value)
24    }
25}
26
27impl FromStr for MemoryScope {
28    type Err = String;
29
30    fn from_str(value: &str) -> Result<Self, Self::Err> {
31        match value {
32            "global_user" | "global-user" | "user" => Ok(Self::GlobalUser),
33            "project" => Ok(Self::Project),
34            "repo" | "repository" => Ok(Self::Repo),
35            "run" => Ok(Self::Run),
36            _ => Err(format!("unknown memory scope: {value}")),
37        }
38    }
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
42#[serde(rename_all = "snake_case")]
43pub enum MemoryKind {
44    Preference,
45    Convention,
46    Command,
47    FailurePattern,
48    Fact,
49}
50
51impl Display for MemoryKind {
52    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53        let value = match self {
54            Self::Preference => "preference",
55            Self::Convention => "convention",
56            Self::Command => "command",
57            Self::FailurePattern => "failure_pattern",
58            Self::Fact => "fact",
59        };
60        f.write_str(value)
61    }
62}
63
64impl FromStr for MemoryKind {
65    type Err = String;
66
67    fn from_str(value: &str) -> Result<Self, Self::Err> {
68        match value {
69            "preference" => Ok(Self::Preference),
70            "convention" => Ok(Self::Convention),
71            "command" => Ok(Self::Command),
72            "failure_pattern" | "failure-pattern" => Ok(Self::FailurePattern),
73            "fact" => Ok(Self::Fact),
74            _ => Err(format!("unknown memory kind: {value}")),
75        }
76    }
77}
78
79pub fn normalize_memory_text(value: &str) -> String {
80    value
81        .trim()
82        .trim_end_matches(['.', '!', '?', ';', ':'])
83        .split_whitespace()
84        .collect::<Vec<_>>()
85        .join(" ")
86        .to_lowercase()
87}