use std::borrow::Cow;
use std::cell::RefCell;
pub use super::helpers::{MessageResolution, safe_resolve_message_set};
use super::{Arguments, AttrKey, BundleLookup, I18nError, MessageKey};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FailingLookup {
message_key: Cow<'static, str>,
locale: Cow<'static, str>,
}
impl FailingLookup {
#[must_use]
pub fn new(message_key: impl Into<Cow<'static, str>>) -> Self {
Self::with_locale(message_key, Cow::Borrowed("test"))
}
#[must_use]
pub fn with_locale(
message_key: impl Into<Cow<'static, str>>,
locale: impl Into<Cow<'static, str>>,
) -> Self {
Self {
message_key: message_key.into(),
locale: locale.into(),
}
}
}
impl BundleLookup for FailingLookup {
fn message(&self, _key: MessageKey<'_>, _args: &Arguments<'_>) -> Result<String, I18nError> {
Err(I18nError::MissingMessage {
key: self.message_key.clone().into_owned(),
locale: self.locale.clone().into_owned(),
})
}
fn attribute(
&self,
_key: MessageKey<'_>,
_attribute: AttrKey<'_>,
_args: &Arguments<'_>,
) -> Result<String, I18nError> {
Err(I18nError::MissingMessage {
key: self.message_key.clone().into_owned(),
locale: self.locale.clone().into_owned(),
})
}
}
#[derive(Debug, Default)]
pub struct RecordingEmitter {
messages: RefCell<Vec<String>>,
}
impl RecordingEmitter {
#[must_use]
pub fn recorded_messages(&self) -> Vec<String> {
self.messages.borrow().clone()
}
pub fn record(&self, message: String) {
self.messages.borrow_mut().push(message);
}
}