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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use super::{Editor, SegmentRow, TimingMethod};
use crate::comparison::personal_best;
use crate::run::RunMetadata;
use crate::timing::formatter::none_wrapper::EmptyWrapper;
use crate::timing::formatter::{Accuracy, Short, TimeFormatter};
use crate::CachedImageId;
use serde_json::{to_writer, Result as JsonResult};
use std::io::Write;
#[derive(Debug, Serialize, Deserialize)]
pub struct State {
pub icon_change: Option<String>,
pub game: String,
pub category: String,
pub offset: String,
pub attempts: u32,
pub timing_method: TimingMethod,
pub segments: Vec<Segment>,
pub comparison_names: Vec<String>,
pub buttons: Buttons,
pub metadata: RunMetadata,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Buttons {
pub can_remove: bool,
pub can_move_up: bool,
pub can_move_down: bool,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Segment {
pub icon_change: Option<String>,
pub name: String,
pub split_time: String,
pub segment_time: String,
pub best_segment_time: String,
pub comparison_times: Vec<String>,
pub selected: SelectionState,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum SelectionState {
NotSelected,
Selected,
Active,
}
impl State {
pub fn write_json<W>(&self, writer: W) -> JsonResult<()>
where
W: Write,
{
to_writer(writer, self)
}
}
impl Editor {
pub fn state(&mut self) -> State {
let formatter = EmptyWrapper::new(Short::with_accuracy(Accuracy::Hundredths));
let icon_change = self
.game_icon_id
.update_with(self.run.game_icon().into())
.map(str::to_owned);
let game = self.game_name().to_string();
let category = self.category_name().to_string();
let offset = formatter.format(self.offset()).to_string();
let attempts = self.attempt_count();
let timing_method = self.selected_timing_method();
let comparison_names = self
.custom_comparisons()
.iter()
.cloned()
.filter(|n| n != personal_best::NAME)
.collect::<Vec<_>>();
let buttons = Buttons {
can_remove: self.can_remove_segments(),
can_move_up: self.can_move_segments_up(),
can_move_down: self.can_move_segments_down(),
};
let mut segments = Vec::with_capacity(self.run.len());
self.segment_icon_ids
.resize(self.run.len(), CachedImageId::default());
for segment_index in 0..self.run.len() {
let (name, split_time, segment_time, best_segment_time, comparison_times);
{
let row = SegmentRow::new(segment_index, self);
name = row.name().to_string();
split_time = formatter.format(row.split_time()).to_string();
segment_time = formatter.format(row.segment_time()).to_string();
best_segment_time = formatter.format(row.best_segment_time()).to_string();
comparison_times = comparison_names
.iter()
.map(|c| formatter.format(row.comparison_time(c)).to_string())
.collect();
}
let icon_change = self.segment_icon_ids[segment_index]
.update_with(self.run.segment(segment_index).icon().into())
.map(str::to_owned);
let selected = if self.active_segment_index() == segment_index {
SelectionState::Active
} else if self.selected_segments.contains(&segment_index) {
SelectionState::Selected
} else {
SelectionState::NotSelected
};
segments.push(Segment {
icon_change,
name,
split_time,
segment_time,
best_segment_time,
comparison_times,
selected,
});
}
State {
icon_change,
game,
category,
offset,
attempts,
timing_method,
segments,
comparison_names,
buttons,
metadata: self.run.metadata().clone(),
}
}
}