cyfs_base/base/
channel.rs

1use super::error::{BuckyError, BuckyResult};
2use std::fmt::{Display, Formatter};
3use std::str::FromStr;
4
5#[derive(Debug, Clone, Eq, PartialEq)]
6pub enum CyfsChannel {
7    Nightly,
8    Beta,
9    Stable,
10}
11
12impl FromStr for CyfsChannel {
13    type Err = BuckyError;
14
15    fn from_str(str: &str) -> BuckyResult<Self> {
16        let ret = match str {
17            "nightly" => CyfsChannel::Nightly,
18            "beta" => CyfsChannel::Beta,
19            "stable" => CyfsChannel::Stable,
20            _ => {
21                log::warn!("unknown channel name {}, use default nightly channel", str);
22                CyfsChannel::Nightly
23            }
24        };
25
26        Ok(ret)
27    }
28}
29
30impl Display for CyfsChannel {
31    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32        match self {
33            CyfsChannel::Nightly => write!(f, "nightly"),
34            CyfsChannel::Beta => write!(f, "beta"),
35            CyfsChannel::Stable => write!(f, "stable"),
36        }
37    }
38}
39
40impl CyfsChannel {
41    fn get_ver(&self) -> u8 {
42        match self {
43            CyfsChannel::Nightly => 0,
44            CyfsChannel::Beta => 1,
45            CyfsChannel::Stable => 2,
46        }
47    }
48}
49
50pub fn get_version() -> &'static str {
51    &VERSION
52}
53
54pub fn get_channel() -> &'static CyfsChannel {
55    &CHANNEL
56}
57
58pub fn get_target() -> &'static str {
59    &TARGET
60}
61
62fn get_version_impl() -> String {
63    let channel_ver = get_channel().get_ver();
64    format!("1.1.{}.{}-{} ({})", channel_ver, env!("VERSION"), get_channel(), env!("BUILDDATE"))
65}
66
67fn get_channel_impl() -> CyfsChannel {
68    let channel_str = match std::env::var("CYFS_CHANNEL") {
69        Ok(channel) => {
70            info!("got channel config from CYFS_CHANNEL env: channel={}", channel);
71            channel
72        }
73        Err(_) => {
74            let channel = env!("CHANNEL").to_owned();
75            info!("use default channel config: channel={}", channel);
76            channel
77        }
78    };
79    
80    CyfsChannel::from_str(channel_str.as_str()).unwrap()
81}
82
83lazy_static::lazy_static! {
84    static ref CHANNEL: CyfsChannel = get_channel_impl();
85    static ref VERSION: String = get_version_impl();
86    static ref TARGET: &'static str = env!("TARGET");
87}