marketplace_client/
lib.rs1pub mod proto;
6
7mod consume;
8mod grpc;
9mod rest;
10
11#[derive(Debug)]
13pub enum MarketplaceError {
14 Transport(String),
16 Unauthenticated,
18 PermissionDenied,
20 Invalid(String),
22 Conflict(String),
24 Http { status: u16, detail: String },
26 Decode(String),
28}
29
30impl std::fmt::Display for MarketplaceError {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 match self {
33 MarketplaceError::Transport(m) => write!(f, "transport error: {m}"),
34 MarketplaceError::Unauthenticated => {
35 write!(f, "unauthenticated (check your PAT / credentials)")
36 }
37 MarketplaceError::PermissionDenied => write!(f, "permission denied"),
38 MarketplaceError::Invalid(m) => write!(f, "invalid request: {m}"),
39 MarketplaceError::Conflict(m) => write!(f, "conflict: {m}"),
40 MarketplaceError::Http { status, detail } => write!(f, "http {status}: {detail}"),
41 MarketplaceError::Decode(m) => write!(f, "decode error: {m}"),
42 }
43 }
44}
45impl std::error::Error for MarketplaceError {}
46
47pub struct PublishParams {
48 pub app_wasm: Vec<u8>,
49 pub manifest_mpack: Vec<u8>,
50 pub release_json: Vec<u8>,
51 pub artifact_sig: [u8; 64],
52 pub publisher_pubkey: [u8; 32],
53}
54#[derive(Debug)]
55pub struct PackageRef {
56 pub name: String,
57 pub version: String,
58}
59
60#[derive(Debug)]
62pub struct PackageSummary {
63 pub name: String,
64 pub kind: String,
65 pub current_version: String,
66 pub publisher_handle: String,
67 pub download_count: u64,
68}
69
70#[derive(Debug)]
72pub struct CapabilitiesView {
73 pub wasi: Vec<String>,
74 pub argone: Vec<String>,
75 pub accept_hash: String,
76}
77
78#[derive(Debug)]
80pub struct VersionInfo {
81 pub version: String,
82 pub critical: bool,
83 pub channel: String,
84 pub published_at: String,
85 pub capabilities: CapabilitiesView,
86 pub download_count: u64,
87}
88
89#[derive(Debug)]
91pub struct UpdateAvailable {
92 pub name: String,
93 pub latest_version: String,
94 pub critical: bool,
95}
96
97#[derive(Debug)]
99pub struct PackageInfo {
100 pub package: PackageSummary,
101 pub readme: String,
102 pub publisher_handle: String,
103 pub versions: Vec<VersionInfo>,
104}
105
106#[derive(Debug)]
109pub struct DownloadedArtifact {
110 pub name: String,
111 pub version: String,
112 pub release_json: Vec<u8>,
113 pub manifest_mpack: Vec<u8>,
114 pub artifact_sig: [u8; 64],
115 pub publisher_pubkey: [u8; 32],
116 pub root_countersig: [u8; 64],
117 pub capabilities: CapabilitiesView,
118 pub app_wasm: Vec<u8>,
119}
120
121pub struct Client {
125 base_url: String,
126 http: reqwest::Client,
127}
128
129impl Client {
130 pub fn connect(base_url: &str) -> Result<Client, MarketplaceError> {
133 build_client(base_url, false)
134 }
135
136 pub fn connect_h2c(base_url: &str) -> Result<Client, MarketplaceError> {
140 build_client(base_url, true)
141 }
142}
143
144fn build_client(base_url: &str, h2c: bool) -> Result<Client, MarketplaceError> {
145 if !(base_url.starts_with("http://") || base_url.starts_with("https://")) {
146 return Err(MarketplaceError::Transport(format!(
147 "registry URL must start with http:// or https:// (got {base_url})"
148 )));
149 }
150 let mut builder = reqwest::Client::builder();
151 if h2c {
152 builder = builder.http2_prior_knowledge();
153 }
154 let http = builder
155 .build()
156 .map_err(|e| MarketplaceError::Transport(e.to_string()))?;
157 Ok(Client {
158 base_url: base_url.trim_end_matches('/').to_string(),
159 http,
160 })
161}
162
163#[cfg(test)]
164mod codegen_smoke {
165 #[test]
166 fn generated_types_are_accessible() {
167 let req = crate::proto::PublishRequest::default();
169 assert!(req.app_wasm.is_empty());
170 let _ref = crate::proto::PackageRef {
171 name: "hello".into(),
172 version: "1.2.3".into(),
173 };
174 }
175}
176
177#[cfg(test)]
178mod connect_tests {
179 use super::*;
180 #[test]
181 fn rejects_unsupported_scheme() {
182 assert!(matches!(
183 Client::connect("ftp://x"),
184 Err(MarketplaceError::Transport(_))
185 ));
186 }
187 #[test]
188 fn accepts_http_and_https() {
189 assert!(Client::connect("http://localhost:8080").is_ok());
190 assert!(Client::connect("https://marketplace.example").is_ok());
191 }
192 #[test]
193 fn connect_h2c_accepts_http_and_https() {
194 assert!(Client::connect_h2c("http://localhost:8080").is_ok());
195 assert!(Client::connect_h2c("https://marketplace.example").is_ok());
196 }
197}