Skip to content

Commit 66cb359

Browse files
author
Stephan Dilly
committed
simplify code using macro
1 parent 1991a20 commit 66cb359

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/app.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ enum Focus {
4242
Stage,
4343
}
4444

45+
/// allows generating code to make sure
46+
/// we always enumerate all components in both getter functions
47+
macro_rules! components {
48+
($self:ident, [$($element:ident),+]) => {
49+
fn components(& $self) -> Vec<&dyn Component> {
50+
vec![
51+
$(&$self.$element,)+
52+
]
53+
}
54+
55+
fn components_mut(&mut $self) -> Vec<&mut dyn Component> {
56+
vec![
57+
$(&mut $self.$element,)+
58+
]
59+
}
60+
};
61+
}
62+
4563
///
4664
pub struct App {
4765
focus: Focus,
@@ -251,6 +269,11 @@ impl App {
251269

252270
// private impls
253271
impl App {
272+
components!(
273+
self,
274+
[msg, reset, commit, help, index, index_wd, diff]
275+
);
276+
254277
fn update_diff(&mut self) {
255278
if let Some((path, is_stage)) = self.selected_path() {
256279
let diff_params = DiffParams(path.clone(), is_stage);
@@ -471,30 +494,6 @@ impl App {
471494
&& self.diff_target == DiffTarget::Stage
472495
}
473496

474-
fn components(&self) -> Vec<&dyn Component> {
475-
vec![
476-
&self.msg,
477-
&self.reset,
478-
&self.commit,
479-
&self.help,
480-
&self.index,
481-
&self.index_wd,
482-
&self.diff,
483-
]
484-
}
485-
486-
fn components_mut(&mut self) -> Vec<&mut dyn Component> {
487-
vec![
488-
&mut self.msg,
489-
&mut self.reset,
490-
&mut self.commit,
491-
&mut self.help,
492-
&mut self.index,
493-
&mut self.index_wd,
494-
&mut self.diff,
495-
]
496-
}
497-
498497
fn event_pump(
499498
ev: Event,
500499
components: &mut [&mut dyn Component],

0 commit comments

Comments
 (0)