/*
Hurry Curry! - a game about cooking
Copyright (C) 2025 Hurry Curry! Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
use crate::tr;
use hurrycurry_protocol::Message;
use std::fmt::{Debug, Display};
pub enum TrError {
Tr {
id: &'static str,
params: Vec,
},
Plain(String),
}
impl From for Message {
fn from(value: TrError) -> Self {
match value {
TrError::Tr { id, params } => Self::Translation {
id: id.to_owned(),
params,
},
TrError::Plain(s) => Self::Text(s),
}
}
}
impl Debug for TrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TrError::Tr { id, params } => write!(f, "{} {:?}", tr(id), params),
TrError::Plain(s) => write!(f, "{s}"),
}
}
}
impl Display for TrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TrError::Tr { id, params } => {
if params.is_empty() {
write!(f, "{}", tr(id))
} else {
let mut s = tr(id).to_string();
for (i, p) in params.iter().enumerate() {
s = s.replace(&format!("{{{i}}}"), &format!("{p:?}"));
}
write!(f, "{s}")
}
}
TrError::Plain(s) => write!(f, "{s}"),
}
}
}