Trait tpnote_lib::filename::NotePathBuf
source · pub trait NotePathBuf {
// Required methods
fn from_disassembled(
sort_tag: &str,
stem: &str,
copy_counter: Option<usize>,
extension: &str
) -> Self;
fn set_next_unused(&mut self) -> Result<(), FileError>;
fn shorten_filename(&mut self);
}
Expand description
Extents PathBuf
with methods dealing with paths to Tp-Note files.
Required Methods§
sourcefn from_disassembled(
sort_tag: &str,
stem: &str,
copy_counter: Option<usize>,
extension: &str
) -> Self
fn from_disassembled( sort_tag: &str, stem: &str, copy_counter: Option<usize>, extension: &str ) -> Self
Concatenates the sort_tag
, stem
, copy_counter
, .
and
extension
.
This functions inserts all potentially necessary separators and
extra separators.
sourcefn set_next_unused(&mut self) -> Result<(), FileError>
fn set_next_unused(&mut self) -> Result<(), FileError>
Append/increment a copy counter.
When the path p
exists on disk already, append some extension
with an incrementing counter to the sort-tag in p
until
we find a free unused filename.
use std::env::temp_dir;
use std::fs;
use tpnote_lib::filename::NotePathBuf;
// Prepare test: create existing note file.
let raw = "some content";
let mut notefile = temp_dir().join("20221101-My day--Note.md");
fs::write(¬efile, raw.as_bytes()).unwrap();
let expected = temp_dir().join("20221101-My day--Note(1).md");
let _ = fs::remove_file(&expected);
// Start test
notefile.set_next_unused();
assert_eq!(notefile, expected);
When the filename is not used, keep it.
use std::env::temp_dir;
use std::fs;
use tpnote_lib::filename::NotePathBuf;
// Prepare test: make sure that there is no note file.
let mut notefile = temp_dir().join("20221101-My day--Note.md");
let _ = fs::remove_file(¬efile);
// The name should not change.
let expected = notefile.clone();
// Start test
notefile.set_next_unused();
assert_eq!(notefile, expected);
sourcefn shorten_filename(&mut self)
fn shorten_filename(&mut self)
Shortens the stem of a filename so that
filename.len() <= FILENAME_LEN_MAX
.
If stem ends with a pattern similar to a copy counter,
append -
to stem (cf. unit test in the source code).
use std::ffi::OsString;
use std::path::PathBuf;
use tpnote_lib::filename::NotePathBuf;
use tpnote_lib::config::FILENAME_LEN_MAX;
// Test short filename.
let mut input = PathBuf::from("short filename.md");
input.shorten_filename();
let output = input;
assert_eq!(OsString::from("short filename.md"),
output.into_os_string());
// Test too long filename.
let mut input = String::from("some/path/");
for _ in 0..(FILENAME_LEN_MAX - "long fil.ext".len()) {
input.push('x');
}
let mut expected = input.clone();
input.push_str("long filename to be cut.ext");
let mut input = PathBuf::from(input);
expected.push_str("long fil.ext");
input.shorten_filename();
let output = PathBuf::from(input);
assert_eq!(PathBuf::from(expected), output);