ratatui_toolkit/widgets/markdown_widget/foundation/source/constructors/from_file.rs
1//! Constructor for creating a `MarkdownSource` from a file.
2
3use std::fs;
4use std::io;
5use std::path::Path;
6
7use super::super::MarkdownSource;
8
9impl MarkdownSource {
10 /// Create a new `MarkdownSource` from a file path.
11 ///
12 /// Reads the file content immediately and caches it.
13 ///
14 /// # Arguments
15 /// * `path` - Path to the markdown file.
16 ///
17 /// # Errors
18 /// Returns an `io::Error` if the file cannot be read.
19 pub fn from_file(path: impl AsRef<Path>) -> io::Result<Self> {
20 let path = path.as_ref().to_path_buf();
21 let content = fs::read_to_string(&path)?;
22 Ok(Self::File { path, content })
23 }
24}