cli/
options.rs

1/*---------------------------------------------------------------------------------------------
2 *  Copyright (c) Microsoft Corporation. All rights reserved.
3 *  Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5
6use std::fmt;
7
8use serde::{Deserialize, Serialize};
9
10use crate::constants::SERVER_NAME_MAP;
11
12#[derive(clap::ValueEnum, Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
13pub enum Quality {
14	#[serde(rename = "stable")]
15	Stable,
16	#[serde(rename = "exploration")]
17	Exploration,
18	#[serde(other)]
19	Insiders,
20}
21
22impl Quality {
23	/// Lowercased quality name in paths and protocol
24	pub fn get_machine_name(&self) -> &'static str {
25		match self {
26			Quality::Insiders => "insiders",
27			Quality::Exploration => "exploration",
28			Quality::Stable => "stable",
29		}
30	}
31
32	/// Uppercased quality display name for humans
33	pub fn get_capitalized_name(&self) -> &'static str {
34		match self {
35			Quality::Insiders => "Insiders",
36			Quality::Exploration => "Exploration",
37			Quality::Stable => "Stable",
38		}
39	}
40
41	/// Server application name
42	pub fn server_entrypoint(&self) -> String {
43		let mut server_name = SERVER_NAME_MAP
44			.as_ref()
45			.and_then(|m| m.get(self))
46			.map(|s| s.server_application_name.as_str())
47			.unwrap_or("code-server-oss")
48			.to_string();
49
50		if cfg!(windows) {
51			server_name.push_str(".cmd");
52		}
53
54		server_name
55	}
56}
57
58impl fmt::Display for Quality {
59	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60		write!(f, "{}", self.get_capitalized_name())
61	}
62}
63
64impl TryFrom<&str> for Quality {
65	type Error = String;
66
67	fn try_from(s: &str) -> Result<Self, Self::Error> {
68		match s {
69			"stable" => Ok(Quality::Stable),
70			"insiders" | "insider" => Ok(Quality::Insiders),
71			"exploration" => Ok(Quality::Exploration),
72			_ => Err(format!(
73				"Unknown quality: {}. Must be one of stable, insiders, or exploration.",
74				s
75			)),
76		}
77	}
78}
79
80#[derive(clap::ValueEnum, Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
81pub enum TelemetryLevel {
82	Off,
83	Crash,
84	Error,
85	All,
86}
87
88impl fmt::Display for TelemetryLevel {
89	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90		match self {
91			TelemetryLevel::Off => write!(f, "off"),
92			TelemetryLevel::Crash => write!(f, "crash"),
93			TelemetryLevel::Error => write!(f, "error"),
94			TelemetryLevel::All => write!(f, "all"),
95		}
96	}
97}