ratatui_toolkit/widgets/markdown_widget/foundation/source/methods/reload.rs
1//! Method to reload content from a file.
2
3use std::fs;
4use std::io;
5
6use super::super::MarkdownSource;
7
8impl MarkdownSource {
9 /// Reload the content from the file.
10 ///
11 /// For string sources, this is a no-op and returns `Ok(false)`.
12 /// For file sources, this re-reads the file and returns `Ok(true)` if
13 /// the content has changed.
14 ///
15 /// # Errors
16 /// Returns an `io::Error` if the file cannot be read.
17 pub fn reload(&mut self) -> io::Result<bool> {
18 match self {
19 Self::String(_) => Ok(false),
20 Self::File { path, content } => {
21 let new_content = fs::read_to_string(&*path)?;
22 if new_content != *content {
23 *content = new_content;
24 Ok(true)
25 } else {
26 Ok(false)
27 }
28 }
29 }
30 }
31}