Skip to content

Commit 79adaa1

Browse files
authored
Add macro to remove code duplication (#111)
Streamlines how mutation row objects are created.
1 parent 159bbfe commit 79adaa1

File tree

8 files changed

+16
-49
lines changed

8 files changed

+16
-49
lines changed

src/_macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ macro_rules! decode_metadata_row {
168168
};
169169
}
170170

171+
macro_rules! table_row_decode_metadata {
172+
($decode_metadata: ident, $table: ident, $pos: ident) => {
173+
match $decode_metadata {
174+
true => metadata_to_vector!($table, $pos).unwrap().map(|x| x),
175+
false => None,
176+
}
177+
};
178+
}
179+
171180
macro_rules! process_state_input {
172181
($state: expr) => {
173182
match $state {

src/edge_table.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ fn make_edge_table_row(
3535
right: table.right(pos).unwrap(),
3636
parent: table.parent(pos).unwrap(),
3737
child: table.child(pos).unwrap(),
38-
metadata: match decode_metadata {
39-
true => match metadata_to_vector!(table, pos).unwrap() {
40-
Some(x) => Some(x),
41-
None => None,
42-
},
43-
false => None,
44-
},
38+
metadata: table_row_decode_metadata!(decode_metadata, table, pos),
4539
};
4640
Some(rv)
4741
} else {

src/individual_table.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,7 @@ fn make_individual_table_row(
5858
flags: table.flags(pos).unwrap(),
5959
location: table.location(pos).unwrap(),
6060
parents: table.parents(pos).unwrap(),
61-
metadata: match decode_metadata {
62-
true => match metadata_to_vector!(table, pos).unwrap() {
63-
Some(x) => Some(x),
64-
None => None,
65-
},
66-
false => None,
67-
},
61+
metadata: table_row_decode_metadata!(decode_metadata, table, pos),
6862
};
6963
Some(rv)
7064
} else {

src/migration_table.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ fn make_migration_table_row(
4141
source: table.source(pos).unwrap(),
4242
dest: table.dest(pos).unwrap(),
4343
time: table.time(pos).unwrap(),
44-
metadata: match decode_metadata {
45-
true => match metadata_to_vector!(table, pos).unwrap() {
46-
Some(x) => Some(x),
47-
None => None,
48-
},
49-
false => None,
50-
},
44+
metadata: table_row_decode_metadata!(decode_metadata, table, pos),
5145
})
5246
} else {
5347
None

src/mutation_table.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@ fn make_mutation_table_row(
3838
parent: table.parent(pos).unwrap(),
3939
time: table.time(pos).unwrap(),
4040
derived_state: table.derived_state(pos).unwrap(),
41-
metadata: match decode_metadata {
42-
true => match metadata_to_vector!(table, pos).unwrap() {
43-
Some(x) => Some(x),
44-
None => None,
45-
},
46-
false => None,
47-
},
41+
metadata: table_row_decode_metadata!(decode_metadata, table, pos),
4842
};
4943
Some(rv)
5044
} else {

src/node_table.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ fn make_node_table_row(
3535
flags: table.flags(pos).unwrap(),
3636
population: table.population(pos).unwrap(),
3737
individual: table.individual(pos).unwrap(),
38-
metadata: match decode_metadata {
39-
true => match metadata_to_vector!(table, pos).unwrap() {
40-
Some(x) => Some(x),
41-
None => None,
42-
},
43-
false => None,
44-
},
38+
metadata: table_row_decode_metadata!(decode_metadata, table, pos),
4539
})
4640
} else {
4741
None

src/population_table.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ fn make_population_table_row(
2424
if pos < table.num_rows() as tsk_id_t {
2525
let rv = PopulationTableRow {
2626
id: pos,
27-
metadata: match decode_metadata {
28-
true => match metadata_to_vector!(table, pos).unwrap() {
29-
Some(x) => Some(x),
30-
None => None,
31-
},
32-
false => None,
33-
},
27+
metadata: table_row_decode_metadata!(decode_metadata, table, pos),
3428
};
3529
Some(rv)
3630
} else {

src/site_table.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ fn make_site_table_row(
3030
id: pos,
3131
position: table.position(pos).unwrap(),
3232
ancestral_state: table.ancestral_state(pos).unwrap(),
33-
metadata: match decode_metadata {
34-
true => match metadata_to_vector!(table, pos).unwrap() {
35-
Some(x) => Some(x),
36-
None => None,
37-
},
38-
false => None,
39-
},
33+
metadata: table_row_decode_metadata!(decode_metadata, table, pos),
4034
};
4135
Some(rv)
4236
} else {

0 commit comments

Comments
 (0)