Skip to main content

Directive

Struct Directive 

Source
#[non_exhaustive]
pub struct Directive { pub name: String, pub raw_value: Option<String>, }
Expand description

A single directive within a policy: a name and an optional raw value (CSP3 §2.3, serialized-directive).

raw_value is exactly the substring that followed the directive name, with only the separating whitespace run stripped – it is not yet parsed against any directive-specific grammar.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§name: String

The directive name, exactly as it appeared in the input (original casing preserved – CSP3 directive-name matching is ASCII-case- insensitive, see Directive::name_is_valid and crate::registry_lookup).

§raw_value: Option<String>

The raw value that followed the directive name, if any.

Implementations§

Source§

impl Directive

Source

pub fn name_is_valid(&self) -> bool

Whether Directive::name matches the ABNF directive-name production (1*( ALPHA / DIGIT / "-" ), CSP3 §2.3).

A false result does not mean this directive was dropped – csp-parse parses leniently (see plan/DECISIONS.md, 2026-08-22) and still reports it; callers that need strict conformance checking should check this explicitly.

Source

pub fn value_is_valid(&self) -> bool

Whether Directive::raw_value (if present) matches the ABNF directive-value production (CSP3 §2.3): any run of ASCII whitespace or a byte in %x21-2B / %x2D-3A / %x3C-7E (printable ASCII excluding , and ;).

Source§

impl Directive

Source

pub fn value(&self) -> DirectiveValue

Interprets Directive::raw_value via the CSP3 directive registry for Directive::name. Computed on demand rather than stored on Directive itself, so crate::parse_policy_list (Phase 02) stays a pure, registry-independent split – see plan/DECISIONS.md, 2026-08-22.

Examples found in repository?
examples/parse.rs (line 20)
6fn main() {
7    let policy = "default-src 'self'; \
8                  script-src 'self' 'nonce-2726c7f26c' https://cdn.example.com; \
9                  img-src 'self' data:; \
10                  frame-ancestors 'none'; \
11                  upgrade-insecure-requests; \
12                  report-to csp-endpoint";
13
14    println!("Parsing: {policy}\n");
15
16    let policy_list = parse_policy_list(policy);
17    for policy in &policy_list.policies {
18        for directive in &policy.directives {
19            println!("{}:", directive.name);
20            match directive.value() {
21                DirectiveValue::SourceList(list) => println!("  source list: {list:?}"),
22                DirectiveValue::AncestorSourceList(list) => {
23                    println!("  ancestor source list: {list:?}");
24                }
25                DirectiveValue::Boolean => println!("  (no value)"),
26                DirectiveValue::Token(token) => println!("  token: {token:?}"),
27                other => println!("  {other:?}"),
28            }
29        }
30    }
31}
Source

pub fn boolean_value_is_unexpected(&self) -> bool

For a ValueGrammar::Boolean directive (e.g. upgrade-insecure-requests): whether it unexpectedly carries a value. CSP3 defines these directives as valueless – a present value is a caller-visible anomaly (a diagnostic), not silently dropped or a panic. false for non-boolean or unregistered directives.

Trait Implementations§

Source§

impl Clone for Directive

Source§

fn clone(&self) -> Directive

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Directive

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Directive

Source§

impl PartialEq for Directive

Source§

fn eq(&self, other: &Directive) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Directive

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.