message_format/ast/
select_format.rs1use std::collections::HashMap;
8use std::fmt;
9use std::hash::Hash;
10
11use ast::Format;
12use {Args, Message};
13
14pub struct SelectFormat<K> {
16 #[allow(dead_code)]
18 variable_name: String,
19 #[allow(dead_code)]
21 mappings: HashMap<K, Message>,
22 default: Message,
25}
26
27impl<K> SelectFormat<K>
28 where K: Eq + Hash
29{
30 pub fn new(variable_name: &str, default: Message) -> Self {
32 SelectFormat {
33 variable_name: variable_name.to_string(),
34 mappings: HashMap::<K, Message>::new(),
35 default: default,
36 }
37 }
38
39 pub fn map(mut self, value: K, message: Message) -> Self {
41 self.mappings.insert(value, message);
42 self
43 }
44}
45
46impl<K> Format for SelectFormat<K> {
47 fn format_message_part(&self, stream: &mut fmt::Write, args: &Args) -> fmt::Result {
48 try!(self.default.format_message(stream, args));
49 Ok(())
50 }
51}