Skip to main content

client_core/
version.rs

1//! Self-version reporting helpers shared by CLI and desktop.
2//!
3//! Both clients pass their own CARGO_PKG_VERSION (binary-crate level, not
4//! cinchcli-core's version) into `ClientInfo` at startup. The resulting
5//! struct is consumed by RestClient (HTTP headers) and WsClient (hello).
6
7use reqwest::header::{HeaderName, HeaderValue};
8
9use crate::protocol::{ClientHelloPayload, WSMessage};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum ClientType {
13    Cli,
14    Desktop,
15}
16
17impl ClientType {
18    pub fn as_str(self) -> &'static str {
19        match self {
20            ClientType::Cli => "cli",
21            ClientType::Desktop => "desktop",
22        }
23    }
24}
25
26#[derive(Debug, Clone)]
27pub struct ClientInfo {
28    pub client_type: ClientType,
29    pub version: String,
30}
31
32pub const HEADER_CLIENT_VERSION: &str = "x-cinch-client-version";
33pub const HEADER_CLIENT_TYPE: &str = "x-cinch-client-type";
34
35impl ClientInfo {
36    pub fn http_headers(&self) -> [(HeaderName, HeaderValue); 2] {
37        [
38            (
39                HeaderName::from_static(HEADER_CLIENT_VERSION),
40                HeaderValue::from_str(&self.version).expect("ascii semver"),
41            ),
42            (
43                HeaderName::from_static(HEADER_CLIENT_TYPE),
44                HeaderValue::from_static(self.client_type.as_str()),
45            ),
46        ]
47    }
48
49    pub fn client_hello_message(&self) -> WSMessage {
50        WSMessage {
51            action: crate::protocol::ACTION_CLIENT_HELLO.to_string(),
52            client_hello: Some(ClientHelloPayload {
53                version: self.version.clone(),
54                type_: self.client_type.as_str().to_string(),
55                os: std::env::consts::OS.to_string(),
56            }),
57            ..Default::default()
58        }
59    }
60}