Skip to main content

opys_core/
discovery.rs

1use serde::{Deserialize, Serialize};
2
3/// Where a discovered hash sits, keyed by algorithm. The string is a
4/// *location* (header name or URL), not the hash itself.
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(untagged)]
7pub enum HashRef {
8    Sha256 { sha256: String },
9    Sha1 { sha1: String },
10    Md5 { md5: String },
11}
12
13impl HashRef {
14    pub fn algo(&self) -> crate::HashAlgo {
15        match self {
16            HashRef::Sha256 { .. } => crate::HashAlgo::Sha256,
17            HashRef::Sha1 { .. } => crate::HashAlgo::Sha1,
18            HashRef::Md5 { .. } => crate::HashAlgo::Md5,
19        }
20    }
21    pub fn location(&self) -> &str {
22        match self {
23            HashRef::Sha256 { sha256 } => sha256,
24            HashRef::Sha1 { sha1 } => sha1,
25            HashRef::Md5 { md5 } => md5,
26        }
27    }
28}
29
30#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
31pub struct IntegrityProbes {
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub header: Option<HashRef>,
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub url: Option<HashRef>,
36}
37
38#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
39pub struct SizeProbes {
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    pub header: Option<String>,
42}
43
44#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
45pub struct Discovery {
46    #[serde(default, skip_serializing_if = "Option::is_none")]
47    pub integrity: Option<IntegrityProbes>,
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub size: Option<SizeProbes>,
50}