token_parser: simplify AddColumnRoute serialization
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -56,6 +56,12 @@ impl TokenWriter {
|
||||
self.tokens_written += 1;
|
||||
}
|
||||
|
||||
pub fn str(&self) -> &str {
|
||||
// SAFETY: only &strs are ever serialized, so its guaranteed to be
|
||||
// correct here
|
||||
unsafe { std::str::from_utf8_unchecked(self.buffer()) }
|
||||
}
|
||||
|
||||
pub fn buffer(&self) -> &[u8] {
|
||||
&self.buf
|
||||
}
|
||||
@@ -194,21 +200,27 @@ mod tests {
|
||||
use crate::ui::add_column::{AddAlgoRoute, AddColumnRoute};
|
||||
|
||||
{
|
||||
let data = &"column:algo_selection:last_per_pubkey"
|
||||
.split(":")
|
||||
.collect::<Vec<&str>>();
|
||||
let data_str = "column:algo_selection:last_per_pubkey";
|
||||
let data = &data_str.split(":").collect::<Vec<&str>>();
|
||||
let mut token_writer = TokenWriter::default();
|
||||
let mut parser = TokenParser::new(&data);
|
||||
let parsed = AddColumnRoute::parse(&mut parser).unwrap();
|
||||
let expected = AddColumnRoute::Algo(AddAlgoRoute::LastPerPubkey);
|
||||
assert_eq!(expected, parsed)
|
||||
parsed.serialize(&mut token_writer);
|
||||
assert_eq!(expected, parsed);
|
||||
assert_eq!(token_writer.str(), data_str);
|
||||
}
|
||||
|
||||
{
|
||||
let data: &[&str] = &["column"];
|
||||
let data_str = "column";
|
||||
let mut token_writer = TokenWriter::default();
|
||||
let data: &[&str] = &[data_str];
|
||||
let mut parser = TokenParser::new(data);
|
||||
let parsed = AddColumnRoute::parse(&mut parser).unwrap();
|
||||
let expected = AddColumnRoute::Base;
|
||||
assert_eq!(expected, parsed)
|
||||
parsed.serialize(&mut token_writer);
|
||||
assert_eq!(expected, parsed);
|
||||
assert_eq!(token_writer.str(), data_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,14 +97,14 @@ impl AddColumnRoute {
|
||||
/// Route tokens use in both serialization and deserialization
|
||||
fn tokens(&self) -> &'static [&'static str] {
|
||||
match self {
|
||||
Self::Base => &[],
|
||||
Self::UndecidedNotification => &["notification_selection"],
|
||||
Self::ExternalNotification => &["external_notif_selection"],
|
||||
Self::UndecidedIndividual => &["individual_selection"],
|
||||
Self::ExternalIndividual => &["external_individual_selection"],
|
||||
Self::Hashtag => &["hashtag"],
|
||||
Self::Algo(AddAlgoRoute::Base) => &["algo_selection"],
|
||||
Self::Algo(AddAlgoRoute::LastPerPubkey) => &["algo_selection", "last_per_pubkey"],
|
||||
Self::Base => &["column"],
|
||||
Self::UndecidedNotification => &["column", "notification_selection"],
|
||||
Self::ExternalNotification => &["column", "external_notif_selection"],
|
||||
Self::UndecidedIndividual => &["column", "individual_selection"],
|
||||
Self::ExternalIndividual => &["column", "external_individual_selection"],
|
||||
Self::Hashtag => &["column", "hashtag"],
|
||||
Self::Algo(AddAlgoRoute::Base) => &["column", "algo_selection"],
|
||||
Self::Algo(AddAlgoRoute::LastPerPubkey) => &["column", "algo_selection", "last_per_pubkey"],
|
||||
// NOTE!!! When adding to this, update the parser for TokenSerializable below
|
||||
}
|
||||
}
|
||||
@@ -118,17 +118,10 @@ impl TokenSerializable for AddColumnRoute {
|
||||
}
|
||||
|
||||
fn parse<'a>(parser: &mut TokenParser<'a>) -> Result<Self, ParseError<'a>> {
|
||||
// all start with column
|
||||
parser.parse_token("column")?;
|
||||
|
||||
// if we're done then we have the base
|
||||
if parser.is_eof() {
|
||||
return Ok(AddColumnRoute::Base);
|
||||
}
|
||||
|
||||
TokenParser::alt(
|
||||
parser,
|
||||
&[
|
||||
|p| parse_column_route(p, AddColumnRoute::Base),
|
||||
|p| parse_column_route(p, AddColumnRoute::UndecidedNotification),
|
||||
|p| parse_column_route(p, AddColumnRoute::ExternalNotification),
|
||||
|p| parse_column_route(p, AddColumnRoute::UndecidedIndividual),
|
||||
|
||||
Reference in New Issue
Block a user