Skip to main content

brooks_lib/cdni/imd/
spec.rs

1// brooks, Copyright 2026, Will Hawkins
2//
3// This file is part of brooks.
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18//! Interconnection metadata definitions
19
20use crate::macros::IntoCdniMetadata;
21use brooks_macros::CdniMetadata;
22use serde::{Deserialize, Serialize};
23use serde_json::Value;
24
25use std::fmt::Debug;
26
27use crate::cdni::gmd::spec::TypedGenericMetadata;
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct HostMetadata<A: Debug + Clone + Default> {
31    pub metadata: Vec<TypedGenericMetadata<A>>,
32    #[serde(skip)]
33    pub aug: A,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize, CdniMetadata)]
37pub struct TypedHostMetadata<A: Debug + Clone + Default> {
38    #[serde(rename = "generic-metadata-type")]
39    pub tpe: String,
40    #[serde(rename = "generic-metadata-value")]
41    pub value: HostMetadata<A>,
42}
43
44#[cfg(test)]
45mod test_parse_host_metadata {
46    use std::path::Path;
47
48    use crate::{
49        cdni::{
50            gmd::spec::TypedGenericMetadata, imd::spec::TypedHostMetadata, ps::spec::TypedHeader,
51            tests::test_helpers::typed_header,
52        },
53        tests::read_test_file,
54    };
55
56    #[test]
57    fn test_verify_bad_generic_md_typename() {
58        let json = read_test_file(Path::new("./src/cdni/tests/host_metadata/all.json"));
59        let host_metadata = serde_json::from_str::<TypedHostMetadata<()>>(&json)
60            .expect("Could not parse JSON test file");
61        assert_eq!(
62            host_metadata.tpe,
63            TypedHostMetadata::<()>::typed_cdni_metadata_name()
64        );
65    }
66
67    #[test]
68    fn test_conversion_to_typed_generic_metadata() {
69        let x = typed_header("X-Cache-Control", "true", Some(true));
70        let y: TypedGenericMetadata<()> = x.into();
71        assert_eq!(y.tpe, TypedHeader::<()>::typed_cdni_metadata_name());
72    }
73}