From 692cc39c9a93eb552f323efa8e25d7ffd99690ec Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 31 Dec 2023 08:59:03 +0000 Subject: [PATCH] Make FeedFile generic across type of item. Now it can hold some other kind of TextFragment, instead of always being StatusDisplay. --- src/file.rs | 63 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/src/file.rs b/src/file.rs index 6675505..cae8abf 100644 --- a/src/file.rs +++ b/src/file.rs @@ -15,15 +15,34 @@ enum FilePosition { Fine(isize, usize), // line #n of this item is just off bottom of screen } -struct FeedFileContents { +trait FeedType { + type Item: TextFragment + Sized; + + fn get_from_client(id: &str, client: &mut Client) -> + Result; +} + +struct StatusFeedType {} +impl FeedType for StatusFeedType { + type Item = StatusDisplay; + + fn get_from_client(id: &str, client: &mut Client) -> + Result + { + let st = client.status_by_id(&id)?; + Ok(StatusDisplay::new(st.clone(), client)) + } +} + +struct FeedFileContents { id: FeedId, header: FileHeader, extender: Option, origin: isize, - items: Vec>, + items: Vec, } -impl FeedFileContents { +impl FeedFileContents { fn update_items(&mut self, client: &mut Client) { // FIXME: if the feed has been extended rather than created, // we should be able to make less effort than this @@ -34,10 +53,9 @@ impl FeedFileContents { self.items.clear(); for id in ids { - let st = client.status_by_id(&id) - .expect("Any id stored in a Feed should also be cached") - .clone(); - self.items.push(Box::new(StatusDisplay::new(st, client))); + let item = Type::get_from_client(&id, client) + .expect("Any id stored in a Feed should also be cached"); + self.items.push(item); } } @@ -69,19 +87,19 @@ impl FeedFileContents { } } else { let index = self.phys_index(index); - &*self.items[index] + &self.items[index] } } } -struct FeedFile { - contents: FeedFileContents, +struct FeedFile { + contents: FeedFileContents, rendered: HashMap>, pos: FilePosition, last_size: Option<(usize, usize)>, } -impl FeedFile { +impl FeedFile { fn new(client: &mut Client, id: FeedId, desc: ColouredString) -> Result { @@ -278,7 +296,7 @@ impl FeedFile { } } -impl ActivityState for FeedFile { +impl ActivityState for FeedFile { fn resize(&mut self, w: usize, h: usize) { if self.last_size != Some((w, h)) { self.last_size = Some((w, h)); @@ -428,26 +446,29 @@ impl ActivityState for FeedFile { pub fn home_timeline(client: &mut Client) -> Result, ClientError> { - let file = FeedFile::new(client, FeedId::Home, ColouredString::general( - "Home timeline ", - "HHHHHHHHHHHHHHHHHKH"))?; + let file = FeedFile::::new( + client, FeedId::Home, ColouredString::general( + "Home timeline ", + "HHHHHHHHHHHHHHHHHKH"))?; Ok(Box::new(file)) } pub fn local_timeline(client: &mut Client) -> Result, ClientError> { - let file = FeedFile::new(client, FeedId::Local, ColouredString::general( - "Local public timeline ", - "HHHHHHHHHHHHHHHHHHHHHHHHHKH"))?; + let file = FeedFile::::new( + client, FeedId::Local, ColouredString::general( + "Local public timeline ", + "HHHHHHHHHHHHHHHHHHHHHHHHHKH"))?; Ok(Box::new(file)) } pub fn public_timeline(client: &mut Client) -> Result, ClientError> { - let file = FeedFile::new(client, FeedId::Public, ColouredString::general( - "Public timeline

", - "HHHHHHHHHHHHHHHHHHHKH"))?; + let file = FeedFile::::new( + client, FeedId::Public, ColouredString::general( + "Public timeline

", + "HHHHHHHHHHHHHHHHHHHKH"))?; Ok(Box::new(file)) } -- 2.30.2