use std::cmp::Ordering;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PositionChirho {
TopChirho,
BottomChirho,
}
pub trait SwKeyChirho: fmt::Display + fmt::Debug {
fn get_text_chirho(&self) -> &str;
fn set_text_chirho(&mut self, text_chirho: &str);
fn get_short_text_chirho(&self) -> String {
self.get_text_chirho().to_string()
}
fn get_osis_ref_chirho(&self) -> String {
self.get_text_chirho().to_string()
}
fn pop_error_chirho(&mut self) -> KeyErrorChirho;
fn has_error_chirho(&self) -> bool;
fn clone_key_chirho(&self) -> Box<dyn SwKeyChirho>;
fn get_index_chirho(&self) -> i64;
fn set_index_chirho(&mut self, index_chirho: i64);
fn is_traversable_chirho(&self) -> bool {
true
}
fn increment_chirho(&mut self, steps_chirho: i32);
fn decrement_chirho(&mut self, steps_chirho: i32);
fn set_position_chirho(&mut self, position_chirho: PositionChirho);
fn compare_chirho(&self, other_chirho: &dyn SwKeyChirho) -> Ordering;
fn equals_chirho(&self, other_chirho: &dyn SwKeyChirho) -> bool {
self.compare_chirho(other_chirho) == Ordering::Equal
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum KeyErrorChirho {
#[default]
NoneChirho,
OutOfBoundsChirho,
ParseErrorChirho,
NotFoundChirho,
}
impl KeyErrorChirho {
pub fn is_error_chirho(&self) -> bool {
!matches!(self, Self::NoneChirho)
}
}
#[derive(Debug, Clone, Default)]
pub struct SimpleKeyChirho {
text_chirho: String,
error_chirho: KeyErrorChirho,
index_chirho: i64,
}
impl SimpleKeyChirho {
pub fn new_chirho(text_chirho: impl Into<String>) -> Self {
Self {
text_chirho: text_chirho.into(),
error_chirho: KeyErrorChirho::NoneChirho,
index_chirho: 0,
}
}
}
impl fmt::Display for SimpleKeyChirho {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.text_chirho)
}
}
impl SwKeyChirho for SimpleKeyChirho {
fn get_text_chirho(&self) -> &str {
&self.text_chirho
}
fn set_text_chirho(&mut self, text_chirho: &str) {
self.text_chirho = text_chirho.to_string();
self.error_chirho = KeyErrorChirho::NoneChirho;
}
fn pop_error_chirho(&mut self) -> KeyErrorChirho {
let error_chirho = self.error_chirho;
self.error_chirho = KeyErrorChirho::NoneChirho;
error_chirho
}
fn has_error_chirho(&self) -> bool {
self.error_chirho.is_error_chirho()
}
fn clone_key_chirho(&self) -> Box<dyn SwKeyChirho> {
Box::new(self.clone())
}
fn get_index_chirho(&self) -> i64 {
self.index_chirho
}
fn set_index_chirho(&mut self, index_chirho: i64) {
self.index_chirho = index_chirho;
}
fn increment_chirho(&mut self, _steps_chirho: i32) {
self.error_chirho = KeyErrorChirho::OutOfBoundsChirho;
}
fn decrement_chirho(&mut self, _steps_chirho: i32) {
self.error_chirho = KeyErrorChirho::OutOfBoundsChirho;
}
fn set_position_chirho(&mut self, _position_chirho: PositionChirho) {
}
fn is_traversable_chirho(&self) -> bool {
false
}
fn compare_chirho(&self, other_chirho: &dyn SwKeyChirho) -> Ordering {
self.text_chirho.cmp(&other_chirho.get_text_chirho().to_string())
}
}
#[cfg(test)]
mod tests_chirho {
use super::*;
#[test]
fn test_simple_key_chirho() {
let mut key_chirho = SimpleKeyChirho::new_chirho("test");
assert_eq!(key_chirho.get_text_chirho(), "test");
assert!(!key_chirho.has_error_chirho());
key_chirho.set_text_chirho("new text");
assert_eq!(key_chirho.get_text_chirho(), "new text");
}
#[test]
fn test_key_error_chirho() {
let mut key_chirho = SimpleKeyChirho::new_chirho("test");
key_chirho.increment_chirho(1);
assert!(key_chirho.has_error_chirho());
let error_chirho = key_chirho.pop_error_chirho();
assert_eq!(error_chirho, KeyErrorChirho::OutOfBoundsChirho);
assert!(!key_chirho.has_error_chirho());
}
#[test]
fn test_key_comparison_chirho() {
let key1_chirho = SimpleKeyChirho::new_chirho("aaa");
let key2_chirho = SimpleKeyChirho::new_chirho("bbb");
assert_eq!(key1_chirho.compare_chirho(&key2_chirho), Ordering::Less);
assert!(key1_chirho.equals_chirho(&SimpleKeyChirho::new_chirho("aaa")));
}
}