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
use crate::catalog::osbsc::OSBSCStar;
#[allow(dead_code)] #[derive(Debug, Clone)]
pub struct Polyline<T> {
pub lines: Vec<T>,
}
#[allow(dead_code)] #[derive(Debug, Clone)]
pub struct Constellation<'a> {
pub name: Option<String>,
pub lines: Vec<Polyline<&'a OSBSCStar>>,
}
#[macro_export]
macro_rules! parse_constellation_catalog {
($path:expr, $stars:expr) => {{
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::Path;
let display = $path.display();
let file = match File::open(&$path) {
Err(why) => panic!(
"couldn't open {}: {}",
display,
<dyn Error>::to_string(&why)
),
Ok(file) => file,
};
let reader = BufReader::new(file);
let file_lines = reader.lines();
let mut constellations: Vec<Constellation> = [].to_vec();
for l in file_lines {
let mut poly_lines: Vec<Polyline<&OSBSCStar>> = vec![];
let stuff = l.unwrap();
let name_lines: Vec<&str> = stuff.split('=').collect();
let line_strings = name_lines[1].trim().split(';');
for line in line_strings {
let mut pline: Vec<&OSBSCStar> = vec![];
let ids = line.replace(&['[', ']'][..], "");
for id in ids.split(',') {
pline.push(&$stars.get(&id.trim().parse::<usize>().unwrap()).unwrap());
}
poly_lines.push(Polyline { lines: pline });
}
constellations.push(Constellation {
name: Some(String::from(name_lines[0].trim())),
lines: poly_lines,
})
}
constellations
}};
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::catalog::ValidParse;
use crate::constellation::*;
use crate::parse_catalog;
#[test]
#[ignore]
fn test_constellations_1() {
let data_file = "data/OSBSC/os-bright-star-catalog-hip.utf8";
if !std::path::Path::new(&data_file).exists() {
panic!("File \"{}\" doesn't exist. Please run \"get_data.sh\" to fetch the data required for this test.", &data_file)
};
let _stars = parse_catalog!(
OSBSCStar,
Path::new(&data_file),
None
);
let mut _star_map = HashMap::new();
for star in _stars {
_star_map.insert(star.Hipparcos_id.unwrap(), star);
}
let pline1 = Polyline {
lines: vec![
_star_map.get(&88).unwrap(),
_star_map.get(&107).unwrap(),
_star_map.get(&122).unwrap(),
],
};
let pline2 = Polyline {
lines: vec![
_star_map.get(&145).unwrap(),
_star_map.get(&194).unwrap(),
_star_map.get(&418).unwrap(),
],
};
let _con = Constellation {
name: Some(String::from("test")),
lines: vec![pline1, pline2],
};
println!("{:?}", _con);
}
#[test]
#[ignore]
fn test_constellations_2() {
let data_file = "data/OSBSC/os-bright-star-catalog-hip.utf8";
if !std::path::Path::new(&data_file).exists() {
panic!("File \"{}\" doesn't exist. Please run \"get_data.sh\" to fetch the data required for this test.", &data_file)
};
let _stars = parse_catalog!(
OSBSCStar,
Path::new(&data_file),
None
);
let mut _star_map = HashMap::new();
for star in _stars {
_star_map.insert(star.Hipparcos_id.unwrap(), star);
}
let data_file = "data/OSBSC/constellation-lines-hip.utf8";
if !std::path::Path::new(&data_file).exists() {
panic!("File \"{}\" doesn't exist. Please run \"get_data.sh\" to fetch the data required for this test.", &data_file)
};
let constells = parse_constellation_catalog!(Path::new(&data_file), _star_map);
println!("Number of Constellations: {}", constells.len());
println!("{:?}", constells.first());
}
}