Skip to content

Commit

Permalink
Merge pull request #274 from cmleinz/tweaks
Browse files Browse the repository at this point in the history
Lazy evaluation on avail error list
  • Loading branch information
ChristopherRabotin committed Jan 26, 2024
2 parents b21c88d + 8745ca7 commit 9b2ed84
Showing 1 changed file with 24 additions and 40 deletions.
64 changes: 24 additions & 40 deletions src/cosmic/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,30 @@ impl Bodies {
}
}

fn avail() -> Vec<String> {
vec![
"SSB".to_string(),
"Sun".to_string(),
"MercuryBarycenter".to_string(),
"Mercury".to_string(),
"VenusBarycenter".to_string(),
"Venus".to_string(),
"EarthBarycenter".to_string(),
"Earth".to_string(),
"Luna".to_string(),
"MarsBarycenter".to_string(),
"JupiterBarycenter".to_string(),
"SaturnBarycenter".to_string(),
"UranusBarycenter".to_string(),
"NeptuneBarycenter".to_string(),
"PlutoBarycenter".to_string(),
]
}

impl TryFrom<String> for Bodies {
type Error = NyxError;

fn try_from(name: String) -> Result<Self, Self::Error> {
let avail = vec![
"SSB".to_string(),
"Sun".to_string(),
"MercuryBarycenter".to_string(),
"Mercury".to_string(),
"VenusBarycenter".to_string(),
"Venus".to_string(),
"EarthBarycenter".to_string(),
"Earth".to_string(),
"Luna".to_string(),
"MarsBarycenter".to_string(),
"JupiterBarycenter".to_string(),
"SaturnBarycenter".to_string(),
"UranusBarycenter".to_string(),
"NeptuneBarycenter".to_string(),
"PlutoBarycenter".to_string(),
];

match name.to_lowercase().as_str() {
"solar system barycenter" | "ssb" => Ok(Self::SSB),
"sun" => Ok(Self::Sun),
Expand All @@ -126,7 +128,7 @@ impl TryFrom<String> for Bodies {
"pluto" | "pluto barycenter" => Ok(Self::PlutoBarycenter),
_ => Err(NyxError::ObjectNotFound {
needle: name,
haystack: avail,
haystack: avail(),
}),
}
}
Expand All @@ -136,24 +138,6 @@ impl TryFrom<Vec<usize>> for Bodies {
type Error = NyxError;

fn try_from(ephem_path: Vec<usize>) -> Result<Self, Self::Error> {
let avail = vec![
"SSB".to_string(),
"Sun".to_string(),
"MercuryBarycenter".to_string(),
"Mercury".to_string(),
"VenusBarycenter".to_string(),
"Venus".to_string(),
"EarthBarycenter".to_string(),
"Earth".to_string(),
"Luna".to_string(),
"MarsBarycenter".to_string(),
"JupiterBarycenter".to_string(),
"SaturnBarycenter".to_string(),
"UranusBarycenter".to_string(),
"NeptuneBarycenter".to_string(),
"PlutoBarycenter".to_string(),
];

match ephem_path.len() {
0 => Ok(Self::SSB),
1 => match ephem_path[0] {
Expand All @@ -169,7 +153,7 @@ impl TryFrom<Vec<usize>> for Bodies {
9 => Ok(Self::PlutoBarycenter),
_ => Err(NyxError::ObjectNotFound {
needle: format!("{ephem_path:?}"),
haystack: avail,
haystack: avail(),
}),
},
2 if ephem_path[0] == 3 => match ephem_path[1] {
Expand All @@ -178,12 +162,12 @@ impl TryFrom<Vec<usize>> for Bodies {
1 => Ok(Self::Luna),
_ => Err(NyxError::ObjectNotFound {
needle: format!("{ephem_path:?}"),
haystack: avail,
haystack: avail(),
}),
},
_ => Err(NyxError::ObjectNotFound {
needle: format!("{ephem_path:?}"),
haystack: avail,
haystack: avail(),
}),
}
}
Expand Down

0 comments on commit 9b2ed84

Please sign in to comment.