1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
use error::{HcResult, HolochainError}; lazy_static! { pub static ref HDK_VERSION: HDKVersion = { let version = env!( "HDK_VERSION", "failed to obtain hdk version from build environment. Check build.rs" ); HDKVersion::new(version).unwrap_or_else(|_| { panic!("Failed to create HDK Version '{}'. Check Build.rs", version) }) }; } #[derive(Clone, PartialEq, Eq)] pub enum Lifecycle { Beta(i16), Alpha(i16), Stable(i16), } #[derive(Clone, PartialEq, Eq)] pub struct HDKVersion { versioning: (i16, i16, i16), lifecycle: Lifecycle, } fn get_lifecycle(lifecycle_string: &str) -> HcResult<Lifecycle> { if lifecycle_string.contains("beta") { Ok(Lifecycle::Beta( lifecycle_string .split("beta") .nth(1) .ok_or("Could not get beta version")? .parse::<i16>() .map_err(|_| HolochainError::ErrorGeneric("Could not parse version".to_string()))?, )) } else if lifecycle_string.contains("stable") { Ok(Lifecycle::Stable( lifecycle_string .split("stable") .nth(1) .ok_or("Could not get stable version")? .parse::<i16>() .map_err(|_| HolochainError::ErrorGeneric("Could not parse version".to_string()))?, )) } else if lifecycle_string.contains("alpha") { Ok(Lifecycle::Alpha( lifecycle_string .split("alpha") .nth(1) .ok_or("Could not get alpha version")? .parse::<i16>() .map_err(|_| HolochainError::ErrorGeneric("Could not parse version".to_string()))?, )) } else { Err(HolochainError::ErrorGeneric( "Invalid Lifecycle Version".to_string(), )) } } impl ToString for HDKVersion { fn to_string(&self) -> String { let version = vec![ self.versioning.0.to_string(), self.versioning.1.to_string(), self.versioning.2.to_string(), ] .join("."); let life_cycle = match self.lifecycle { Lifecycle::Alpha(num) => vec!["alpha", &num.to_string()].join(""), Lifecycle::Beta(num) => vec!["beta", &num.to_string()].join(""), Lifecycle::Stable(num) => vec!["stable", &num.to_string()].join(""), }; vec![version, life_cycle].join("-") } } impl HDKVersion { pub fn new(version_string: &str) -> HcResult<HDKVersion> { let mut splits = version_string.split('-'); let version = splits.next().ok_or("Could not get version")?; let mut version_splits = version.trim_start_matches('v').split('.'); let versioning = ( version_splits .next() .ok_or("Could not get version")? .parse::<i16>() .map_err(|_| HolochainError::ErrorGeneric("Could not parse version".to_string()))?, version_splits .next() .ok_or("Could not get version")? .parse::<i16>() .map_err(|_| HolochainError::ErrorGeneric("Could not parse version".to_string()))?, version_splits .next() .ok_or("Could not get version")? .parse::<i16>() .map_err(|_| HolochainError::ErrorGeneric("Could not parse version".to_string()))?, ); let lifecycle = get_lifecycle(splits.next().ok_or("Could not get lifecycle")?)?; Ok(HDKVersion { versioning, lifecycle, }) } }