libdaw/notation/
rest.rs

1mod parse;
2
3use super::resolve_state::ResolveState;
4use crate::{metronome::Beat, parse::IResult};
5use nom::{combinator::all_consuming, error::convert_error, Finish as _};
6use std::str::FromStr;
7
8#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
9pub struct Rest {
10    // Conceptual length of the note in beats
11    pub length: Option<Beat>,
12}
13
14impl Rest {
15    pub fn inner_length(&self, state: &ResolveState) -> Beat {
16        self.length.unwrap_or(state.length)
17    }
18    pub const fn duration(&self) -> Beat {
19        Beat::ZERO
20    }
21    pub fn length(&self) -> Beat {
22        self.inner_length(&Default::default())
23    }
24    pub fn parse(input: &str) -> IResult<&str, Self> {
25        parse::rest(input)
26    }
27    pub(super) fn update_state(&self, state: &mut ResolveState) {
28        state.length = self.inner_length(state);
29    }
30}
31
32impl FromStr for Rest {
33    type Err = String;
34
35    fn from_str(s: &str) -> Result<Self, Self::Err> {
36        let note = all_consuming(parse::rest)(s)
37            .finish()
38            .map_err(move |e| convert_error(s, e))?
39            .1;
40        Ok(note)
41    }
42}