From c55ab36c7bf41419914a445dd93c212d06fe3cb7 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 25 Jan 2024 16:50:55 -0800 Subject: [PATCH] Lazy evaluation on avail error list --- src/cosmic/bodies.rs | 64 +++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 40 deletions(-) diff --git a/src/cosmic/bodies.rs b/src/cosmic/bodies.rs index 090f2ef6..014b0b4e 100644 --- a/src/cosmic/bodies.rs +++ b/src/cosmic/bodies.rs @@ -88,28 +88,30 @@ impl Bodies { } } +fn avail() -> Vec { + 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 for Bodies { type Error = NyxError; fn try_from(name: String) -> Result { - 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), @@ -124,7 +126,7 @@ impl TryFrom for Bodies { "uranus" | "uranus barycenter" => Ok(Self::UranusBarycenter), "neptune" | "neptune barycenter" => Ok(Self::NeptuneBarycenter), "pluto" | "pluto barycenter" => Ok(Self::PlutoBarycenter), - _ => Err(NyxError::ObjectNotFound(name, avail)), + _ => Err(NyxError::ObjectNotFound(name, avail())), } } } @@ -133,24 +135,6 @@ impl TryFrom> for Bodies { type Error = NyxError; fn try_from(ephem_path: Vec) -> Result { - 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] { @@ -164,15 +148,15 @@ impl TryFrom> for Bodies { 7 => Ok(Self::UranusBarycenter), 8 => Ok(Self::NeptuneBarycenter), 9 => Ok(Self::PlutoBarycenter), - _ => Err(NyxError::ObjectNotFound(format!("{ephem_path:?}"), avail)), + _ => Err(NyxError::ObjectNotFound(format!("{ephem_path:?}"), avail())), }, 2 if ephem_path[0] == 3 => match ephem_path[1] { // This only support the Earth system 0 => Ok(Self::Earth), 1 => Ok(Self::Luna), - _ => Err(NyxError::ObjectNotFound(format!("{ephem_path:?}"), avail)), + _ => Err(NyxError::ObjectNotFound(format!("{ephem_path:?}"), avail())), }, - _ => Err(NyxError::ObjectNotFound(format!("{ephem_path:?}"), avail)), + _ => Err(NyxError::ObjectNotFound(format!("{ephem_path:?}"), avail())), } } }