use serde::{Deserialize, Serialize};
use terraphim_automata::LinkType as AutomataLinkType;
use terraphim_types::Thesaurus;
use thiserror::Error;
pub use terraphim_automata::LinkType;
#[derive(Error, Debug)]
pub enum ReplacementError {
#[error("Automata error: {0}")]
Automata(#[from] terraphim_automata::TerraphimAutomataError),
#[error("UTF-8 conversion error: {0}")]
Utf8(#[from] std::string::FromUtf8Error),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HookResult {
pub result: String,
pub original: String,
pub replacements: usize,
pub changed: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
impl HookResult {
pub fn success(original: String, result: String) -> Self {
let changed = original != result;
let replacements = if changed { 1 } else { 0 };
Self {
result,
original,
replacements,
changed,
error: None,
}
}
pub fn pass_through(original: String) -> Self {
Self {
result: original.clone(),
original,
replacements: 0,
changed: false,
error: None,
}
}
pub fn fail_open(original: String, error: String) -> Self {
Self {
result: original.clone(),
original,
replacements: 0,
changed: false,
error: Some(error),
}
}
}
pub struct ReplacementService {
thesaurus: Thesaurus,
link_type: AutomataLinkType,
}
impl ReplacementService {
pub fn new(thesaurus: Thesaurus) -> Self {
Self {
thesaurus,
link_type: AutomataLinkType::PlainText,
}
}
pub fn with_link_type(mut self, link_type: AutomataLinkType) -> Self {
self.link_type = link_type;
self
}
pub fn replace(&self, text: &str) -> Result<HookResult, ReplacementError> {
let result_bytes =
terraphim_automata::replace_matches(text, self.thesaurus.clone(), self.link_type)?;
let result = String::from_utf8(result_bytes)?;
Ok(HookResult::success(text.to_string(), result))
}
pub fn replace_fail_open(&self, text: &str) -> HookResult {
match self.replace(text) {
Ok(result) => result,
Err(e) => HookResult::fail_open(text.to_string(), e.to_string()),
}
}
pub fn find_matches(
&self,
text: &str,
) -> Result<Vec<terraphim_automata::Matched>, ReplacementError> {
Ok(terraphim_automata::find_matches(
text,
self.thesaurus.clone(),
true,
)?)
}
pub fn contains_matches(&self, text: &str) -> bool {
self.find_matches(text)
.map(|matches| !matches.is_empty())
.unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use terraphim_types::{NormalizedTerm, NormalizedTermValue};
fn create_test_thesaurus() -> Thesaurus {
let mut thesaurus = Thesaurus::new("test".to_string());
let bun_term = NormalizedTerm::new(1, NormalizedTermValue::from("bun"));
thesaurus.insert(NormalizedTermValue::from("npm"), bun_term.clone());
thesaurus.insert(NormalizedTermValue::from("yarn"), bun_term.clone());
thesaurus.insert(NormalizedTermValue::from("pnpm"), bun_term);
thesaurus
}
#[test]
fn test_replacement_service_basic() {
let thesaurus = create_test_thesaurus();
let service = ReplacementService::new(thesaurus);
let result = service.replace("npm install").unwrap();
assert!(result.changed);
assert_eq!(result.result, "bun install");
}
#[test]
fn test_replacement_service_no_match() {
let thesaurus = create_test_thesaurus();
let service = ReplacementService::new(thesaurus);
let result = service.replace("cargo build").unwrap();
assert!(!result.changed);
assert_eq!(result.result, "cargo build");
}
#[test]
fn test_hook_result_success() {
let result = HookResult::success("npm".to_string(), "bun".to_string());
assert!(result.changed);
assert_eq!(result.replacements, 1);
assert!(result.error.is_none());
}
#[test]
fn test_hook_result_pass_through() {
let result = HookResult::pass_through("unchanged".to_string());
assert!(!result.changed);
assert_eq!(result.replacements, 0);
assert_eq!(result.result, result.original);
}
#[test]
fn test_hook_result_fail_open() {
let result = HookResult::fail_open("original".to_string(), "error msg".to_string());
assert!(!result.changed);
assert_eq!(result.result, "original");
assert_eq!(result.error, Some("error msg".to_string()));
}
}