keydata

Struct KeynoteFile

Source
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

Source

pub fn new<'a>(filename: &str) -> Result<KeynoteFile, &'a str>

Creates a new KeynoteFile

§Arguments
  • filename - name of file to create in keynotes folder
§Examples ///
use keydata::*;
let kn_file = KeynoteFile::new("kntest.dat").unwrap();
 
assert!(kn_file.filepath.ends_with("kntest.dat"));
  
Source

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
Source

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 slice
  • key - key for the entry as string slice
  • value - 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
Source

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  
Source

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  
Source

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  
Source

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 
Source

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     
Source

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     
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.