pub struct Directive {
pub name: String,
pub value: Option<String>,
pub kind: DirectiveKind,
pub selector: Option<String>,
}Expand description
A ChordPro directive such as {title: My Song} or {start_of_chorus}.
Directives are enclosed in curly braces and consist of a name and an
optional value separated by a colon. Some directives have standard short
aliases (e.g., t for title, st for subtitle).
The name field stores the canonical (long-form, lowercase) name after
alias resolution. The kind field provides a typed classification for
pattern matching.
§Selector Suffixes
Directives may carry an optional selector suffix that targets a specific
instrument or user. The selector is separated from the directive name by a
hyphen (e.g., {textfont-piano: Courier} has selector "piano"). The
parser splits the raw directive name at the last hyphen to detect
selectors: if the prefix resolves to a known directive, the suffix is
stored in selector; otherwise the entire name is treated as a single
(possibly unknown) directive with no selector.
§Examples
use chordsketch_core::ast::{Directive, DirectiveKind};
// {title: My Song}
let d = Directive::with_value("title", "My Song");
assert_eq!(d.name, "title");
assert_eq!(d.value.as_deref(), Some("My Song"));
assert_eq!(d.kind, DirectiveKind::Title);
assert_eq!(d.selector, None);
// {start_of_chorus}
let d = Directive::name_only("start_of_chorus");
assert_eq!(d.name, "start_of_chorus");
assert!(d.value.is_none());
assert_eq!(d.kind, DirectiveKind::StartOfChorus);
assert_eq!(d.selector, None);Fields§
§name: StringThe canonical directive name (e.g., "title", "start_of_chorus").
value: Option<String>The optional value after the colon (e.g., "My Song" in {title: My Song}).
kind: DirectiveKindThe classified kind of this directive.
selector: Option<String>An optional selector suffix for instrument/user targeting.
For example, {textfont-piano: Courier} has selector = Some("piano").
When no selector suffix is present, this is None.
Implementations§
Source§impl Directive
impl Directive
Sourcepub fn with_value(name: impl Into<String>, value: impl Into<String>) -> Self
pub fn with_value(name: impl Into<String>, value: impl Into<String>) -> Self
Creates a directive with both a name and a value.
The name is resolved to its canonical form and the DirectiveKind
is determined automatically.
Sourcepub fn name_only(name: impl Into<String>) -> Self
pub fn name_only(name: impl Into<String>) -> Self
Creates a directive with only a name and no value.
The name is resolved to its canonical form and the DirectiveKind
is determined automatically.
Sourcepub fn with_selector(
name: impl Into<String>,
value: Option<String>,
selector: impl Into<String>,
) -> Self
pub fn with_selector( name: impl Into<String>, value: Option<String>, selector: impl Into<String>, ) -> Self
Creates a directive with a name, value, and selector suffix.
The name is resolved to its canonical form and the DirectiveKind
is determined automatically.
Sourcepub fn is_section_start(&self) -> bool
pub fn is_section_start(&self) -> bool
Returns true if this directive marks the start of a section
(e.g., start_of_chorus, start_of_verse, etc.).
Sourcepub fn is_section_end(&self) -> bool
pub fn is_section_end(&self) -> bool
Returns true if this directive marks the end of a section
(e.g., end_of_chorus, end_of_verse, etc.).
Sourcepub fn section_name(&self) -> Option<&str>
pub fn section_name(&self) -> Option<&str>
If this is a section start or end directive, returns the section name
(e.g., "chorus" from "start_of_chorus").