richat_shared/
version.rs

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
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Serialize)]
pub struct Version {
    pub package: &'static str,
    pub version: &'static str,
    pub proto: &'static str,
    pub proto_richat: &'static str,
    pub solana: &'static str,
    pub git: &'static str,
    pub rustc: &'static str,
    pub buildts: &'static str,
}

impl Version {
    pub fn create_grpc_version_info(self) -> GrpcVersionInfo {
        GrpcVersionInfo::new(self)
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GrpcVersionInfoExtra {
    hostname: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct GrpcVersionInfo {
    version: Version,
    extra: GrpcVersionInfoExtra,
}

impl GrpcVersionInfo {
    pub fn new(version: Version) -> Self {
        Self {
            version,
            extra: GrpcVersionInfoExtra {
                hostname: hostname::get()
                    .ok()
                    .and_then(|name| name.into_string().ok()),
            },
        }
    }

    pub fn json(&self) -> String {
        serde_json::to_string(self).expect("json serialization never fail")
    }

    pub fn value(&self) -> serde_json::Value {
        serde_json::to_value(self).expect("json serialization never fail")
    }
}