mirror of https://codeberg.org/topola/topola.git
refactor(specctra-core/read): implement read_named via read_optional
This commit is contained in:
parent
50ef279286
commit
81c0de1f91
|
|
@ -17,15 +17,6 @@ impl ListToken {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expect_start(self, valid_names: &[&'static str]) -> Result<(), ParseError> {
|
|
||||||
assert!(!valid_names.is_empty());
|
|
||||||
if self.is_start_of(valid_names) {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ParseError::ExpectedStartOfList(valid_names[0]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn expect_any_start(self) -> Result<String, ParseError> {
|
pub fn expect_any_start(self) -> Result<String, ParseError> {
|
||||||
if let Self::Start { name } = self {
|
if let Self::Start { name } = self {
|
||||||
Ok(name.to_ascii_lowercase())
|
Ok(name.to_ascii_lowercase())
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,6 @@ impl InputToken {
|
||||||
Self { token, context }
|
Self { token, context }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expect_start(self, valid_names: &[&'static str]) -> Result<(), ParseErrorContext> {
|
|
||||||
self.token
|
|
||||||
.expect_start(valid_names)
|
|
||||||
.map_err(|err| err.add_context(self.context))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn expect_any_start(self) -> Result<String, ParseErrorContext> {
|
pub fn expect_any_start(self) -> Result<String, ParseErrorContext> {
|
||||||
self.token
|
self.token
|
||||||
.expect_any_start()
|
.expect_any_start()
|
||||||
|
|
@ -295,10 +289,9 @@ impl<R: std::io::BufRead> ListTokenizer<R> {
|
||||||
&mut self,
|
&mut self,
|
||||||
valid_names: &[&'static str],
|
valid_names: &[&'static str],
|
||||||
) -> Result<T, ParseErrorContext> {
|
) -> Result<T, ParseErrorContext> {
|
||||||
self.consume_token()?.expect_start(valid_names)?;
|
assert!(!valid_names.is_empty());
|
||||||
let value = self.read_value::<T>()?;
|
self.read_optional(valid_names)?
|
||||||
self.consume_token()?.expect_end()?;
|
.ok_or_else(|| self.add_context(ParseError::ExpectedStartOfList(valid_names[0])))
|
||||||
Ok(value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_optional<T: ReadDsn<R>>(
|
pub fn read_optional<T: ReadDsn<R>>(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue