lux_lib/remote/package/
source.rs1use std::fmt::Display;
2
3use miette::Diagnostic;
4use serde::{de, Deserialize, Deserializer, Serialize};
5use thiserror::Error;
6use url::Url;
7
8const PLUS: &str = "+";
9
10#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
15pub(crate) enum Source {
16 LuarocksRockspec(Url),
17 LuarocksSrcRock(Url),
18 LuarocksBinaryRock(Url),
19 Wally(Url),
20 RockspecContent(String),
21 Local,
22 #[cfg(test)]
23 Test,
24}
25
26impl Source {
27 pub(crate) fn url(self) -> Option<Url> {
28 match self {
29 Self::LuarocksRockspec(url)
30 | Self::LuarocksSrcRock(url)
31 | Self::LuarocksBinaryRock(url)
32 | Self::Wally(url) => Some(url),
33 Self::RockspecContent(_) | Self::Local => None,
34 #[cfg(test)]
35 Self::Test => None,
36 }
37 }
38}
39
40impl Display for Source {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 match &self {
43 Source::LuarocksRockspec(url) => format!("luarocks_rockspec{PLUS}{url}").fmt(f),
44 Source::LuarocksSrcRock(url) => format!("luarocks_src_rock{PLUS}{url}").fmt(f),
45 Source::LuarocksBinaryRock(url) => format!("luarocks_rock{PLUS}{url}").fmt(f),
46 Source::Wally(url) => format!("wally{PLUS}{url}").fmt(f),
47 Source::RockspecContent(content) => format!("rockspec{PLUS}{content}").fmt(f),
48 Source::Local => "local".fmt(f),
49 #[cfg(test)]
50 Source::Test => "test+foo_bar".fmt(f),
51 }
52 }
53}
54
55impl Serialize for Source {
56 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
57 where
58 S: serde::Serializer,
59 {
60 format!("{self}").serialize(serializer)
61 }
62}
63
64#[derive(Error, Debug, Diagnostic)]
65#[non_exhaustive]
66pub enum Error {
67 #[error("error parsing remote source URL {0}. Missing URL.")]
68 MissingUrl(String),
69 #[error("error parsing remote source URL {0}. Expected <source_type>+<url>.")]
70 MissingSeparator(String),
71 #[error("error parsing remote source type {0}. Expected 'luarocks' or 'rockspec'.")]
72 UnknownRemoteSourceType(String),
73 #[error(transparent)]
74 Url(#[from] url::ParseError),
75}
76
77impl TryFrom<String> for Source {
78 type Error = Error;
79
80 fn try_from(value: String) -> Result<Self, Self::Error> {
81 if let Some(pos) = value.find(PLUS) {
82 if let Some(str) = value.get(pos + 1..) {
83 let remote_source_type = value[..pos].into();
84 match remote_source_type {
85 "luarocks_rockspec" => Ok(Self::LuarocksRockspec(Url::parse(str)?)),
86 "luarocks_src_rock" => Ok(Self::LuarocksSrcRock(Url::parse(str)?)),
87 "luarocks_rock" => Ok(Self::LuarocksBinaryRock(Url::parse(str)?)),
88 "wally" => Ok(Self::Wally(Url::parse(str)?)),
89 "rockspec" => Ok(Self::RockspecContent(str.into())),
90 #[cfg(test)]
91 "test" => Ok(Self::Test),
92 _ => Err(Error::UnknownRemoteSourceType(remote_source_type.into())),
93 }
94 } else {
95 Err(Error::MissingUrl(value))
96 }
97 } else if value == "local" {
98 Ok(Self::Local)
99 } else {
100 Err(Error::MissingSeparator(value))
101 }
102 }
103}
104
105impl<'de> Deserialize<'de> for Source {
106 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
107 where
108 D: Deserializer<'de>,
109 {
110 let value = String::deserialize(deserializer)?;
111 Self::try_from(value).map_err(de::Error::custom)
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 const LUAROCKS_ROCKSPEC: &str = "
120rockspec_format = \"3.0\"
121package = 'luarocks'
122version = '3.11.1-1'
123source = {
124 url = 'git+https://github.com/luarocks/luarocks',
125 tag = 'v3.11.1'
126}
127";
128
129 #[test]
130 fn luarocks_source_roundtrip() {
131 let url = Url::parse("https://luarocks.org/").unwrap();
132 let source = Source::LuarocksRockspec(url.clone());
133 let roundtripped = Source::try_from(format!("{source}")).unwrap();
134 assert_eq!(source, roundtripped);
135 let source = Source::LuarocksSrcRock(url.clone());
136 let roundtripped = Source::try_from(format!("{source}")).unwrap();
137 assert_eq!(source, roundtripped);
138 let source = Source::LuarocksBinaryRock(url);
139 let roundtripped = Source::try_from(format!("{source}")).unwrap();
140 assert_eq!(source, roundtripped)
141 }
142
143 #[test]
144 fn wally_source_roundtrip() {
145 let url = Url::parse("https://github.com/UpliftGames/wally-index").unwrap();
146 let source = Source::Wally(url);
147 let roundtripped = Source::try_from(format!("{source}")).unwrap();
148 assert_eq!(source, roundtripped);
149 }
150
151 #[test]
152 fn rockspec_source_roundtrip() {
153 let source = Source::RockspecContent(LUAROCKS_ROCKSPEC.into());
154 let roundtripped = Source::try_from(format!("{source}")).unwrap();
155 assert_eq!(source, roundtripped)
156 }
157}