bookdata/layout/
path.rs

1use std::fmt;
2use std::{borrow::Cow, path::PathBuf};
3
4use anyhow::Result;
5use relative_path::RelativePath;
6
7use super::workdir::resolve_path;
8
9/// Paths to book data files (primarily for defining constants, etc.).
10#[derive(Debug, Clone)]
11pub struct BDPath<'a> {
12    path: Cow<'a, str>,
13}
14
15impl<'a> BDPath<'a> {
16    /// Create a new book data path.
17    pub const fn new(path: &'a str) -> BDPath<'a> {
18        BDPath {
19            path: Cow::Borrowed(path),
20        }
21    }
22
23    /// Resolve a path.
24    pub fn resolve(&self) -> Result<PathBuf> {
25        let path = resolve_path(&self)?;
26        Ok(path)
27    }
28}
29
30impl<'a, 'b> AsRef<RelativePath> for &'b BDPath<'a>
31where
32    'b: 'a,
33{
34    fn as_ref(&self) -> &'a RelativePath {
35        RelativePath::new(self.path.as_ref())
36    }
37}
38
39impl<'a> fmt::Display for BDPath<'a> {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.write_str(self.path.as_ref())
42    }
43}