use crate::error::Error; use j_db::database::Database; use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize, Serialize, Clone)] pub struct SocialCreditPhrase { id: Option, pub phrase: String, pub lower_bound: i64, pub upper_bound: i64, } impl j_db::model::JdbModel for SocialCreditPhrase { fn id(&self) -> Option { self.id } fn set_id(&mut self, id: u64) { self.id = Some(id) } fn tree() -> String { "SocialCreditPhrase".to_string() } } impl SocialCreditPhrase { pub fn check_if_match(db: &Database, msg: &str) -> Result, Error> { let lowercase_msg = msg.to_lowercase(); let matches = db .filter(|_, phrase: &Self| lowercase_msg.contains(&phrase.phrase.to_lowercase()))? .next(); Ok(matches) } pub fn add_new_phrase( db: &Database, phrase: String, lower_bound: i64, upper_bound: i64, ) -> Result { let new_phrase = db.insert(Self { id: None, phrase, lower_bound, upper_bound, })?; Ok(new_phrase) } pub fn get_phrases(db: &Database) -> Result, Error> { let phrases = db.filter(|_, _phrase: &Self| true)?.collect(); Ok(phrases) } }