weaver_lib/
partial.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::normalize_line_endings;
6
7#[derive(Debug, Serialize, Deserialize, Default, Clone)]
8pub struct Partial {
9    pub name: String,
10    pub at_path: String,
11    pub contents: String,
12}
13
14impl Partial {
15    pub fn new_from_path(path: PathBuf) -> Self {
16        let contents_result = std::fs::read_to_string(&path);
17
18        if contents_result.is_err() {
19            dbg!("error reading file: {}", contents_result.err());
20            panic!("failed to read '{}'", path.display());
21        }
22
23        let contents = normalize_line_endings(contents_result.as_ref().unwrap().as_bytes());
24
25        Self {
26            at_path: path.display().to_string(),
27            name: path.file_name().unwrap().display().to_string(),
28            contents,
29        }
30    }
31}