pub struct Context<S: ContextState + ?Sized> { /* private fields */ }Expand description
Tiny wrapper around “Tera context” with some additional information.
Implementations§
Source§impl<S: ContextState> Context<S>
The methods below are available in all ContentState states.
impl<S: ContextState> Context<S>
The methods below are available in all ContentState states.
Sourcepub fn get_dir_path(&self) -> &Path
pub fn get_dir_path(&self) -> &Path
Getter for self.dir_path.
See from() method for details.
Sourcepub fn get_root_path(&self) -> &Path
pub fn get_root_path(&self) -> &Path
Getter for self.root_path.
See from() method for details.
Sourcepub fn get_doc_file_date(&self) -> Option<SystemTime>
pub fn get_doc_file_date(&self) -> Option<SystemTime>
Getter for self.doc_file_date.
See from() method for details.
Sourcepub fn from_context_path(context: &Context<S>) -> Context<HasSettings>
pub fn from_context_path(context: &Context<S>) -> Context<HasSettings>
Constructor. Unlike from() this constructor does not access
the filesystem in order to detect dir_path, root_path and
doc_file_date. It copies these values from the passed context.
Use this constructor when you are sure that the above date has
not changed since you instantiated context. In this case you
can avoid repeated file access.
Source§impl Context<Invalid>
The start state of all Context objects.
impl Context<Invalid>
The start state of all Context objects.
Sourcepub fn from(
path: &Path,
root_path: PathBuf,
) -> Result<Context<HasSettings>, FileError>
pub fn from( path: &Path, root_path: PathBuf, ) -> Result<Context<HasSettings>, FileError>
Constructor: path is Tp-Notes first positional command line parameter
<path> (see man page). path must point to a directory or
a file.
root_path is the document root: the directory Tp-Note’s viewer
treats as its security boundary for rewriting local links, and the
value stored under the TMPL_VAR_ROOT_PATH template key. This
constructor does not discover it: the caller must supply it, e.g. by
searching upward for a tpnote.toml project configuration file (a
convention the tpnote binary implements; cf. its CUSTOMIZATION man
page section) or by any other means appropriate to the embedding
application.
A copy of path is stored in self.ct as key TMPL_VAR_PATH. Its
directory path as key TMPL_VAR_DIR_PATH. If path points to a
file, its file creation date is stored with the key
TMPL_VAR_DOC_FILE_DATE.
use std::path::Path;
use tpnote_lib::settings::set_test_default_settings;
use tpnote_lib::config::TMPL_VAR_DIR_PATH;
use tpnote_lib::config::TMPL_VAR_PATH;
use tpnote_lib::context::Context;
set_test_default_settings().unwrap();
let mut context = Context::from(&Path::new("/path/to/mynote.md"), Path::new("/path/to").to_path_buf()).unwrap();
assert_eq!(context.get_path(), Path::new("/path/to/mynote.md"));
assert_eq!(context.get_dir_path(), Path::new("/path/to/"));
assert_eq!(&context.get(TMPL_VAR_PATH).unwrap().to_string(),
"/path/to/mynote.md");
assert_eq!(&context.get(TMPL_VAR_DIR_PATH).unwrap().to_string(),
"/path/to");§Panics
Debug builds assert that path’s directory is a subdirectory of
root_path, catching a caller that passes a root_path computed
for a different directory.
Trait Implementations§
Source§impl<S: ContextState> Deref for Context<S>
Auto dereferences for convenient access to tera::Context.
impl<S: ContextState> Deref for Context<S>
Auto dereferences for convenient access to tera::Context.
impl<S: PartialEq + ContextState + ?Sized> StructuralPartialEq for Context<S>
Auto Trait Implementations§
impl<S> Freeze for Context<S>
impl<S> RefUnwindSafe for Context<S>
impl<S> Send for Context<S>
impl<S> Sync for Context<S>
impl<S> Unpin for Context<S>
impl<S> UnsafeUnpin for Context<S>
impl<S> UnwindSafe for Context<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more