summaryrefslogtreecommitdiffstats
path: root/src/version.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-01 17:49:08 +0100
committersalmonik <salmonik@tuta.io>2026-11-01 17:49:08 +0100
commit2fe2e8cdbc3bb520994ff0944188f2fa657c546a (patch)
tree10f29b4dd62c2b8127d20f0bffd4325b3ca0bfd2 /src/version.rs
parentdocs: add the phased GUI plan (diff)
downloadzagan-bet-2fe2e8cdbc3bb520994ff0944188f2fa657c546a.tar.gz
zagan-bet-2fe2e8cdbc3bb520994ff0944188f2fa657c546a.zip
v4.0.0: require HELLO MAJOR.MINOR lockstep before auth
Diffstat (limited to 'src/version.rs')
-rw-r--r--src/version.rs117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/version.rs b/src/version.rs
new file mode 100644
index 0000000..c1dcee5
--- /dev/null
+++ b/src/version.rs
@@ -0,0 +1,117 @@
1//! Version compatibility and the `HELLO` handshake.
2//!
3//! Client and server must share MAJOR.MINOR; only the patch component may
4//! differ. The first line after TLS is `HELLO <version>`, and nothing else is
5//! accepted until the server has answered it.
6
7use std::env;
8
9/// Version this build reports. The client and the server share it.
10pub const VERSION: &str = env!("CARGO_PKG_VERSION");
11
12/// Where players download new releases when the server advertises one and the
13/// operator has not set `DOWNLOAD_URL`.
14pub const DEFAULT_DOWNLOAD_URL: &str = "https://zagan.2137697.xyz/";
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
17pub struct Version {
18 pub major: u64,
19 pub minor: u64,
20 pub patch: u64,
21}
22
23impl Version {
24 /// Parse `MAJOR.MINOR.PATCH`, tolerating a leading `v` and a pre-release or
25 /// build suffix (`4.0.0-rc1`, `4.0.0+build`).
26 pub fn parse(raw: &str) -> Option<Self> {
27 let raw = raw.trim();
28 let raw = raw.strip_prefix('v').unwrap_or(raw);
29
30 // Drop a pre-release or build suffix, but not an empty one.
31 let (core, suffix) = match raw.find(['-', '+']) {
32 Some(index) => (&raw[..index], Some(&raw[index + 1..])),
33 None => (raw, None),
34 };
35 if suffix == Some("") {
36 return None;
37 }
38
39 let mut parts = core.split('.');
40 let major = parts.next()?.parse().ok()?;
41 let minor = parts.next()?.parse().ok()?;
42 let patch = parts.next()?.parse().ok()?;
43 if parts.next().is_some() {
44 return None;
45 }
46
47 Some(Self {
48 major,
49 minor,
50 patch,
51 })
52 }
53
54 pub fn compatible_with(self, other: Self) -> bool {
55 self.major == other.major && self.minor == other.minor
56 }
57}
58
59/// True when both versions parse and share MAJOR.MINOR.
60pub fn compatible(client: &str, server: &str) -> bool {
61 match (Version::parse(client), Version::parse(server)) {
62 (Some(client), Some(server)) => client.compatible_with(server),
63 _ => false,
64 }
65}
66
67/// True when `latest` is strictly newer than `current`. Unparseable versions
68/// are never "newer", so a broken reply cannot nag the player.
69pub fn is_newer(latest: &str, current: &str) -> bool {
70 match (Version::parse(latest), Version::parse(current)) {
71 (Some(latest), Some(current)) => latest > current,
72 _ => false,
73 }
74}
75
76/// The server line that accepts a handshake.
77pub fn hello_line(version: &str, latest: &str, url: &str) -> String {
78 format!("HELLO {version} latest={latest} url={url}")
79}
80
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct HelloReply {
83 pub version: String,
84 pub latest: String,
85 pub url: String,
86}
87
88/// Parse `HELLO <version> latest=<version> url=<url>`.
89pub fn parse_hello(line: &str) -> Option<HelloReply> {
90 let rest = line.trim().strip_prefix("HELLO ")?;
91 let mut tokens = rest.split_whitespace();
92 let version = tokens.next()?.to_string();
93
94 let mut latest = None;
95 let mut url = None;
96 for token in tokens {
97 if let Some(value) = token.strip_prefix("latest=") {
98 latest = Some(value.to_string());
99 } else if let Some(value) = token.strip_prefix("url=") {
100 url = Some(value.to_string());
101 }
102 }
103
104 Some(HelloReply {
105 version,
106 latest: latest?,
107 url: url?,
108 })
109}
110
111/// Download page advertised by the server. `DOWNLOAD_URL` wins when set.
112pub fn download_url() -> String {
113 env::var("DOWNLOAD_URL")
114 .ok()
115 .filter(|value| !value.trim().is_empty())
116 .unwrap_or_else(|| DEFAULT_DOWNLOAD_URL.to_string())
117}