Struct Context

Source
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.

Source

pub fn get_path(&self) -> &Path

Getter for self.path. See from() method for details.

Source

pub fn get_dir_path(&self) -> &Path

Getter for self.dir_path. See from() method for details.

Source

pub fn get_root_path(&self) -> &Path

Getter for self.root_path. See from() method for details.

Source

pub fn get_doc_file_date(&self) -> Option<SystemTime>

Getter for self.doc_file_date. See from() method for details.

Source

pub fn from_context_path(context: &Context<S>) -> Context<HasSettings>

Constructor. Unlike from() this constructor does not access the file system 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.

Source

pub fn from(path: &Path) -> 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.

A copy of path is stored in self.ct as key TMPL_VAR_PATH. It directory path as key TMPL_VAR_DIR_PATH. The root directory, where the marker file tpnote.toml was found, is stored with the key TMPL_VAR_ROOT_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")).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(),
            r#""/path/to/mynote.md""#);
assert_eq!(&context.get(TMPL_VAR_DIR_PATH).unwrap().to_string(),
            r#""/path/to""#);

Methods from Deref<Target = Context>§

Source

pub fn get(&self, index: &str) -> Option<&Value>

Returns the value at a given key index.

Source

pub fn contains_key(&self, index: &str) -> bool

Checks if a value exists at a specific index.

Trait Implementations§

Source§

impl<S: Clone + ContextState + ?Sized> Clone for Context<S>

Source§

fn clone(&self) -> Context<S>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + ContextState + ?Sized> Debug for Context<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: ContextState> Deref for Context<S>

Auto dereferences for convenient access to tera::Context.

Source§

type Target = Context

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S: PartialEq + ContextState + ?Sized> PartialEq for Context<S>

Source§

fn eq(&self, other: &Context<S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S: ContextState + ?Sized> StructuralPartialEq for Context<S>

Auto Trait Implementations§

§

impl<S> Freeze for Context<S>
where S: ?Sized,

§

impl<S> RefUnwindSafe for Context<S>
where S: RefUnwindSafe + ?Sized,

§

impl<S> Send for Context<S>
where S: Send + ?Sized,

§

impl<S> Sync for Context<S>
where S: Sync + ?Sized,

§

impl<S> Unpin for Context<S>
where S: Unpin + ?Sized,

§

impl<S> UnwindSafe for Context<S>
where S: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,