| 1 | use crate::message::{Message, MessageFlags}; |
| 2 | |
| 3 | /// A helper type to build a `Message` conveniently via method chaining. |
| 4 | pub struct MessageBuilder { |
| 5 | m: Message, |
| 6 | } |
| 7 | |
| 8 | impl Message { |
| 9 | /// Build a new singular message. |
| 10 | pub fn build_singular() -> MessageBuilder { |
| 11 | MessageBuilder { |
| 12 | m: Message { |
| 13 | is_plural: false, |
| 14 | ..Message::default() |
| 15 | }, |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /// Build a new plural message. |
| 20 | pub fn build_plural() -> MessageBuilder { |
| 21 | MessageBuilder { |
| 22 | m: Message { |
| 23 | is_plural: true, |
| 24 | ..Message::default() |
| 25 | }, |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl MessageBuilder { |
| 31 | /// Set the comments field. |
| 32 | pub fn with_comments(&mut self, comments: String) -> &mut Self { |
| 33 | self.m.comments = comments; |
| 34 | self |
| 35 | } |
| 36 | |
| 37 | /// Set the source field. |
| 38 | pub fn with_source(&mut self, source: String) -> &mut Self { |
| 39 | self.m.source = source; |
| 40 | self |
| 41 | } |
| 42 | |
| 43 | /// Set the flags field. |
| 44 | pub fn with_flags(&mut self, flags: MessageFlags) -> &mut Self { |
| 45 | self.m.flags = flags; |
| 46 | self |
| 47 | } |
| 48 | |
| 49 | /// Set the msgctxt field. |
| 50 | pub fn with_msgctxt(&mut self, msgctxt: String) -> &mut Self { |
| 51 | self.m.msgctxt = msgctxt; |
| 52 | self |
| 53 | } |
| 54 | |
| 55 | /// Set the msgid field. |
| 56 | pub fn with_msgid(&mut self, msgid: String) -> &mut Self { |
| 57 | self.m.msgid = msgid; |
| 58 | self |
| 59 | } |
| 60 | |
| 61 | /// Set the msgid_plural field. |
| 62 | pub fn with_msgid_plural(&mut self, msgid_plural: String) -> &mut Self { |
| 63 | self.m.msgid_plural = msgid_plural; |
| 64 | self |
| 65 | } |
| 66 | |
| 67 | /// Set the msgstr field. |
| 68 | pub fn with_msgstr(&mut self, msgstr: String) -> &mut Self { |
| 69 | self.m.msgstr = msgstr; |
| 70 | self |
| 71 | } |
| 72 | |
| 73 | /// Set the msgstr_plural field. |
| 74 | pub fn with_msgstr_plural(&mut self, msgstr_plural: Vec<String>) -> &mut Self { |
| 75 | self.m.msgstr_plural = msgstr_plural; |
| 76 | self |
| 77 | } |
| 78 | |
| 79 | /// Finish building and get the resulting `Message` object. |
| 80 | /// This builder object should be discarded and not be re-used afterwards. |
| 81 | pub fn done(&mut self) -> Message { |
| 82 | std::mem::take(&mut self.m) |
| 83 | } |
| 84 | } |
| 85 | |