Skip to main content

brooks_lib/cdni/cors/
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//! CORS (and related) metadata definitions
19
20use crate::macros::IntoCdniMetadata;
21use brooks_macros::CdniMetadata;
22
23use regex::Regex;
24use serde::{Deserialize, Serialize};
25use serde_json::Value;
26
27use std::fmt::Debug;
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct AccessControlAllowOriginPattern {
31    pub pattern: String,
32
33    #[serde(skip)]
34    pub aug: Option<Regex>,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct AccessControlAllowOrigin<A: Debug + Clone + Default> {
39    #[serde(rename = "allow-list")]
40    pub allow_origin: Vec<AccessControlAllowOriginPattern>,
41    #[serde(rename = "wildcard-return")]
42    pub wildcard_return: bool,
43
44    #[serde(skip)]
45    pub aug: A,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct CrossoriginPolicy<A: Debug + Clone + Default> {
50    #[serde(rename = "allow-origin")]
51    pub allow_origin: AccessControlAllowOrigin<A>,
52    #[serde(rename = "preflight-only")]
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub preflight_only: Option<bool>,
55
56    #[serde(skip)]
57    pub aug: A,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, CdniMetadata)]
61pub struct TypedCrossoriginPolicy<A: Debug + Clone + Default> {
62    #[serde(rename = "generic-metadata-type")]
63    pub tpe: String,
64    #[serde(rename = "generic-metadata-value")]
65    pub value: CrossoriginPolicy<A>,
66}
67
68#[cfg(test)]
69mod test_parse_cors_metadata {
70    use std::path::Path;
71
72    use crate::{cdni::cors::spec::TypedCrossoriginPolicy, tests::read_test_file};
73
74    #[test]
75    fn test_verify_draft_figure1() {
76        let json = read_test_file(Path::new("./src/cdni/tests/cors/figure1.json"));
77        let cop_metadata = serde_json::from_str::<TypedCrossoriginPolicy<()>>(&json)
78            .expect("Could not parse JSON test file");
79        assert_eq!(
80            cop_metadata.tpe,
81            TypedCrossoriginPolicy::<()>::typed_cdni_metadata_name()
82        );
83    }
84}