Skip to main content

tablo_core/declaration/
policy.rs

1//! Why a panel's content security policy cannot be served.
2
3use std::fmt;
4
5/// Why a panel's [`content_security_policy`](crate::Panel::content_security_policy) or a
6/// [`content_security_policy_source`](crate::Panel::content_security_policy_source) cannot be
7/// served.
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum PolicyFault {
11    /// A `Panel::content_security_policy` that is no valid header value: drop its line breaks.
12    InvalidHeader {
13        /// The policy.
14        policy: String,
15    },
16    /// A `Panel::content_security_policy_source` the panel's policy cannot carry: a directive
17    /// that is no lowercase name, a source that is empty or holds whitespace, `;` or `,`, a
18    /// directive the policy leaves out that is no fetch directive, or any source on a panel
19    /// without a policy.
20    ///
21    /// Fix the source, or set the whole policy with `content_security_policy`.
22    InvalidSource {
23        /// The directive.
24        directive: String,
25        /// The source.
26        source: String,
27    },
28}
29
30impl fmt::Display for PolicyFault {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::InvalidHeader { policy } => write!(
34                f,
35                "content_security_policy {policy:?} is not a valid header value"
36            ),
37            Self::InvalidSource { directive, source } => write!(
38                f,
39                "content_security_policy_source({directive:?}, {source:?}) cannot be added to \
40                 the panel's policy: check the directive and the source, or set the whole policy \
41                 with `content_security_policy`"
42            ),
43        }
44    }
45}