katana_markdown_engine/
input.rs1use crate::KmeError;
2use std::path::{Path, PathBuf};
3
4#[derive(Debug, Clone)]
5pub enum MarkdownInput {
6 Content { path: PathBuf, content: String },
7 File { path: PathBuf },
8}
9
10impl MarkdownInput {
11 pub fn from_content(path: impl Into<PathBuf>, content: impl Into<String>) -> Self {
12 Self::Content {
13 path: path.into(),
14 content: content.into(),
15 }
16 }
17
18 pub fn from_file(path: impl Into<PathBuf>) -> Self {
19 Self::File { path: path.into() }
20 }
21
22 pub(crate) fn into_parts(self) -> Result<(PathBuf, String), KmeError> {
23 match self {
24 Self::Content { path, content } => Ok((path, content)),
25 Self::File { path } => Self::read_file(&path),
26 }
27 }
28
29 fn read_file(path: &Path) -> Result<(PathBuf, String), KmeError> {
30 let content = std::fs::read_to_string(path).map_err(|source| KmeError::ReadFile {
31 path: path.to_path_buf(),
32 source,
33 })?;
34 Ok((path.to_path_buf(), content))
35 }
36}