tokens: add TimelineRoute token serializer
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -30,7 +30,7 @@ pub mod kind;
|
||||
pub mod route;
|
||||
|
||||
pub use cache::{TimelineCache, TimelineCacheKey};
|
||||
pub use kind::{AlgoTimeline, ColumnTitle, PubkeySource, TimelineKind};
|
||||
pub use kind::{ColumnTitle, PubkeySource, TimelineKind};
|
||||
pub use route::TimelineRoute;
|
||||
|
||||
#[derive(Debug, Hash, Copy, Clone, Eq, PartialEq)]
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::{
|
||||
draft::Drafts,
|
||||
nav::RenderNavAction,
|
||||
profile::ProfileAction,
|
||||
storage::{ParseError, Payload, Token, TokenParser, TokenSerializable, TokenWriter},
|
||||
timeline::{TimelineCache, TimelineId, TimelineKind},
|
||||
ui::{
|
||||
self,
|
||||
@@ -24,6 +25,67 @@ pub enum TimelineRoute {
|
||||
Quote(NoteId),
|
||||
}
|
||||
|
||||
const PROFILE_TOKENS: &[Token] = &[Token::id("profile"), Token::pubkey()];
|
||||
const THREAD_TOKENS: &[Token] = &[Token::id("thread"), Token::note_id()];
|
||||
const REPLY_TOKENS: &[Token] = &[Token::id("reply"), Token::note_id()];
|
||||
const QUOTE_TOKENS: &[Token] = &[Token::id("quote"), Token::note_id()];
|
||||
|
||||
impl TimelineRoute {
|
||||
fn payload(&self) -> Option<Payload> {
|
||||
match self {
|
||||
TimelineRoute::Profile(pk) => Some(Payload::pubkey(*pk)),
|
||||
TimelineRoute::Thread(note_id) => Some(Payload::note_id(*note_id)),
|
||||
TimelineRoute::Reply(note_id) => Some(Payload::note_id(*note_id)),
|
||||
TimelineRoute::Quote(note_id) => Some(Payload::note_id(*note_id)),
|
||||
TimelineRoute::Timeline(_timeline_id) => todo!("handle timeline_ids"),
|
||||
}
|
||||
}
|
||||
|
||||
fn tokens(&self) -> &'static [Token] {
|
||||
match self {
|
||||
TimelineRoute::Profile(_) => PROFILE_TOKENS,
|
||||
TimelineRoute::Thread(_) => THREAD_TOKENS,
|
||||
TimelineRoute::Reply(_) => REPLY_TOKENS,
|
||||
TimelineRoute::Quote(_) => QUOTE_TOKENS,
|
||||
TimelineRoute::Timeline(_) => todo!("handle timeline_ids"),
|
||||
}
|
||||
}
|
||||
|
||||
/// NOTE!! update parse_from_tokens as well when adding to this match
|
||||
fn parse<'a>(&self, parser: &mut TokenParser<'a>) -> Result<TimelineRoute, ParseError<'a>> {
|
||||
let payload = Token::parse_all(parser, self.tokens())?;
|
||||
|
||||
match self {
|
||||
TimelineRoute::Profile(_) => {
|
||||
Ok(TimelineRoute::Profile(Payload::parse_pubkey(payload)?))
|
||||
}
|
||||
TimelineRoute::Thread(_) => Ok(TimelineRoute::Thread(Payload::parse_note_id(payload)?)),
|
||||
TimelineRoute::Reply(_) => Ok(TimelineRoute::Reply(Payload::parse_note_id(payload)?)),
|
||||
TimelineRoute::Quote(_) => Ok(TimelineRoute::Quote(Payload::parse_note_id(payload)?)),
|
||||
TimelineRoute::Timeline(_) => todo!("handle timeline parsing"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenSerializable for TimelineRoute {
|
||||
fn serialize_tokens(&self, writer: &mut TokenWriter) {
|
||||
Token::serialize_all(writer, self.tokens(), self.payload().as_ref());
|
||||
}
|
||||
|
||||
fn parse_from_tokens<'a>(parser: &mut TokenParser<'a>) -> Result<Self, ParseError<'a>> {
|
||||
TokenParser::alt(
|
||||
parser,
|
||||
&[
|
||||
|p| TimelineRoute::Profile(Pubkey::new([0; 32])).parse(p),
|
||||
|p| TimelineRoute::Thread(NoteId::new([0; 32])).parse(p),
|
||||
|p| TimelineRoute::Reply(NoteId::new([0; 32])).parse(p),
|
||||
|p| TimelineRoute::Quote(NoteId::new([0; 32])).parse(p),
|
||||
|_p| todo!("handle timeline parsing"),
|
||||
],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn render_timeline_route(
|
||||
ndb: &Ndb,
|
||||
@@ -193,3 +255,28 @@ pub fn render_profile_route(
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::storage::{TokenParser, TokenSerializable, TokenWriter};
|
||||
use enostr::NoteId;
|
||||
|
||||
#[test]
|
||||
fn test_timeline_route_serialize() {
|
||||
use super::TimelineRoute;
|
||||
|
||||
{
|
||||
let note_id_hex = "1c54e5b0c386425f7e017d9e068ddef8962eb2ce1bb08ed27e24b93411c12e60";
|
||||
let note_id = NoteId::from_hex(note_id_hex).unwrap();
|
||||
let data_str = format!("thread:{}", note_id_hex);
|
||||
let data = &data_str.split(":").collect::<Vec<&str>>();
|
||||
let mut token_writer = TokenWriter::default();
|
||||
let mut parser = TokenParser::new(&data);
|
||||
let parsed = TimelineRoute::parse_from_tokens(&mut parser).unwrap();
|
||||
let expected = TimelineRoute::Thread(note_id);
|
||||
parsed.serialize_tokens(&mut token_writer);
|
||||
assert_eq!(expected, parsed);
|
||||
assert_eq!(token_writer.str(), data_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user