pkg/
package.rs

1use serde_derive::{Deserialize, Serialize};
2use std::{collections::HashMap, fmt};
3use toml::{self, from_str, to_string};
4
5use crate::Error;
6
7#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd)]
8pub struct Package {
9    pub name: PackageName,
10    pub version: String,
11    pub target: String,
12    //pub summary: String,
13    //pub description: String,
14    pub depends: Vec<PackageName>,
15}
16
17/*impl Ord for Package {
18    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
19        self.name.cmp(&other.name)
20    }
21}*/
22
23impl Package {
24    pub fn from_toml(text: &str) -> Result<Self, toml::de::Error> {
25        from_str(text)
26    }
27
28    #[allow(dead_code)]
29    pub fn to_toml(&self) -> String {
30        // to_string *should* be safe to unwrap for this struct
31        // use error handeling callbacks for this
32        to_string(self).unwrap()
33    }
34}
35
36#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize, Serialize)]
37#[serde(into = "String")]
38#[serde(try_from = "String")]
39pub struct PackageName(String);
40
41impl PackageName {
42    pub fn new(name: impl Into<String>) -> Result<Self, Error> {
43        let name = name.into();
44        //TODO: are there any other characters that should be invalid?
45        if name.contains('.') || name.contains('/') || name.contains('\0') {
46            return Err(Error::PackageNameInvalid(name));
47        }
48        Ok(Self(name))
49    }
50
51    pub fn as_str(&self) -> &str {
52        self.0.as_str()
53    }
54}
55
56impl From<PackageName> for String {
57    fn from(package_name: PackageName) -> Self {
58        package_name.0
59    }
60}
61
62impl TryFrom<String> for PackageName {
63    type Error = Error;
64    fn try_from(name: String) -> Result<Self, Error> {
65        Self::new(name)
66    }
67}
68
69impl fmt::Display for PackageName {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        write!(f, "{}", self.0)
72    }
73}
74
75#[derive(Debug)]
76pub struct PackageInfo {
77    pub installed: bool,
78    pub version: String,
79    pub target: String,
80
81    pub download_size: String,
82    // pub install_size: String,
83    pub depends: Vec<PackageName>,
84}
85
86 #[derive(Debug, serde::Deserialize)]
87pub struct Repository {
88    pub packages: HashMap<String, String>,
89}
90
91impl Repository {
92    pub fn from_toml(text: &str) -> Result<Self, toml::de::Error> {
93        from_str(text)
94    }
95}