Skip to main content

Directive

Struct Directive 

Source
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: String

The 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: DirectiveKind

The 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

Source

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.

Source

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.

Source

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.

Source

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.).

Source

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.).

Source

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").

Trait Implementations§

Source§

impl Clone for Directive

Source§

fn clone(&self) -> Directive

Returns a duplicate of the value. Read more
1.0.0 · 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 PartialEq for Directive

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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.