Skip to main content

brooks_lib/cdni/original/
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//! Definitions of metadata specified in original interconnection specification
19
20use crate::macros::IntoCdniMetadata;
21use brooks_macros::CdniMetadata;
22
23use serde::{Deserialize, Serialize};
24use serde_json::Value;
25
26use std::fmt::Debug;
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct Source {
30    pub endpoints: Vec<String>,
31    pub protocol: String,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct SourceMetadata<A: Debug + Clone + Default> {
36    pub sources: Vec<Source>,
37    #[serde(skip)]
38    pub aug: A,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize, CdniMetadata)]
42pub struct TypedSourceMetadata<A: Debug + Clone + Default> {
43    #[serde(rename = "generic-metadata-type")]
44    pub tpe: String,
45    #[serde(rename = "generic-metadata-value")]
46    pub value: SourceMetadata<A>,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct CachePolicy<A: Debug + Clone + Default> {
51    pub policy: String,
52    #[serde(skip)]
53    pub aug: A,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize, CdniMetadata)]
57pub struct TypedCachePolicy<A: Debug + Clone + Default> {
58    #[serde(rename = "generic-metadata-type")]
59    pub tpe: String,
60    #[serde(rename = "generic-metadata-value")]
61    pub value: CachePolicy<A>,
62}
63
64#[cfg(test)]
65mod test_parse_source_metadata {
66    use std::path::Path;
67
68    use crate::{cdni::original::spec::TypedSourceMetadata, tests::read_test_file};
69
70    #[test]
71    fn test_verify_8006_example() {
72        let json = read_test_file(Path::new(
73            "./src/cdni/tests/source/modified-8006-example.json",
74        ));
75        let cop_metadata = serde_json::from_str::<TypedSourceMetadata<()>>(&json)
76            .expect("Could not parse JSON test file");
77        assert_eq!(
78            cop_metadata.tpe,
79            TypedSourceMetadata::<()>::typed_cdni_metadata_name()
80        );
81    }
82}