mc_launchermeta/
version_manifest.rs

1////////////////////////////////////////////////////////////////////////////////
2// Copyright (c) 2023. Rob Bailey                                              /
3// This Source Code Form is subject to the terms of the Mozilla Public         /
4// License, v. 2.0. If a copy of the MPL was not distributed with this         /
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.                   /
6////////////////////////////////////////////////////////////////////////////////
7
8use serde::{Deserialize, Serialize};
9
10use crate::VersionKind;
11
12#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
13#[serde(deny_unknown_fields)]
14pub struct Latest {
15    pub release: String,
16    pub snapshot: String,
17}
18
19#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21#[serde(deny_unknown_fields)]
22pub struct Version {
23    pub id: String,
24    pub url: String,
25    pub time: String,
26    pub release_time: String,
27    #[serde(rename = "type")]
28    pub kind: VersionKind,
29}
30
31#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
32#[serde(deny_unknown_fields)]
33pub struct Manifest {
34    pub latest: Latest,
35    pub versions: Vec<Version>,
36}
37
38impl Manifest {
39    pub fn get_version(&self, id: &str) -> Option<&Version> {
40        self.versions.iter().find(|v| v.id == id)
41    }
42
43    pub fn get_latest(&self, kind: VersionKind) -> Option<&Version> {
44        match kind {
45            VersionKind::Release => self.get_version(&self.latest.release),
46            VersionKind::Snapshot => self.get_version(&self.latest.snapshot),
47            _ => None,
48        }
49    }
50}