Struct tpnote_lib::content::ContentString
source · #[repr(transparent)]pub struct ContentString { /* 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§
source§impl ContentString
impl ContentString
sourcepub fn new(
owner: String,
dependent_builder: impl for<'_q> FnOnce(&'_q String) -> ContentRef<'_q>
) -> Self
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)>
sourcepub fn borrow_owner<'_q>(&'_q self) -> &'_q String
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
sourcepub fn borrow_dependent<'_q>(&'_q self) -> &'_q ContentRef<'_q>
pub fn borrow_dependent<'_q>(&'_q self) -> &'_q ContentRef<'_q>
Examples found in repository?
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
fn header(&self) -> &str {
self.borrow_dependent().header
}
/// Cheap access to the note's body.
fn body(&self) -> &str {
self.borrow_dependent().body
}
}
/// Default is the empty string.
impl Default for ContentString {
fn default() -> Self {
Self::from(String::new())
}
}
/// Returns the whole raw content with header and body.
/// Possible `\r\n` in the input are replaced by `\n`.
impl AsRef<str> for ContentString {
fn as_ref(&self) -> &str {
self.borrow_owner()
}
}
/// Concatenates the header and the body and prints the content.
/// This function is expensive as it involves copying the
/// whole content.
impl<'a> fmt::Display for ContentRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = if self.header.is_empty() {
self.body.to_string()
} else {
format!("\u{feff}---\n{}\n---\n{}", &self.header, &self.body)
};
write!(f, "{}", s)
}
}
/// Delegates the printing to `Display for ContentRef`.
impl fmt::Display for ContentString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::fmt::Display::fmt(&self.borrow_dependent(), f)
}pub fn into_owner(self) -> String
Trait Implementations§
source§impl AsRef<str> for ContentString
impl AsRef<str> for ContentString
Returns the whole raw content with header and body.
Possible \r\n in the input are replaced by \n.
source§impl Content for ContentString
impl Content for ContentString
Add header() and body() implementation.
source§fn open(path: &Path) -> Result<Self, Error>where
Self: Sized,
fn open(path: &Path) -> Result<Self, Error>where
Self: Sized,
path and stores the content
Content. Possible \r\n are replaced by \n.
This trait has a default implementation, the empty content. Read moresource§fn from_string_with_cr(input: String) -> Self
fn from_string_with_cr(input: String) -> Self
\r\n are converted to \n if there are any.
If not, no memory allocation occurs and the buffer remains untouched. Read moresource§fn save_as(&self, new_file_path: &Path) -> Result<(), Error>
fn save_as(&self, new_file_path: &Path) -> Result<(), Error>
new_file_path as filename.
If new_file_path contains missing directories, they will be
created on the fly. Read moresource§impl Debug for ContentString
impl Debug for ContentString
source§impl Default for ContentString
impl Default for ContentString
Default is the empty string.
source§impl Display for ContentString
impl Display for ContentString
Delegates the printing to Display for ContentRef.
source§impl Drop for ContentString
impl Drop for ContentString
source§impl From<String> for ContentString
impl From<String> for ContentString
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 + ---.
Contract: the imput string does not contain \r\n. If
it may, use Content::from_string_with_cr() instead.
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
let input = "---\ntitle: \"My note\"\n---\nMy body";
let c = ContentString::from(input.to_string());
assert_eq!(c.header(), r#"title: "My note""#);
assert_eq!(c.body(), "My body");
// A test without front matter leads to an empty header:
let c = ContentString::from("No header".to_string());
assert_eq!(c.header(), "");
assert_eq!(c.body(), "No header");source§impl PartialEq<ContentString> for ContentString
impl PartialEq<ContentString> for ContentString
impl Eq for ContentString
Auto Trait Implementations§
impl RefUnwindSafe for ContentString
impl Send for ContentString
impl Sync for ContentString
impl Unpin for ContentString
impl UnwindSafe for ContentString
Blanket Implementations§
§impl<T> AsUncased for Twhere
T: AsRef<str> + ?Sized,
impl<T> AsUncased for Twhere
T: AsRef<str> + ?Sized,
§fn as_uncased(&self) -> &UncasedStr
fn as_uncased(&self) -> &UncasedStr
self to an [UncasedStr].source§impl<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,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.