Skip to main content

modrinth_wrapped/models/
version_file.rs

1use std::str::FromStr;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
6#[serde(rename_all = "kebab-case")]
7/// The type of a version file
8pub enum VersionFileType {
9    RequiredResourcePack,
10    OptionalResourcePack,
11    SourcesJar,
12    DevJar,
13    JavadocJar,
14    Signature,
15    #[serde(other)]
16    Unknown,
17}
18
19#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
20#[serde(rename_all = "lowercase")]
21/// Hash types of a version file
22pub enum VersionHashAlgorithm {
23    Sha512,
24    Sha1,
25}
26
27impl std::fmt::Display for VersionHashAlgorithm {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            VersionHashAlgorithm::Sha1 => write!(f, "sha1"),
31            VersionHashAlgorithm::Sha512 => write!(f, "sha512"),
32        }
33    }
34}
35
36#[derive(Debug)]
37pub struct ParseVersionHashTypeError(String);
38
39impl std::fmt::Display for ParseVersionHashTypeError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(f, "unknown VersionHashType: {}", self.0)
42    }
43}
44
45impl std::error::Error for ParseVersionHashTypeError {}
46
47impl FromStr for VersionHashAlgorithm {
48    type Err = ParseVersionHashTypeError;
49
50    fn from_str(s: &str) -> Result<Self, Self::Err> {
51        match s.to_ascii_lowercase().as_str() {
52            "sha512" => Ok(VersionHashAlgorithm::Sha512),
53            "sha1" => Ok(VersionHashAlgorithm::Sha1),
54            other => Err(ParseVersionHashTypeError(other.to_string())),
55        }
56    }
57}
58
59impl TryFrom<String> for VersionHashAlgorithm {
60    type Error = ParseVersionHashTypeError;
61
62    fn try_from(value: String) -> Result<Self, Self::Error> {
63        Self::from_str(&value)
64    }
65}
66
67impl From<VersionHashAlgorithm> for String {
68    fn from(v: VersionHashAlgorithm) -> Self {
69        match v {
70            VersionHashAlgorithm::Sha512 => "sha512".to_string(),
71            VersionHashAlgorithm::Sha1 => "sha1".to_string(),
72        }
73    }
74}
75
76#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
77/// Hashes of a version file
78pub struct VersionFileHashes {
79    pub sha512: String,
80    pub sha1: String,
81}
82
83#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
84/// A file for a version of a project.
85pub struct VersionFile {
86    /// A map of hashes of the file. The key is the hashing algorithm and the value is the string version of the hash.
87    pub hashes: VersionFileHashes,
88    /// A direct link to the file
89    pub url: String,
90    /// The name of the file
91    pub filename: String,
92    /// Whether this file is the primary one for its version.
93    /// Only a maximum of one file per version will have this set to true.
94    /// If there are not any primary files, it can be inferred that the first file is the primary one.
95    pub primary: bool,
96    /// The size of the file in bytes
97    pub size: u32,
98    /// The type of this version file
99    pub file_type: Option<VersionFileType>,
100}