chiark / gitweb /
Turn FileType::get_from_client into an &self method.
authorSimon Tatham <anakin@pobox.com>
Fri, 26 Jan 2024 08:48:04 +0000 (08:48 +0000)
committerSimon Tatham <anakin@pobox.com>
Fri, 26 Jan 2024 12:40:11 +0000 (12:40 +0000)
This enables the FileType object itself to store information about
where its data comes from, instead of _necessarily_ being constrained
to get it from the client.

Therefore, I've also renamed it, so that the method name doesn't imply
that the data always lives in the client.

This also means that every call to FileContents::update_items() must
pass through the FileType from its owning File, but that's no trouble.

src/file.rs

index 5fd8785839be5f752607aa0518e7334599e35087..915529eac311f9c70c06c8978383a461abaffac4 100644 (file)
@@ -173,7 +173,8 @@ trait FileType {
     const CAN_GET_POSTS: bool = false;
     const IS_EXAMINE_USER: bool = false;
 
-    fn get_from_client(
+    fn get_item(
+        &self,
         id: &str,
         client: &mut Client,
     ) -> Result<Self::Item, ClientError>;
@@ -197,7 +198,8 @@ impl StatusFeedType {
 impl FileType for StatusFeedType {
     type Item = StatusDisplay;
 
-    fn get_from_client(
+    fn get_item(
+        &self,
         id: &str,
         client: &mut Client,
     ) -> Result<Self::Item, ClientError> {
@@ -221,7 +223,8 @@ impl NotificationStatusFeedType {
 impl FileType for NotificationStatusFeedType {
     type Item = StatusDisplay;
 
-    fn get_from_client(
+    fn get_item(
+        &self,
         id: &str,
         client: &mut Client,
     ) -> Result<Self::Item, ClientError> {
@@ -248,7 +251,8 @@ impl EgoNotificationFeedType {
 impl FileType for EgoNotificationFeedType {
     type Item = NotificationLog;
 
-    fn get_from_client(
+    fn get_item(
+        &self,
         id: &str,
         client: &mut Client,
     ) -> Result<Self::Item, ClientError> {
@@ -265,7 +269,8 @@ struct UserListFeedType {}
 impl FileType for UserListFeedType {
     type Item = UserListEntry;
 
-    fn get_from_client(
+    fn get_item(
+        &self,
         id: &str,
         client: &mut Client,
     ) -> Result<Self::Item, ClientError> {
@@ -283,7 +288,7 @@ struct FileContents<Type: FileType, Source: FileDataSource> {
 }
 
 impl<Type: FileType, Source: FileDataSource> FileContents<Type, Source> {
-    fn update_items(&mut self, client: &mut Client) {
+    fn update_items(&mut self, file_desc: &Type, client: &mut Client) {
         // FIXME: if the feed has been extended rather than created,
         // we should be able to make less effort than this. But we
         // still need to regenerate any item derived from something we
@@ -295,7 +300,8 @@ impl<Type: FileType, Source: FileDataSource> FileContents<Type, Source> {
 
         self.items.clear();
         for id in ids {
-            let item = Type::get_from_client(&id, client)
+            let item = file_desc
+                .get_item(&id, client)
                 .expect("Any id stored in a Feed should also be cached");
             self.items.push((id.to_owned(), item));
         }
@@ -463,7 +469,7 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
             items: Vec::new(),
         };
 
-        contents.update_items(client);
+        contents.update_items(&file_desc, client);
 
         // Start with the initial position at the file top
         let mut initial_pos = FilePosition::item_top(contents.first_index());
@@ -763,7 +769,7 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
                     }
                 }
 
-                self.contents.update_items(client);
+                self.contents.update_items(&self.file_desc, client);
                 self.clip_pos_within_item();
                 self.fix_overshoot_at_top(); // in case the extender vanished
                 Ok(())
@@ -1014,7 +1020,8 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
                 SelectionPurpose::Favourite => {
                     match client.favourite_post(&id, !alt) {
                         Ok(_) => {
-                            self.contents.update_items(client);
+                            self.contents
+                                .update_items(&self.file_desc, client);
                             LogicalAction::Nothing
                         }
                         Err(_) => LogicalAction::Beep,
@@ -1023,7 +1030,8 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
                 SelectionPurpose::Boost => {
                     match client.boost_post(&id, !alt) {
                         Ok(_) => {
-                            self.contents.update_items(client);
+                            self.contents
+                                .update_items(&self.file_desc, client);
                             LogicalAction::Nothing
                         }
                         Err(_) => LogicalAction::Beep,
@@ -1062,7 +1070,8 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
                             .copied(),
                     ) {
                         Ok(_) => {
-                            self.contents.update_items(client);
+                            self.contents
+                                .update_items(&self.file_desc, client);
                             LogicalAction::Nothing
                         }
                         Err(_) => LogicalAction::Beep,
@@ -1720,7 +1729,7 @@ impl<Type: FileType, Source: FileDataSource> ActivityState
         client: &mut Client,
     ) {
         if self.contents.source.updated(feeds_updated) {
-            self.contents.update_items(client);
+            self.contents.update_items(&self.file_desc, client);
             self.ensure_enough_rendered();
 
             // If someone's home feed was almost completely empty,
@@ -1760,7 +1769,7 @@ impl<Type: FileType, Source: FileDataSource> ActivityState
     }
 
     fn show_new_content(&mut self, client: &mut Client) {
-        self.contents.update_items(client);
+        self.contents.update_items(&self.file_desc, client);
         if let Some(index) = self.latest_read_index {
             if self.contents.last_index() > index {
                 self.pos = FilePosition::item_top(index + 1);
@@ -2026,7 +2035,8 @@ impl FileType for ExamineUserFileType {
     const CAN_GET_POSTS: bool = true;
     const IS_EXAMINE_USER: bool = true;
 
-    fn get_from_client(
+    fn get_item(
+        &self,
         id: &str,
         client: &mut Client,
     ) -> Result<Self::Item, ClientError> {
@@ -2063,7 +2073,8 @@ impl FileType for DetailedStatusFileType {
     type Item = DetailedStatusDisplay;
     const CAN_LIST: CanList = CanList::ForPost;
 
-    fn get_from_client(
+    fn get_item(
+        &self,
         id: &str,
         client: &mut Client,
     ) -> Result<Self::Item, ClientError> {
@@ -2099,7 +2110,8 @@ struct InstanceRulesFileType;
 impl FileType for InstanceRulesFileType {
     type Item = InstanceRulesDisplay;
 
-    fn get_from_client(
+    fn get_item(
+        &self,
         _id: &str,
         client: &mut Client,
     ) -> Result<Self::Item, ClientError> {