| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use std::collections::{BTreeMap, BTreeSet}; |
| 24 |
|
| 25 |
use quasi_router::{Node, Params, Screen}; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 33 |
pub struct View { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
edits: BTreeMap<String, String>, |
| 40 |
|
| 41 |
ticked: BTreeSet<String>, |
| 42 |
} |
| 43 |
|
| 44 |
impl View { |
| 45 |
|
| 46 |
#[must_use] |
| 47 |
pub fn new() -> Self { |
| 48 |
Self::default() |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
#[must_use] |
| 53 |
pub fn showing<'a>(&'a self, name: &str, described: Option<&'a str>) -> &'a str { |
| 54 |
self.edits |
| 55 |
.get(name) |
| 56 |
.map_or(described.unwrap_or_default(), String::as_str) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
pub fn buffer(&mut self, name: &str, described: Option<&str>) -> &mut String { |
| 66 |
self.edits |
| 67 |
.entry(name.to_owned()) |
| 68 |
.or_insert_with(|| described.unwrap_or_default().to_owned()) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
#[must_use] |
| 73 |
pub fn edit(&self, name: &str) -> Option<&str> { |
| 74 |
self.edits.get(name).map(String::as_str) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
pub fn set(&mut self, name: impl Into<String>, value: impl Into<String>) { |
| 79 |
self.edits.insert(name.into(), value.into()); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
#[must_use] |
| 84 |
pub fn is_ticked(&self, value: &str) -> bool { |
| 85 |
self.ticked.contains(value) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
pub fn tick(&mut self, value: &str) { |
| 90 |
if !self.ticked.remove(value) { |
| 91 |
self.ticked.insert(value.to_owned()); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
pub fn ticks(&self) -> impl Iterator<Item = &str> { |
| 97 |
self.ticked.iter().map(String::as_str) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub fn seed(&mut self, screen: &Screen) { |
| 106 |
for slot in &screen.slots { |
| 107 |
for node in &slot.body { |
| 108 |
if let Node::List { rows, .. } = node { |
| 109 |
for row in rows { |
| 110 |
if let (Some(true), Some(value)) = (row.selected, row.value.as_ref()) { |
| 111 |
self.ticked.insert(value.clone()); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
pub fn reset(&mut self) { |
| 121 |
self.edits.clear(); |
| 122 |
self.ticked.clear(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
#[must_use] |
| 131 |
pub fn submission(&self, names: &[String], described: &BTreeMap<String, String>) -> Params { |
| 132 |
let mut params = Params::new(); |
| 133 |
for name in names { |
| 134 |
let value = self |
| 135 |
.edits |
| 136 |
.get(name) |
| 137 |
.or_else(|| described.get(name)) |
| 138 |
.map_or("", String::as_str); |
| 139 |
params = params.with(name.clone(), value.to_owned()); |
| 140 |
} |
| 141 |
params |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
#[must_use] |
| 146 |
pub fn gathering(&self, under: &str) -> Params { |
| 147 |
let mut params = Params::new(); |
| 148 |
for value in self.ticks() { |
| 149 |
params = params.with(under.to_owned(), value.to_owned()); |
| 150 |
} |
| 151 |
params |
| 152 |
} |
| 153 |
} |
| 154 |
|