refactor(specctra-core/read): inline expect_* functions from ListToken into InputToken

This commit is contained in:
Alain Emilia Anna Zscheile 2024-12-06 16:35:09 +01:00 committed by mikolaj
parent 81c0de1f91
commit ca012a8c13
2 changed files with 15 additions and 33 deletions

View File

@ -17,30 +17,6 @@ impl ListToken {
}
}
pub fn expect_any_start(self) -> Result<String, ParseError> {
if let Self::Start { name } = self {
Ok(name.to_ascii_lowercase())
} else {
Err(ParseError::ExpectedStartOfList(""))
}
}
pub fn expect_leaf(self) -> Result<String, ParseError> {
if let Self::Leaf { value } = self {
Ok(value)
} else {
Err(ParseError::ExpectedLeaf)
}
}
pub fn expect_end(self) -> Result<(), ParseError> {
if let Self::End = self {
Ok(())
} else {
Err(ParseError::ExpectedEndOfList)
}
}
pub fn len(&self) -> usize {
match &self {
Self::Start { name } => 1 + name.len(),

View File

@ -14,21 +14,27 @@ impl InputToken {
}
pub fn expect_any_start(self) -> Result<String, ParseErrorContext> {
self.token
.expect_any_start()
.map_err(|err| err.add_context(self.context))
if let ListToken::Start { name } = self.token {
Ok(name.to_ascii_lowercase())
} else {
Err(ParseError::ExpectedStartOfList("").add_context(self.context))
}
}
pub fn expect_leaf(self) -> Result<String, ParseErrorContext> {
self.token
.expect_leaf()
.map_err(|err| err.add_context(self.context))
if let ListToken::Leaf { value } = self.token {
Ok(value)
} else {
Err(ParseError::ExpectedLeaf.add_context(self.context))
}
}
pub fn expect_end(self) -> Result<(), ParseErrorContext> {
self.token
.expect_end()
.map_err(|err| err.add_context(self.context))
if let ListToken::End = self.token {
Ok(())
} else {
Err(ParseError::ExpectedEndOfList.add_context(self.context))
}
}
}