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
use super::error::{BuckyError, BuckyResult};
use std::fmt::{Display, Formatter};
use std::str::FromStr;

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum CyfsChannel {
    Nightly,
    Beta,
    Stable,
}

impl FromStr for CyfsChannel {
    type Err = BuckyError;

    fn from_str(str: &str) -> BuckyResult<Self> {
        let ret = match str {
            "nightly" => CyfsChannel::Nightly,
            "beta" => CyfsChannel::Beta,
            "stable" => CyfsChannel::Stable,
            _ => {
                log::warn!("unknown channel name {}, use default nightly channel", str);
                CyfsChannel::Nightly
            }
        };

        Ok(ret)
    }
}

impl Display for CyfsChannel {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            CyfsChannel::Nightly => write!(f, "nightly"),
            CyfsChannel::Beta => write!(f, "beta"),
            CyfsChannel::Stable => write!(f, "stable"),
        }
    }
}

impl CyfsChannel {
    fn get_ver(&self) -> u8 {
        match self {
            CyfsChannel::Nightly => 0,
            CyfsChannel::Beta => 1,
            CyfsChannel::Stable => 2,
        }
    }
}

pub fn get_version() -> &'static str {
    &VERSION
}

pub fn get_channel() -> &'static CyfsChannel {
    &CHANNEL
}

pub fn get_target() -> &'static str {
    &TARGET
}

fn get_version_impl() -> String {
    let channel_ver = get_channel().get_ver();
    format!("1.0.{}.{}-{} ({})", channel_ver, env!("VERSION"), get_channel(), env!("BUILDDATE"))
}

fn get_channel_impl() -> CyfsChannel {
    let channel_str = match std::env::var("CYFS_CHANNEL") {
        Ok(channel) => {
            info!("got channel config from CYFS_CHANNEL env: channel={}", channel);
            channel
        }
        Err(_) => {
            let channel = env!("CHANNEL").to_owned();
            info!("use default channel config: channel={}", channel);
            channel
        }
    };
    
    CyfsChannel::from_str(channel_str.as_str()).unwrap()
}

lazy_static::lazy_static! {
    static ref CHANNEL: CyfsChannel = get_channel_impl();
    static ref VERSION: String = get_version_impl();
    static ref TARGET: &'static str = env!("TARGET");
}