libdit: handle errors in dit input format
This commit is contained in:
parent
17cb1b29f7
commit
78bbf29805
|
@ -1,4 +1,5 @@
|
|||
pub mod libdit {
|
||||
use std::process::exit;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// Represents a time in a StarDIT day
|
||||
|
@ -12,27 +13,50 @@ pub mod libdit {
|
|||
type Err = std::num::ParseIntError;
|
||||
|
||||
fn from_str(dit: &str) -> Result<Self, Self::Err> {
|
||||
return Ok(Dit {
|
||||
deca: u16::from_str_radix(&dit[0..1], 10)?,
|
||||
decim: u16::from_str_radix(&dit[2..4], 10)?,
|
||||
desec: u16::from_str_radix(&dit[5..7], 10)?,
|
||||
});
|
||||
if dit.len() != 7 {
|
||||
eprintln!("Wrong DIT length");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Ok(Dit {
|
||||
deca: match dit[0..1].parse() {
|
||||
Ok(deca) => deca,
|
||||
Err(_) => {
|
||||
eprintln!("Wrong deca");
|
||||
exit(1);
|
||||
}
|
||||
},
|
||||
decim: match dit[2..4].parse() {
|
||||
Ok(decim) => decim,
|
||||
Err(_) => {
|
||||
eprintln!("Wrong decim");
|
||||
exit(1);
|
||||
}
|
||||
},
|
||||
desec: match dit[5..7].parse() {
|
||||
Ok(desec) => desec,
|
||||
Err(_) => {
|
||||
eprintln!("Wrong desec");
|
||||
exit(1);
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns relative progress in the day, between 0 and 1 corresponding to `unix`
|
||||
pub fn unix_to_dit(unix: f64) -> f64 {
|
||||
return (unix - (12.0 * 3600.0)) % 86400.0 / 86400.0;
|
||||
(unix - (12.0 * 3600.0)) % 86400.0 / 86400.0
|
||||
}
|
||||
|
||||
/// Returns a struct containing deca, decim and desec corresponding to `unix`
|
||||
pub fn unix_to_dit_struct(unix: f64) -> Dit {
|
||||
let dit = unix_to_dit(unix);
|
||||
return Dit {
|
||||
Dit {
|
||||
deca: (dit * 10.0 % 10.0) as u16,
|
||||
decim: (dit * 1000.0 % 100.0) as u16,
|
||||
desec: (dit * 100000.0 % 100.0) as u16,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a string containing "deca.decim.desec" corresponding to `unix`
|
||||
|
@ -45,11 +69,6 @@ pub mod libdit {
|
|||
/// ```
|
||||
pub fn unix_to_dit_string(unix: f64) -> String {
|
||||
let dit = unix_to_dit_struct(unix);
|
||||
return format!(
|
||||
"{}.{}.{}",
|
||||
dit.deca,
|
||||
format!("{:0>2}", dit.decim),
|
||||
format!("{:0>2}", dit.desec)
|
||||
);
|
||||
format!("{}.{:0>2}.{:0>2}", dit.deca, dit.decim, dit.desec,)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue