pub struct KeynoteFile {
pub filepath: PathBuf,
/* private fields */
}
Expand description
A data structure to represent the keynotes data file
Fields§
§filepath: PathBuf
path to the file as a PathBuf
Implementations§
Source§impl KeynoteFile
impl KeynoteFile
Sourcepub fn load_data(&mut self) -> Result<(), Box<dyn Error>>
pub fn load_data(&mut self) -> Result<(), Box<dyn Error>>
Loads data from file into KeynoteFile structure
§Examples
use std::fs;
use keydata::*;
let mut file = KeynoteFile::new("kntest.dat").unwrap();
file.load_data();
fs::remove_file(file.filepath); // remove the test file
Sourcepub fn add_entry<'a>(
&mut self,
section_to_add_to: &str,
key: &str,
value: &str,
) -> Result<(), Box<dyn Error>>
pub fn add_entry<'a>( &mut self, section_to_add_to: &str, key: &str, value: &str, ) -> Result<(), Box<dyn Error>>
Add a key-value entry into the file
§Arguments
section_to_add_to
- section to add entry to as string slicekey
- key for the entry as string slicevalue
- value of the entry as string slice
§Examples ///
use std::fs;
use keydata::*;
let mut kn_file = KeynoteFile::new("kntest.dat").unwrap();
kn_file.add_section("leaders").unwrap();
kn_file.add_entry("leaders", "atreides", "leto");
fs::remove_file(kn_file.filepath); // remove the test file
Sourcepub fn remove_entry(&mut self, key: &str) -> Result<(), Box<dyn Error>>
pub fn remove_entry(&mut self, key: &str) -> Result<(), Box<dyn Error>>
Remove a key-value entry from the file
§Arguments
key
- key for the entry to remove as string slice
§Examples ///
use std::fs;
use keydata::*;
let mut kn_file = KeynoteFile::new("kntest.dat").unwrap();
kn_file.add_section("leaders").unwrap();
kn_file.add_entry("leaders", "atreides", "leto");
kn_file.remove_entry("atreides");
fs::remove_file(kn_file.filepath); // remove the test file
Sourcepub fn remove_section(
&mut self,
section_to_remove: &str,
) -> Result<(), Box<dyn Error>>
pub fn remove_section( &mut self, section_to_remove: &str, ) -> Result<(), Box<dyn Error>>
Remove a section from the file
§Arguments
section_to_remove
- section to remove as string slice
§Examples ///
use std::fs;
use keydata::*;
let mut kn_file = KeynoteFile::new("kntest.dat").unwrap();
kn_file.add_section("leaders").unwrap();
kn_file.remove_section("leaders");
fs::remove_file(kn_file.filepath); // remove the test file
Sourcepub fn get_sections(&self) -> &HashMap<String, Section>
pub fn get_sections(&self) -> &HashMap<String, Section>
Returns a reference to this files sections hashmap
§Examples ///
use std::fs;
use keydata::*;
let mut kn_file = KeynoteFile::new("kntest.dat").unwrap();
kn_file.add_section("leaders").unwrap();
let sections = kn_file.get_sections();
fs::remove_file(kn_file.filepath); // remove the test file
Sourcepub fn add_section(&mut self, section_name: &str) -> Result<(), Box<dyn Error>>
pub fn add_section(&mut self, section_name: &str) -> Result<(), Box<dyn Error>>
Adds a new section to the file
§Arguments
section_name
- name of the section to add
§Examples
use std::fs;
use keydata::*;
let mut kn_file = KeynoteFile::new("kntest.dat").unwrap();
kn_file.add_section("leaders").unwrap();
kn_file.add_section("villains").unwrap();
fs::remove_file(kn_file.filepath); // remove the test file
Sourcepub fn get_value_from_key(&mut self, key: &str) -> Option<&str>
pub fn get_value_from_key(&mut self, key: &str) -> Option<&str>
Gets the value of an entry in the file from a key
§Arguments
key
- key to search the file for
§Examples
use std::fs;
use keydata::*;
let mut kn_file = KeynoteFile::new("kntest.dat").unwrap();
kn_file.add_section("leaders").unwrap();
kn_file.add_entry("leaders", "atreides", "leto");
let value = kn_file.get_value_from_key("atreides");
println!("{}", value.unwrap()); // "leto"
fs::remove_file(kn_file.filepath); // remove the test file
Sourcepub fn contains_key(&mut self, key: &str) -> bool
pub fn contains_key(&mut self, key: &str) -> bool
Checks if a key is present in the file
§Arguments
key
- key to search the file for
§Examples
use std::fs;
use keydata::*;
let mut kn_file = KeynoteFile::new("kntest.dat").unwrap();
kn_file.add_section("leaders").unwrap();
kn_file.add_entry("leaders", "atreides", "leto");
println!("{}", kn_file.contains_key("atreides"));
fs::remove_file(kn_file.filepath); // remove the test file
Sourcepub fn get_section(&mut self, section_name: &str) -> Option<&mut Section>
pub fn get_section(&mut self, section_name: &str) -> Option<&mut Section>
Returns a Section from the file based on section name
§Arguments
section_name
- name of section to return if it exists
§Examples
use std::fs;
use keydata::*;
let mut kn_file = KeynoteFile::new("kntest.dat").unwrap();
kn_file.add_section("leaders").unwrap();
println!("{}", kn_file.get_section("leaders").unwrap().name);
fs::remove_file(kn_file.filepath); // remove the test file
Auto Trait Implementations§
impl Freeze for KeynoteFile
impl RefUnwindSafe for KeynoteFile
impl Send for KeynoteFile
impl Sync for KeynoteFile
impl Unpin for KeynoteFile
impl UnwindSafe for KeynoteFile
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
Mutably borrows from an owned value. Read more