serde_valid/validation/error/
message.rs1use super::{Format, FormatDefault};
2
3#[derive(Debug, Clone)]
4pub struct Message<E> {
5 error: E,
6 format: Format<E>,
7}
8
9impl<E> Message<E> {
10 pub fn new(error: E, format: Format<E>) -> Self {
11 Self { error, format }
12 }
13
14 #[cfg(feature = "fluent")]
15 pub fn fluent_message(&self) -> Option<&crate::features::fluent::Message> {
16 match self.format {
17 Format::Fluent(ref message) => Some(message),
18 _ => None,
19 }
20 }
21}
22
23impl<E> FormatDefault for Message<E>
24where
25 E: FormatDefault,
26{
27 fn format_default(&self) -> String {
28 match &self.format {
29 Format::Default => self.error.format_default(),
30 Format::Message(ref message) => message.to_string(),
31 Format::MessageFn(ref format_fn) => format_fn(&self.error),
32 #[cfg(feature = "fluent")]
33 Format::Fluent(message) => format!("{message}"),
34 }
35 }
36}
37
38impl<E> std::fmt::Display for Message<E>
39where
40 E: FormatDefault,
41{
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 write!(f, "{}", self.format_default())
44 }
45}