doris_rs/
production.rs

1use crate::error::ParsingError;
2
3/// This structure is attached to DORIS file that were named
4/// according to the standard convention.
5#[derive(Debug, Default, Clone, PartialEq)]
6pub struct ProductionAttributes {
7    /// 3 letter satellite name
8    pub satellite: String,
9
10    /// Year of production
11    pub year: u32,
12
13    /// Production Day of Year (DOY), assumed past J2000.
14    pub doy: u32,
15
16    /// True if this file was gzip compressed
17    #[cfg(feature = "flate2")]
18    #[cfg_attr(docsrs, doc(cfg(feature = "flate2")))]
19    pub gzip_compressed: bool,
20}
21
22impl std::fmt::Display for ProductionAttributes {
23    #[cfg(feature = "flate2")]
24    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
25        Ok(())
26    }
27
28    #[cfg(not(feature = "flate2"))]
29    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30        Ok(())
31    }
32}
33
34impl std::str::FromStr for ProductionAttributes {
35    type Err = ParsingError;
36
37    fn from_str(filename: &str) -> Result<Self, Self::Err> {
38        let filename = filename.to_uppercase();
39
40        let name_len = filename.len();
41
42        if name_len != 12 && name_len != 15 {
43            return Err(ParsingError::NonStandardFileName);
44        }
45
46        let doy = 0;
47        let satellite = "Undefined".to_string();
48
49        let offset = filename.find('.').unwrap_or(0);
50
51        let agency = filename[..3].to_string();
52
53        let year = filename[offset + 1..offset + 3]
54            .parse::<u32>()
55            .map_err(|_| ParsingError::NonStandardFileName)?;
56
57        Ok(Self {
58            satellite,
59            year,
60            doy,
61            #[cfg(feature = "flate2")]
62            gzip_compressed: filename.ends_with(".GZ"),
63        })
64    }
65}
66
67#[cfg(test)]
68mod test {
69    use super::*;
70    use std::str::FromStr;
71}