Struct Workflow

Source
pub struct Workflow<W> { /* private fields */ }
Expand description

Holds the input data for the run() method.

Implementations§

Source§

impl Workflow<SyncFilename<'_>>

Source

pub fn run<T: Content>(self) -> Result<PathBuf, NoteError>

Starts the “synchronize filename” workflow. Errors can occur in various ways, see NoteError.

First, the workflow opens the note file path on disk and read its YAML front matter. Then, it calculates from the front matter how the filename should be to be in sync. If it is different, rename the note on disk. Finally, it returns the note’s new or existing filename. Repeated calls, will reload the environment variables, but not the configuration file. This function is stateless.

Note: this method holds an (upgradeable read) lock on the SETTINGS object to ensure that the SETTINGS content does not change. The lock also prevents from concurrent execution.

§Example with TemplateKind::SyncFilename
use tpnote_lib::content::ContentString;
use tpnote_lib::workflow::WorkflowBuilder;
use std::env::temp_dir;
use std::fs;
use std::path::Path;

// Prepare test: create existing note.
let raw = r#"

---
title: "My day"
subtitle: "Note"
---
Body text
"#;
let notefile = temp_dir().join("20221030-hello.md");
fs::write(&notefile, raw.as_bytes()).unwrap();

let expected = temp_dir().join("20221030-My day--Note.md");
let _ = fs::remove_file(&expected);

// Build and run workflow.
let n = WorkflowBuilder::new(&notefile)
     .build()
     // You can plug in your own type (must impl. `Content`).
     .run::<ContentString>()
     .unwrap();

// Check result
assert_eq!(n, expected);
assert!(n.is_file());
Source§

impl<T: Content, F: Fn(TemplateKind) -> TemplateKind> Workflow<SyncFilenameOrCreateNew<'_, T, F>>

Source

pub fn run(self) -> Result<PathBuf, NoteError>

Starts the “synchronize filename or create a new note” workflow. Returns the note’s new or existing filename. Repeated calls, will reload the environment variables, but not the configuration file. This function is stateless. Errors can occur in various ways, see NoteError.

Note: this method holds an (upgradeable read) lock on the SETTINGS object to ensure that the SETTINGS content does not change. The lock also prevents from concurrent execution.

§Example with TemplateKind::FromClipboard
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::workflow::WorkflowBuilder;
use std::env::temp_dir;
use std::path::PathBuf;
use std::fs;

// Prepare test.
let notedir = temp_dir();

let html_clipboard = ContentString::from_string(
    "my HTML clipboard\n".to_string(),
    "html_clipboard".to_string()
);
let txt_clipboard = ContentString::from_string(
    "my TXT clipboard\n".to_string(),
    "txt_clipboard".to_string()
);
let stdin = ContentString::from_string(
    "my stdin\n".to_string(),
    "stdin".to_string()
);
let v = vec![&html_clipboard, &txt_clipboard, &stdin];
// This is the condition to choose: `TemplateKind::FromClipboard`:
assert!(html_clipboard.header().is_empty()
           && txt_clipboard.header().is_empty()
           && stdin.header().is_empty());
assert!(!html_clipboard.body().is_empty() || !txt_clipboard.body().is_empty() || !stdin.body().is_empty());
let template_kind_filter = |tk|tk;

// Build and run workflow.
let n = WorkflowBuilder::new(&notedir)
      // You can plug in your own type (must impl. `Content`).
     .upgrade::<ContentString, _>(
           "default", v, template_kind_filter)
     .build()
     .run()
     .unwrap();

// Check result.
assert!(n.as_os_str().to_str().unwrap()
   .contains("my stdin--Note"));
assert!(n.is_file());
let raw_note = fs::read_to_string(n).unwrap();

#[cfg(not(target_family = "windows"))]
assert!(raw_note.starts_with(
           "\u{feff}---\ntitle:        my stdin"));
#[cfg(target_family = "windows")]
assert!(raw_note.starts_with(
           "\u{feff}---\r\ntitle:"));

Trait Implementations§

Source§

impl<W: Clone> Clone for Workflow<W>

Source§

fn clone(&self) -> Workflow<W>

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<W: Debug> Debug for Workflow<W>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<W> Freeze for Workflow<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for Workflow<W>
where W: RefUnwindSafe,

§

impl<W> Send for Workflow<W>
where W: Send,

§

impl<W> Sync for Workflow<W>
where W: Sync,

§

impl<W> Unpin for Workflow<W>
where W: Unpin,

§

impl<W> UnwindSafe for Workflow<W>
where W: UnwindSafe,

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<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,