pub struct Workflow<W> { /* private fields */ }
Expand description
Holds the input data for the run()
method.
Implementations§
Source§impl Workflow<SyncFilename<'_>>
impl Workflow<SyncFilename<'_>>
Sourcepub fn run<T: Content>(self) -> Result<PathBuf, NoteError>
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(¬efile, 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(¬efile)
.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>>
impl<T: Content, F: Fn(TemplateKind) -> TemplateKind> Workflow<SyncFilenameOrCreateNew<'_, T, F>>
Sourcepub fn run(self) -> Result<PathBuf, NoteError>
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(¬edir)
// 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§
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> 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