-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrust_match_big_enum.rs
174 lines (165 loc) · 6.34 KB
/
rust_match_big_enum.rs
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
use codesort::*;
static INPUT: &str = r#"
let con = &cc.app.con;
let screen = cc.app.screen;
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => CmdResult::PopState,
Internal::close_panel_ok => CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
},
Internal::close_panel_cancel => CmdResult::ClosePanel {
validate_purpose: false,
panel_ref: PanelReference::Active,
},
#[cfg(unix)]
Internal::filesystems => {
let fs_state = crate::filesystems::FilesystemState::new(
self.selected_path(),
self.tree_options(),
con,
);
match fs_state {
Ok(state) => {
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
if bang && cc.app.preview_panel.is_none() {
CmdResult::NewPanel {
state: Box::new(state),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::new_state(Box::new(state))
}
}
Err(e) => CmdResult::DisplayError(format!("{e}")),
}
}
Internal::mode_input => self.on_mode_verb(Mode::Input, con),
Internal::mode_command => self.on_mode_verb(Mode::Command, con),
Internal::open_leave => {
if let Some(selection) = self.selection() {
selection.to_opener(con)?
} else {
CmdResult::error("no selection to open")
}
}
Internal::open_preview => self.open_preview(None, false, cc),
Internal::preview_image => self.open_preview(Some(PreviewMode::Image), false, cc),
Internal::preview_text => self.open_preview(Some(PreviewMode::Text), false, cc),
Internal::preview_binary => self.open_preview(Some(PreviewMode::Hex), false, cc),
Internal::escape => {
CmdResult::HandleInApp(Internal::escape)
}
Internal::clear_stage => {
app_state.stage.clear();
if let Some(panel_id) = cc.app.stage_panel {
CmdResult::ClosePanel {
validate_purpose: false,
panel_ref: PanelReference::Id(panel_id),
}
} else {
CmdResult::Keep
}
}
Internal::stage => self.stage(app_state, cc, con),
Internal::unstage => self.unstage(app_state, cc, con),
Internal::toggle_stage => self.toggle_stage(app_state, cc, con),
_ => CmdResult::Keep,
})
"#;
static OUTPUT: &str = r#"
let con = &cc.app.con;
let screen = cc.app.screen;
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::back => CmdResult::PopState,
Internal::clear_stage => {
app_state.stage.clear();
if let Some(panel_id) = cc.app.stage_panel {
CmdResult::ClosePanel {
validate_purpose: false,
panel_ref: PanelReference::Id(panel_id),
}
} else {
CmdResult::Keep
}
}
Internal::close_panel_cancel => CmdResult::ClosePanel {
validate_purpose: false,
panel_ref: PanelReference::Active,
},
Internal::close_panel_ok => CmdResult::ClosePanel {
validate_purpose: true,
panel_ref: PanelReference::Active,
},
Internal::escape => {
CmdResult::HandleInApp(Internal::escape)
}
#[cfg(unix)]
Internal::filesystems => {
let fs_state = crate::filesystems::FilesystemState::new(
self.selected_path(),
self.tree_options(),
con,
);
match fs_state {
Ok(state) => {
let bang = input_invocation
.map(|inv| inv.bang)
.unwrap_or(internal_exec.bang);
if bang && cc.app.preview_panel.is_none() {
CmdResult::NewPanel {
state: Box::new(state),
purpose: PanelPurpose::None,
direction: HDir::Right,
}
} else {
CmdResult::new_state(Box::new(state))
}
}
Err(e) => CmdResult::DisplayError(format!("{e}")),
}
}
Internal::mode_command => self.on_mode_verb(Mode::Command, con),
Internal::mode_input => self.on_mode_verb(Mode::Input, con),
Internal::open_leave => {
if let Some(selection) = self.selection() {
selection.to_opener(con)?
} else {
CmdResult::error("no selection to open")
}
}
Internal::open_preview => self.open_preview(None, false, cc),
Internal::preview_binary => self.open_preview(Some(PreviewMode::Hex), false, cc),
Internal::preview_image => self.open_preview(Some(PreviewMode::Image), false, cc),
Internal::preview_text => self.open_preview(Some(PreviewMode::Text), false, cc),
Internal::stage => self.stage(app_state, cc, con),
Internal::toggle_stage => self.toggle_stage(app_state, cc, con),
Internal::unstage => self.unstage(app_state, cc, con),
_ => CmdResult::Keep,
})
"#;
#[test]
fn test_match_big_enum() {
let list = LocList::read_str(INPUT, Language::Rust).unwrap();
//list.print_debug(" WHOLE ");
let focused = list.focus_around_line_index(7).unwrap();
focused.print_debug();
{
let blocks = focused.clone().focus.into_blocks();
for (i, block) in blocks.iter().enumerate() {
block.print_debug(&format!(" BLOCK {i}"));
}
}
let sorted_list = focused.sort();
sorted_list.print_debug(" SORTED ");
assert_eq!(sorted_list.to_string(), OUTPUT);
}