#[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
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: StringThe 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
impl Directive
Sourcepub fn name_is_valid(&self) -> bool
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.
Sourcepub fn value_is_valid(&self) -> bool
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
impl Directive
Sourcepub fn value(&self) -> DirectiveValue
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?
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}Sourcepub fn boolean_value_is_unexpected(&self) -> bool
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.