lux_lib/remote/package/
mod.rs1pub mod kind;
4pub mod source;
5
6pub(crate) use source::Source;
7
8use std::cmp::Ordering;
9use std::str::FromStr;
10
11use miette::Diagnostic;
12use thiserror::Error;
13
14use crate::lockfile::RemotePackageSourceUrl;
15use crate::package::version;
16use crate::package::Spec;
17
18#[derive(Clone, Debug)]
19pub(crate) struct Package {
20 pub spec: Spec,
21 pub source: Source,
22 pub source_url: Option<RemotePackageSourceUrl>,
24}
25
26#[derive(PartialEq, Eq, Hash, Clone, Debug)]
27pub(crate) enum Kind {
28 Rockspec,
29 Src,
30 Binary,
31}
32
33impl Ord for Kind {
34 fn cmp(&self, other: &Self) -> Ordering {
35 match (self, other) {
37 (Self::Binary, Self::Binary)
38 | (Self::Rockspec, Self::Rockspec)
39 | (Self::Src, Self::Src) => Ordering::Equal,
40
41 (Self::Binary, _) => Ordering::Greater,
42 (_, Self::Binary) => Ordering::Less,
43 (Self::Rockspec, Self::Src) => Ordering::Greater,
44 (Self::Src, Self::Rockspec) => Ordering::Less,
45 }
46 }
47}
48
49impl PartialOrd for Kind {
50 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
51 Some(self.cmp(other))
52 }
53}
54
55#[derive(Error, Debug, Diagnostic)]
56pub enum Error {
57 #[error("unable to parse package {0}. expected format: `name@version`")]
58 InvalidInput(String),
59 #[error("unable to parse package {package_str}:\n{error}")]
60 InvalidPackageVersion {
61 #[source]
62 error: version::Error,
63 package_str: String,
64 },
65}
66
67impl Package {
68 pub fn new(spec: Spec, source: Source, source_url: Option<RemotePackageSourceUrl>) -> Self {
69 Self {
70 spec,
71 source,
72 source_url,
73 }
74 }
75}
76
77impl FromStr for Spec {
78 type Err = Error;
79
80 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
81 let (name, version) = s
82 .split_once('@')
83 .ok_or_else(|| Error::InvalidInput(s.to_string()))?;
84
85 Self::parse(name.to_string(), version.to_string()).map_err(|error| {
86 Error::InvalidPackageVersion {
87 error,
88 package_str: s.to_string(),
89 }
90 })
91 }
92}