1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#[macro_use]
extern crate serde_derive;

use chrono::{DateTime, Utc};
use url::{Url};
use std::str::FromStr;


impl FromStr for Metalink4 {
    type Err = serde_xml_rs::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        serde_xml_rs::from_str(s)
    }
}


#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename = "metalink")]
pub struct Metalink4 {
    pub generator: Option<String>,
    pub origin: Option<String>,
    pub published: Option<DateTime<Utc>>,
    pub updated: Option<DateTime<Utc>>,
    #[serde(rename = "file")]
    pub files: Vec<File>
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct File {
    pub name: String, // attr
    pub copyright: Option<String>,
    pub description: Option<String>,
    #[serde(rename = "hash", default)]
    pub hashes: Vec<Hash>,
    pub identity: Option<String>,
    pub language: Option<Vec<String>>,
    pub logo: Option<Url>, // uri
    #[serde(rename = "metaurl", default)]
    pub meta_urls: Vec<FileMetaUrl>,
    #[serde(rename = "os", default)]
    pub os: Vec<String>,
    #[serde(rename = "piece", default)]
    pub pieces: Vec<Piece>,
    pub publisher: Option<Publisher>,
    pub signature: Option<String>,
    pub size: Option<u64>,
    #[serde(rename = "url", default)]
    pub urls: Vec<FileUrl>,
    pub version: Option<String>
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Piece {
    pub length: u64, // attr
    pub r#type: String, // attr
    #[serde(rename = "hash", default)]
    pub hashes: Vec<Hash>
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Hash {
    pub r#type: Option<String>, // attr
    #[serde(rename = "$value")]
    pub value: String
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct FileMetaUrl {
    pub priority: Option<u32>, // attr
    pub mediatype: String, // attr
    pub name: Option<String>, // attr
    #[serde(rename = "$value")]
    pub value: String
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Origin {
    pub dynamic: Option<bool>,
    #[serde(rename = "$value")]
    pub value: String
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Publisher {
    pub name: String, //attr
    pub url: Option<Url>
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Signature {
    pub mediatype: String, //attr
    #[serde(rename = "$value")]
    pub value: String
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct FileUrl {
    pub location: Option<String>, //attr
    pub priority: Option<u32>, //attr
    #[serde(rename = "$value")]
    pub value: String
}