Skip to content

Commit

Permalink
token_parser: simplify AddColumnRoute serialization
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Jan 14, 2025
1 parent 78ac173 commit b84a314
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
24 changes: 18 additions & 6 deletions crates/notedeck_columns/src/storage/token_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
}
}
}
25 changes: 9 additions & 16 deletions crates/notedeck_columns/src/ui/add_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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),
Expand Down

0 comments on commit b84a314

Please sign in to comment.