modrinth_wrapped/models/
version_file.rs1use std::str::FromStr;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
6#[serde(rename_all = "kebab-case")]
7pub 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")]
21pub 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)]
77pub struct VersionFileHashes {
79 pub sha512: String,
80 pub sha1: String,
81}
82
83#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
84pub struct VersionFile {
86 pub hashes: VersionFileHashes,
88 pub url: String,
90 pub filename: String,
92 pub primary: bool,
96 pub size: u32,
98 pub file_type: Option<VersionFileType>,
100}