From: Simon Tatham Date: Fri, 26 Jan 2024 12:53:49 +0000 (+0000) Subject: Give File an infallible constructor. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=654e626e7cd61eec54ff599fee4bdbb7f3cc591b;p=mastodonochrome.git Give File an infallible constructor. This is useful for data sources whose init() method doesn't actually need to do anything, because it allows the function constructing one to statically know that no error will be raised, and not have to write an ugly unwrap(). --- diff --git a/src/file.rs b/src/file.rs index 915529e..f29425b 100644 --- a/src/file.rs +++ b/src/file.rs @@ -454,7 +454,20 @@ impl File { show_new: bool, ) -> Result { source.init(client)?; + Ok(Self::new_infallible( + client, source, desc, file_desc, saved_pos, unfolded, show_new, + )) + } + fn new_infallible( + client: &mut Client, + source: Source, + desc: ColouredString, + file_desc: Type, + saved_pos: Option<&SavedFilePos>, + unfolded: Option>>>, + show_new: bool, + ) -> Self { let extender = if source.extendable() { Some(ExtendableIndicator::new()) } else { @@ -506,7 +519,7 @@ impl File { initial_pos = initial_pos.clip(contents.first_index(), contents.last_index()); - let ff = File { + File { contents, rendered: HashMap::new(), pos: initial_pos, @@ -520,8 +533,7 @@ impl File { search_direction: None, last_search: None, latest_read_index, - }; - Ok(ff) + } } fn ensure_item_rendered(