1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
use std::io::Write; use serde_json::{to_writer, Result}; use std::borrow::Cow; use settings::{Field, SettingsDescription, Value}; use std::mem::replace; #[derive(Default, Clone)] pub struct Component { settings: Settings, } #[derive(Clone, Serialize, Deserialize)] #[serde(default)] pub struct Settings { pub text: Text, } #[derive(Clone, Serialize, Deserialize)] pub enum Text { Center(String), Split(String, String), } impl Text { pub fn set_center<S: Into<String>>(&mut self, text: S) { let text = text.into(); if let Text::Center(ref mut inner) = *self { *inner = text; } else { *self = Text::Center(text); } } pub fn set_left<S: Into<String>>(&mut self, text: S) { let text = text.into(); if let Text::Split(ref mut inner, _) = *self { *inner = text; } else { *self = Text::Split(text, String::from("")); } } pub fn set_right<S: Into<String>>(&mut self, text: S) { let text = text.into(); if let Text::Split(_, ref mut inner) = *self { *inner = text; } else { *self = Text::Split(String::from(""), text); } } } #[derive(Serialize, Deserialize)] pub struct State(pub Text); impl Default for Settings { fn default() -> Self { Self { text: Text::Center(String::from("")), } } } impl State { pub fn write_json<W>(&self, writer: W) -> Result<()> where W: Write, { to_writer(writer, self) } } impl Component { pub fn new() -> Self { Default::default() } pub fn with_settings(settings: Settings) -> Self { Self { settings, ..Default::default() } } pub fn settings(&self) -> &Settings { &self.settings } pub fn settings_mut(&mut self) -> &mut Settings { &mut self.settings } pub fn name(&self) -> Cow<str> { let name: Cow<str> = match self.settings.text { Text::Center(ref text) => text.as_str().into(), Text::Split(ref left, ref right) => { let mut name = String::with_capacity(left.len() + right.len() + 1); name.push_str(left); if !left.is_empty() && !right.is_empty() { name.push_str(" "); } name.push_str(right); name.into() } }; if name.trim().is_empty() { "Text".into() } else { name } } pub fn state(&self) -> State { State(self.settings.text.clone()) } pub fn settings_description(&self) -> SettingsDescription { let (first, second) = match self.settings.text { Text::Center(ref text) => (Field::new("Text".into(), text.to_string().into()), None), Text::Split(ref left, ref right) => ( Field::new("Left".into(), left.to_string().into()), Some(Field::new("Right".into(), right.to_string().into())), ), }; let mut fields = vec![Field::new("Split".into(), second.is_some().into()), first]; if let Some(second) = second { fields.push(second); } SettingsDescription::with_fields(fields) } pub fn set_value(&mut self, index: usize, value: Value) { match index { 0 => { self.settings.text = match (value.into_bool().unwrap(), &mut self.settings.text) { (true, &mut Text::Center(ref mut center)) => { Text::Split(replace(center, String::new()), String::new()) } (false, &mut Text::Split(ref mut left, ref mut right)) => { let mut value = replace(left, String::new()); let right = replace(right, String::new()); if !value.is_empty() && !right.is_empty() { value.push(' '); } value.push_str(&right); Text::Center(value) } _ => return, }; } 1 => match self.settings.text { Text::Center(ref mut center) => *center = value.into(), Text::Split(ref mut left, _) => *left = value.into(), }, 2 => match self.settings.text { Text::Center(_) => panic!("Set right text when there's only a center text"), Text::Split(_, ref mut right) => *right = value.into(), }, _ => panic!("Unsupported Setting Index"), } } }