1
0
Fork 0
forked from wry/wry

config: allow configuring repeat rates via toml

This commit is contained in:
Julian Orth 2024-04-05 17:25:07 +02:00
parent b374947b45
commit 1a9b7146fd
9 changed files with 241 additions and 2 deletions

View file

@ -0,0 +1,45 @@
use {
crate::{
config::{
context::Context,
extractor::{s32, Extractor, ExtractorError},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
RepeatRate,
},
toml::{
toml_span::{Span, Spanned},
toml_value::Value,
},
},
indexmap::IndexMap,
thiserror::Error,
};
#[derive(Debug, Error)]
pub enum RepeatRateParserError {
#[error(transparent)]
Expected(#[from] UnexpectedDataType),
#[error(transparent)]
Extract(#[from] ExtractorError),
}
pub struct RepeatRateParser<'a>(pub &'a Context<'a>);
impl Parser for RepeatRateParser<'_> {
type Value = RepeatRate;
type Error = RepeatRateParserError;
const EXPECTED: &'static [DataType] = &[DataType::Table];
fn parse_table(
&mut self,
span: Span,
table: &IndexMap<Spanned<String>, Spanned<Value>>,
) -> ParseResult<Self> {
let mut ext = Extractor::new(self.0, span, table);
let (rate, delay) = ext.extract((s32("rate"), s32("delay")))?;
Ok(RepeatRate {
rate: rate.value,
delay: delay.value,
})
}
}