Added isolang as a message field.

This commit is contained in:
2025-05-20 12:56:59 -04:00
parent ac0df7ae1f
commit 2c9b08c77d
4 changed files with 108 additions and 1 deletions

View File

@@ -11,6 +11,27 @@ use std::{
const RESPONDS_TO: [MsgType; 1] = [MsgType::DocumentRequest];
struct Table;
impl Table {
fn new() -> Self {
Self {}
}
fn add_column(&self) {}
}
#[cfg(test)]
mod tables {
use super::*;
#[test]
fn create_table() {
let tbl = Table::new();
tbl.add_column();
}
}
pub struct Document {
data: HashMap<String, String>,
queue: Queue,

View File

@@ -1,12 +1,13 @@
use crate::{ActionType, ErrorType};
use chrono::prelude::*;
use isolang::Language;
use std::fmt;
use uuid::Uuid;
#[derive(Clone, Debug, PartialEq)]
enum FieldType {
Action,
DateTime,
DateTime,
Error,
StaticString,
Uuid,
@@ -17,6 +18,7 @@ pub enum Field {
Action(ActionType),
DateTime(DateTime<Utc>),
ErrorType(ErrorType),
Lang(Language),
Static(String),
Uuid(Uuid),
}
@@ -49,6 +51,13 @@ impl Field {
_ => Err("not an error type".to_string()),
}
}
pub fn to_language(&self) -> Result<Language, String> {
match self {
Field::Lang(data) => Ok(data.clone()),
_ => Err("not an error type".to_string()),
}
}
}
impl From<String> for Field {
@@ -95,6 +104,12 @@ impl From<ErrorType> for Field {
}
}
impl From<Language> for Field {
fn from(value: Language) -> Self {
Field::Lang(value)
}
}
impl fmt::Display for Field {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -296,4 +311,40 @@ mod fields {
Err(_) => {}
}
}
#[test]
fn from_lang_to_field() {
let langs = [
Language::from_639_1("en").unwrap(),
Language::from_639_1("ja").unwrap(),
];
for lang in langs.into_iter() {
let field: Field = lang.into();
match field {
Field::Lang(data) => assert_eq!(data, lang),
_ => unreachable!("should identify language"),
}
}
}
#[test]
fn from_field_to_lang() {
let langs = [
Language::from_639_1("en").unwrap(),
Language::from_639_1("ja").unwrap(),
];
for lang in langs.into_iter() {
let field: Field = lang.into();
assert_eq!(field.to_language().unwrap(), lang);
}
}
#[test]
fn wrong_field_to_lang() {
let field: Field = Uuid::nil().into();
match field.to_language() {
Ok(_) => unreachable!("should have produced an error"),
Err(_) => {}
}
}
}