plux_rs/
info.rs

1use std::{fmt::Display, path::PathBuf};
2
3use semver::{Version, VersionReq};
4use serde::{Deserialize, Serialize};
5
6use crate::{Bundle, Plugin};
7
8pub struct PluginInfo<I: Info> {
9    pub path: PathBuf,
10    pub bundle: Bundle,
11    pub info: I,
12}
13
14pub trait Info: Send + Sync {
15    fn depends(&self) -> &Vec<Depend>;
16    fn optional_depends(&self) -> &Vec<Depend>;
17}
18
19#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
20pub struct Depend {
21    pub id: String,
22    pub version: VersionReq,
23}
24
25#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
26pub struct StdInfo {
27    pub depends: Vec<Depend>,
28    pub optional_depends: Vec<Depend>,
29}
30
31impl Depend {
32    pub const fn new(name: String, version: VersionReq) -> Self {
33        Self { id: name, version }
34    }
35}
36
37impl Display for Depend {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        write!(f, "{}[{}]", self.id, self.version)
40    }
41}
42
43impl<ID: AsRef<str>> PartialEq<(ID, &Version)> for Depend {
44    fn eq(&self, (id, version): &(ID, &Version)) -> bool {
45        self.id == id.as_ref() && self.version.matches(*version)
46    }
47}
48
49impl PartialEq<Bundle> for Depend {
50    fn eq(&self, Bundle { id, version, .. }: &Bundle) -> bool {
51        self.id == *id && self.version.matches(version)
52    }
53}
54
55impl<O: Send + Sync, I: Info> PartialEq<Plugin<'_, O, I>> for Depend {
56    fn eq(&self, other: &Plugin<'_, O, I>) -> bool {
57        self.id == other.info.bundle.id && self.version.matches(&other.info.bundle.version)
58    }
59}
60
61impl StdInfo {
62    pub const fn new() -> Self {
63        Self {
64            depends: vec![],
65            optional_depends: vec![],
66        }
67    }
68}
69
70impl Info for StdInfo {
71    fn depends(&self) -> &Vec<Depend> {
72        &self.depends
73    }
74
75    fn optional_depends(&self) -> &Vec<Depend> {
76        &self.optional_depends
77    }
78}
79
80impl Display for StdInfo {
81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82        write!(
83            f,
84            "Dependencies: {};{}Optional dependencies: {}",
85            self.depends
86                .iter()
87                .map(|d| d.to_string())
88                .collect::<Vec<_>>()
89                .join(", "),
90            f.alternate().then_some('\n').unwrap_or(' '),
91            self.optional_depends
92                .iter()
93                .map(|d| d.to_string())
94                .collect::<Vec<_>>()
95                .join(", ")
96        )
97    }
98}