starstuff_types/
constellation.rs

1/*!
2WIP Open Source Constellation Catalog Parser
3
4> NOTE: still under construction!
5 */
6
7use crate::catalog::osbsc::OSBSCStar;
8
9/// Polyline
10#[allow(dead_code)] // FIXME
11#[derive(Debug, Clone)]
12pub struct Polyline<T> {
13    pub lines: Vec<T>,
14}
15
16/// Constellation
17#[allow(dead_code)] // FIXME
18#[derive(Debug, Clone)]
19
20pub struct Constellation<'a> {
21    pub name: Option<String>,
22    pub lines: Vec<Polyline<&'a OSBSCStar>>,
23}
24
25/// WIP Parse Open Source Constellation Catalog
26#[macro_export]
27macro_rules! parse_constellation_catalog {
28    ($path:expr, $stars:expr) => {{
29        use std::error::Error;
30        use std::fs::File;
31        use std::io::prelude::*;
32        use std::io::BufReader;
33        use std::path::Path;
34
35        let display = $path.display();
36
37        // Open the path in read-only mode, returns `io::Result<File>`
38        let file = match File::open(&$path) {
39            // The `description` method of `io::Error` returns a string that describes the error
40            Err(why) => panic!(
41                "couldn't open {}: {}",
42                display,
43                <dyn Error>::to_string(&why)
44            ),
45            Ok(file) => file,
46        };
47        let reader = BufReader::new(file);
48        let file_lines = reader.lines();
49        // lines is a instance of some type which implements Iterator<Item=&str>
50
51        let mut constellations: Vec<Constellation> = [].to_vec();
52
53        for l in file_lines {
54            let mut poly_lines: Vec<Polyline<&OSBSCStar>> = vec![];
55            let stuff = l.unwrap();
56            let name_lines: Vec<&str> = stuff.split('=').collect();
57            let line_strings = name_lines[1].trim().split(';');
58            for line in line_strings {
59                let mut pline: Vec<&OSBSCStar> = vec![];
60                let ids = line.replace(&['[', ']'][..], "");
61                for id in ids.split(',') {
62                    pline.push(&$stars.get(&id.trim().parse::<usize>().unwrap()).unwrap());
63                }
64                poly_lines.push(Polyline { lines: pline });
65            }
66            constellations.push(Constellation {
67                name: Some(String::from(name_lines[0].trim())),
68                lines: poly_lines,
69            })
70        }
71        constellations
72    }};
73}
74
75#[cfg(test)]
76mod tests {
77    use std::collections::HashMap;
78
79    use crate::catalog::ValidParse;
80    use crate::constellation::*;
81    use crate::parse_catalog;
82
83    #[test]
84    #[ignore]
85    fn constellations_1() {
86        let data_file = "data/OSBSC/os-bright-star-catalog-hip.utf8";
87
88        if !std::path::Path::new(&data_file).exists() {
89            panic!("File \"{}\" doesn't exist. Please run \"get_data.sh\" to fetch the data required for this test.", &data_file)
90        };
91
92        let _stars = parse_catalog!(
93            OSBSCStar,
94            Path::new(&data_file),
95            // NOTE: it seems like we don't need to pad this catalog even though it has no delimiters.
96            // In case it breaks in the future: Some(262)
97            None
98        );
99
100        let mut _star_map = HashMap::new();
101
102        for star in _stars {
103            _star_map.insert(star.Hipparcos_id.unwrap(), star);
104        }
105
106        let pline1 = Polyline {
107            lines: vec![
108                _star_map.get(&88).unwrap(),
109                _star_map.get(&107).unwrap(),
110                _star_map.get(&122).unwrap(),
111            ],
112        };
113
114        let pline2 = Polyline {
115            lines: vec![
116                _star_map.get(&145).unwrap(),
117                _star_map.get(&194).unwrap(),
118                _star_map.get(&418).unwrap(),
119            ],
120        };
121        let _con = Constellation {
122            name: Some(String::from("test")),
123            lines: vec![pline1, pline2],
124        };
125
126        println!("{:?}", _con);
127        // panic!()
128    }
129
130    #[test]
131    #[ignore]
132    fn constellations_2() {
133        let data_file = "data/OSBSC/os-bright-star-catalog-hip.utf8";
134
135        if !std::path::Path::new(&data_file).exists() {
136            panic!("File \"{}\" doesn't exist. Please run \"get_data.sh\" to fetch the data required for this test.", &data_file)
137        };
138
139        let _stars = parse_catalog!(
140            OSBSCStar,
141            Path::new(&data_file),
142            // NOTE: it seems like we don't need to pad this catalog even though it has no delimiters.
143            // In case it breaks in the future: Some(262)
144            None
145        );
146
147        let mut _star_map = HashMap::new();
148
149        for star in _stars {
150            _star_map.insert(star.Hipparcos_id.unwrap(), star);
151        }
152
153        let data_file = "data/OSBSC/constellation-lines-hip.utf8";
154
155        if !std::path::Path::new(&data_file).exists() {
156            panic!("File \"{}\" doesn't exist. Please run \"get_data.sh\" to fetch the data required for this test.", &data_file)
157        };
158
159        let constells = parse_constellation_catalog!(Path::new(&data_file), _star_map);
160
161        println!("Number of Constellations: {}", constells.len());
162        println!("{:?}", constells.first());
163        // panic!();
164    }
165}