Struct tpnote_lib::content::Content
source · [−]#[repr(transparent)]pub struct Content { /* private fields */ }Expand description
Holds the notes content in a string and two string slices
header and body.
This struct is self referencial.
It deals with operating system specific handling of newlines.
The content of a note is stored as UTF-8 string with
one \n character as newline. If present, a Byte Order Mark
BOM is removed while reading with new().
Implementations
sourceimpl Content
impl Content
pub fn new(
owner: String,
dependent_builder: impl for<'_q> FnOnce(&'_q String) -> ContentRef<'_q>
) -> Self
pub fn try_new<Err>(
owner: String,
dependent_builder: impl for<'_q> FnOnce(&'_q String) -> Result<ContentRef<'_q>, Err>
) -> Result<Self, Err>
pub fn try_new_or_recover<Err>(
owner: String,
dependent_builder: impl for<'_q> FnOnce(&'_q String) -> Result<ContentRef<'_q>, Err>
) -> Result<Self, (String, Err)>
pub fn borrow_owner<'_q>(&'_q self) -> &'_q String
pub fn with_dependent<'outer_fn, Ret>(
&'outer_fn self,
func: impl for<'_q> FnOnce(&'_q String, &'outer_fn ContentRef<'_q>) -> Ret
) -> Ret
pub fn with_dependent_mut<'outer_fn, Ret>(
&'outer_fn mut self,
func: impl for<'_q> FnOnce(&'_q String, &'outer_fn mut ContentRef<'_q>) -> Ret
) -> Ret
pub fn borrow_dependent<'_q>(&'_q self) -> &'_q ContentRef<'_q>
pub fn into_owner(self) -> String
sourceimpl<'a> Content
impl<'a> Content
The content of a note is stored as UTF-8 string with
one \n character as newline. If present, a Byte Order Mark
BOM is removed while reading with new().
sourcepub fn from(input: String) -> Self
pub fn from(input: String) -> Self
Constructor that parses a Tp-Note document.
A valid document is UTF-8 encoded and starts with an optional
BOM (byte order mark) followed by ---. When the start marker
--- does not follow directly the BOM, it must be prepended
by an empty line. In this case all text before is ignored:
BOM + ignored text + empty line + ---.
use tpnote_lib::content::Content;
let c = Content::from(String::from("---\ntitle: \"My note\"\n---\nMy body"));
assert_eq!(c.borrow_dependent().header, r#"title: "My note""#);
assert_eq!(c.borrow_dependent().body, r#"My body"#);
// A test without front matter leads to an empty header:
let c = Content::from(String::from("No header"));
assert_eq!(c.borrow_dependent().header, "");
assert_eq!(c.borrow_dependent().body, r#"No header"#);sourcepub fn from_input_with_cr(input: String) -> Self
pub fn from_input_with_cr(input: String) -> Self
Constructor that reads a structured document with a YAML header
and body. All \r\n are converted to \n if there are any.
If not, no memory allocation occurs and the buffer remains untouched.
use tpnote_lib::content::Content;
let c = Content::from_input_with_cr(String::from(
"---\r\ntitle: \"My note\"\r\n---\r\nMy\nbody\r\n"));
assert_eq!(c.borrow_dependent().header, r#"title: "My note""#);
assert_eq!(c.borrow_dependent().body, "My\nbody\n");
// A test without front matter leads to an empty header:
let c = Content::from(String::from("No header"));
assert_eq!(c.borrow_dependent().header, "");
assert_eq!(c.borrow_dependent().body, r#"No header"#);sourcepub fn write_to_disk(&self, new_file_path: &Path) -> Result<(), FileError>
pub fn write_to_disk(&self, new_file_path: &Path) -> Result<(), FileError>
Writes the note to disk with new_file_path as filename.
If new_file_path contains missing directories, they will be
created on the fly.
use std::path::Path;
use std::env::temp_dir;
use std::fs;
use tpnote_lib::content::Content;
let c = Content::from(
String::from("prelude\n\n---\ntitle: \"My note\"\n---\nMy body"));
let outfile = temp_dir().join("mynote.md");
#[cfg(not(target_family = "windows"))]
let expected = "\u{feff}---\ntitle: \"My note\"\n---\nMy body\n";
#[cfg(target_family = "windows")]
let expected = "\u{feff}---\r\ntitle: \"My note\"\r\n---\r\nMy body\r\n";
c.write_to_disk(&outfile).unwrap();
let result = fs::read_to_string(&outfile).unwrap();
assert_eq!(result, expected);
fs::remove_file(&outfile);Trait Implementations
sourceimpl TryFrom<&Content> for FrontMatter
impl TryFrom<&Content> for FrontMatter
sourcefn try_from(content: &Content) -> Result<FrontMatter, NoteError>
fn try_from(content: &Content) -> Result<FrontMatter, NoteError>
Helper function deserialising the front-matter of the note file.
use tpnote_lib::content::Content;
use tpnote_lib::front_matter::FrontMatter;
use serde_json::json;
// Create existing note.
let raw = "\u{feff}---\ntitle: \"My day\"\nsubtitle: \"Note\"\n---\nBody";
let content = Content::from(raw.to_string());
assert!(!content.is_empty());
assert!(!content.borrow_dependent().header.is_empty());
let front_matter = FrontMatter::try_from(&content).unwrap();
assert_eq!(front_matter.get("title"), Some(&json!("My day")));
assert_eq!(front_matter.get("subtitle"), Some(&json!("Note")));impl Eq for Content
Auto Trait Implementations
impl RefUnwindSafe for Content
impl Send for Content
impl Sync for Content
impl Unpin for Content
impl UnwindSafe for Content
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.