#[repr(transparent)]
pub struct Content { /* private fields */ }
Expand description

This is a newtype and thin wrapper around the note’s content. It deals with operating system specific handling of newlines.

Implementations

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().

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"#);

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"#);

True if the underlying owned Content string is empty.

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

Formats the value using the given formatter. Read more

Delegates the printing to Display for ContentRef.

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Helper function deserialising the front-matter of the note file.

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

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

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.