Skip to main content

brooks_lib/verifying/
verify.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//! All Metadata Information except for what is defined in Processing Stages.
19
20use std::fmt::Debug;
21
22use crate::cdni::cors::spec::TypedCrossoriginPolicy;
23use crate::cdni::mel::spec::TypedSetVariable;
24use crate::cdni::original::spec::TypedSourceMetadata;
25use crate::verifying::cors::verify::{
26    CrossoriginPolicyVerificationError, CrossoriginPolicyVerificationKey,
27};
28use crate::verifying::mel::verify::{MelVerificationError, SetVariableVerificationKey};
29use crate::verifying::ps::error::VerificationError;
30use crate::{environment::scope::Scopes, mel::types::Type};
31
32use std::error::Error;
33use std::fmt::Display;
34
35use serde::Serialize;
36
37use crate::verifying::original::verify::SourceVerificationKey;
38use crate::verifying::ps::verify::PsVerificationKey;
39
40use crate::cdni::ps::spec::TypedStage;
41
42use crate::cdni::gmd::spec::TypedGenericMetadata;
43
44#[derive(Debug, Clone, Default)]
45pub struct MetadataAnalysisContext {
46    pub scopes: Scopes<Type>,
47}
48
49#[derive(Debug, Default)]
50pub enum MetadataVerificationError {
51    #[default]
52    NoError,
53    NotProcessingStage,
54    NoProcessor(String),
55    JsonError(String),
56    PsVerificationError(Box<VerificationError>),
57    MelVerificationError(Box<MelVerificationError>),
58    CrossoriginPolicyVerificationError(Box<CrossoriginPolicyVerificationError>),
59}
60
61impl Error for MetadataVerificationError {}
62
63impl Display for MetadataVerificationError {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        match self {
66            MetadataVerificationError::NoError => write!(f, "No error!"),
67            MetadataVerificationError::NotProcessingStage => {
68                write!(f, "Value was not a processing stage!")
69            }
70            MetadataVerificationError::NoProcessor(tpe) => {
71                write!(f, "No processor registered for Metadata with type {tpe}")
72            }
73            MetadataVerificationError::PsVerificationError(pse) => write!(f, "{pse}"),
74            MetadataVerificationError::JsonError(je) => write!(f, "JSON error: {je}"),
75            MetadataVerificationError::MelVerificationError(mel_verification_error) => {
76                write!(f, "{mel_verification_error}")
77            }
78            MetadataVerificationError::CrossoriginPolicyVerificationError(
79                cop_verification_error,
80            ) => {
81                write!(f, "{cop_verification_error}")
82            }
83        }
84    }
85}
86
87#[derive(Debug, Clone, PartialEq)]
88pub enum CdniVerifiedMetadataTypes {
89    ProcessingStages,
90    Source,
91    SetVariable,
92    CrossoriginPolicy,
93    None,
94}
95
96#[derive(Debug, Clone, Default)]
97pub enum CdniVerificationKey {
98    Stage(Box<TypedStage<PsVerificationKey>>),
99    Source(Box<TypedSourceMetadata<SourceVerificationKey>>),
100    SetVariable(Box<TypedSetVariable<SetVariableVerificationKey>>),
101    CrossoriginPolicy(Box<TypedCrossoriginPolicy<CrossoriginPolicyVerificationKey>>),
102    #[default]
103    None,
104}
105
106impl From<&CdniVerificationKey> for CdniVerifiedMetadataTypes {
107    fn from(value: &CdniVerificationKey) -> Self {
108        match value {
109            CdniVerificationKey::Stage(_) => CdniVerifiedMetadataTypes::ProcessingStages,
110            CdniVerificationKey::Source(_) => CdniVerifiedMetadataTypes::Source,
111            CdniVerificationKey::SetVariable(_) => CdniVerifiedMetadataTypes::SetVariable,
112            CdniVerificationKey::CrossoriginPolicy(_) => {
113                CdniVerifiedMetadataTypes::CrossoriginPolicy
114            }
115            CdniVerificationKey::None => CdniVerifiedMetadataTypes::None,
116        }
117    }
118}
119
120impl Serialize for CdniVerificationKey {
121    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
122    where
123        S: serde::Serializer,
124    {
125        serializer.serialize_none()
126    }
127}
128
129//BEGIN: Verifier Trait
130
131/// The result of an analysis of [`TypedGenericMetadata`].
132pub type AnalysisResult<Ck, Error> = Result<(Ck, CdniVerificationKey), Box<Error>>;
133
134/// Generate a validated representation of a given piece of [`TypedGenericMetadata`] information, if possible.
135pub trait Verifier<Ck, Error>: Debug {
136    fn analyze(&self, v: &TypedGenericMetadata<()>, input: Ck) -> AnalysisResult<Ck, Error>;
137
138    fn type_name(&self, candidate: &str) -> bool;
139}