warg_api/
lib.rs

1//! The serializable types for the Warg REST API.
2#![deny(missing_docs)]
3
4pub mod v1;
5
6use serde::{de::Unexpected, Deserialize, Serialize};
7
8/// Relative URL path for the `WellKnownConfig`.
9pub const WELL_KNOWN_PATH: &str = ".well-known/wasm-pkg/registry.json";
10
11/// This config allows a domain to point to another URL where the registry
12/// API is hosted.
13#[derive(Debug, Default, Deserialize, Serialize)]
14#[serde(rename_all = "camelCase")]
15pub struct WellKnownConfig {
16    /// For OCI registries, the domain name where the registry is hosted.
17    pub oci_registry: Option<String>,
18    /// For OCI registries, a name prefix to use before the namespace.
19    pub oci_namespace_prefix: Option<String>,
20    /// For Warg registries, the URL where the registry is hosted.
21    pub warg_url: Option<String>,
22}
23
24/// A utility type for serializing and deserializing constant status codes.
25struct Status<const CODE: u16>;
26
27impl<const CODE: u16> Serialize for Status<CODE> {
28    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
29    where
30        S: serde::Serializer,
31    {
32        serializer.serialize_u16(CODE)
33    }
34}
35
36impl<'de, const CODE: u16> Deserialize<'de> for Status<CODE> {
37    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
38    where
39        D: serde::Deserializer<'de>,
40    {
41        let code = u16::deserialize(deserializer)?;
42        if code == CODE {
43            Ok(Status::<CODE>)
44        } else {
45            Err(serde::de::Error::invalid_value(
46                Unexpected::Unsigned(code as u64),
47                &"a matching status code",
48            ))
49        }
50    }
51}