Skip to content

Commit

Permalink
feat: MVP reached
Browse files Browse the repository at this point in the history
-> Add files to index
-> List files
-> Export files
  • Loading branch information
LudeeD committed Oct 29, 2020
1 parent 18d6a92 commit 62da975
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 188 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ pulldown-cmark = "0.7.2"
pulldown-cmark-to-cmark = "5.0.0"
regex = "1"
chrono = "0.4"
ring = "0.16.15"
ring = "0.16.15"
data-encoding = "2.3.0"
35 changes: 24 additions & 11 deletions src/exporter/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::fs;
use std::io::Write;
use std::fs::File;

use walkdir::WalkDir;
//
//use std::path::{ PathBuf};
//
Expand Down Expand Up @@ -75,25 +76,37 @@ pub fn export(file_path: Option<PathBuf>) -> Result<()> {
let mut handlebars = Handlebars::new();

handlebars.register_template_string("t1", templates::skeleton);
//match file_path {
// Some(f) => export_single_file(f),
// None => export_all_folder()
//}

let mut a = PathBuf::from("C:\\Users\\Luís Silva\\Desktop\\NOTA\\1.md");
match file_path {
Some(f) => export_single_file(f, &handlebars),
None => export_all_folder(&handlebars)
}

export_single_file(a, handlebars)
// let mut a = PathBuf::from("C:\\Users\\Luís Silva\\Desktop\\NOTA\\1.md");

// export_single_file(a, handlebars)
}

fn export_all_folder() -> Result<()> {
fn export_all_folder( handlebars: & Handlebars ) -> Result<()> {
debug!("exporting all folder");

Ok(())
let nota_path = util::envs::main_folder();

for entry in WalkDir::new(nota_path).follow_links(false).into_iter().filter_map(|e| e.ok()) {
let fname = String::from(entry.file_name().to_string_lossy());

if !(fname.ends_with(".md")) {
continue;
}

export_single_file(entry.into_path(), handlebars);
}

Ok(())
}

fn export_single_file(mut file_path: PathBuf, handlebars: Handlebars) -> Result<()> {
debug!("exporting single file");
fn export_single_file(mut file_path: PathBuf, handlebars: & Handlebars) -> Result<()> {
debug!("exporting file {:?}", file_path);

let mut data = BTreeMap::new();

Expand All @@ -118,7 +131,7 @@ fn export_single_file(mut file_path: PathBuf, handlebars: Handlebars) -> Result<

let mut output_file = File::create(out_file).unwrap();

output_file.write_all(handlebars.render("t1", &data).unwrap().as_bytes());
output_file.write_all(handlebars.render("t1", &data).unwrap().as_bytes()).expect("TODO remove expects");

Ok(())
}
127 changes: 84 additions & 43 deletions src/index/list.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,122 @@
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
use bincode;
use std::path::PathBuf;
use anyhow::Result;

use std::convert::TryInto;

use crate::util::{envs, filesystem};

pub fn init() -> Result<()> {
let clean_index = NotaList{entries: vec![]};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IndexEntry {
pub uid: u64,
pub original_title: Option<String>,
pub file_path: PathBuf,
pub contents_digest: String,
pub replaced_by: Option<u64>
}

clean_index.save();
pub fn init() -> Result<Vec<IndexEntry>> {
debug!("Initializing Index");

Ok(())
}
let clean_index = vec![];

save(&clean_index);

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct ListEntry {
uid: u64,
title: String,
file_path: String,
contents_digest: String
Ok(clean_index)
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct NotaList {
entries: Vec<ListEntry>,
pub fn load() -> Result<Vec<IndexEntry>> {

debug!("Loading Index to memory");

let index_path = envs::index_path();

let bytes = filesystem::read_bytes(&index_path)?;

match bincode::deserialize(&bytes) {
Ok(index) => Ok(index),
Err(_error) => Err(anyhow!("Error occurred while deserializing")),
}
}

impl NotaList {
pub fn save(index_to_save: &Vec<IndexEntry>) -> Result<()> {
debug!("Saving Index");

pub fn new() -> Result<NotaList> {
let index_path = envs::index_path();

let list_path = envs::list_path();
let encoded : Vec<u8> = match bincode::serialize(index_to_save) {
Ok(bytes) => bytes,
Err(_error) => return Err(anyhow!("Error occurred generating bincode"))
};

debug!("list path: {}", list_path);
match filesystem::write_bytes(&index_path, &encoded) {
Ok(()) => Ok(()),
Err(_error) => Err(anyhow!("Error Occurred saving bincode"))
}
}

let bytes = filesystem::read_bytes(&list_path)?;
pub fn list(index_to_list: &Vec<IndexEntry>) -> Result<()> {

match bincode::deserialize(&bytes) {
Ok(index) => Ok(index),
Err(_error) => Err(anyhow!("Error occurred while deserializing")),
}
println!("LIST INDEX: ");

for entry in index_to_list {
println!("=> {:?}", entry);
}

pub fn save(&self) -> Result<()> {
Ok(())
}

pub fn add_new_nota(index: &mut Vec<IndexEntry>, mut entry: IndexEntry) -> Result<()> {

let list_path = envs::list_path();

debug!("list path: {}", list_path);
let new_uid = index.len().try_into().unwrap();

let encoded : Vec<u8> = match bincode::serialize(self) {
Ok(bytes) => bytes,
Err(_error) => return Err(anyhow!("Error occurred generating bincode"))
};
entry.uid = new_uid;

index.push(entry);

Ok(())
}

pub fn search_for_uid(index: & Vec<IndexEntry>, uid_to_search: u64) -> Result<IndexEntry> {

let mut entry : Option<IndexEntry> = None;

for elem in index.iter() {

if elem.uid == uid_to_search {

entry = Some(elem.clone());

match filesystem::write_bytes(&list_path, &encoded) {
Ok(()) => Ok(()),
Err(_error) => Err(anyhow!("Error Occurred saving bincode"))
}

}

pub fn get_next_uid(&mut self) -> Result<u64> {
let number_of_entries : u64 = (self.entries.len() + 1).try_into()?;
Ok(number_of_entries)
match entry {
Some(e) => Ok(e),
None => Err(anyhow!("No index entry found"))
}

pub fn add_new_nota(&mut self, title : Option<String>, file_path: String, contents_digest: String) -> Result<()> {
}

pub fn search_for_path(index: & Vec<IndexEntry>, path_to_search: PathBuf) -> Result<IndexEntry> {

let uid = self.get_next_uid()?;
let mut entry : Option<IndexEntry> = None;

debug!("New entry {:?} {:?} {:?} {:?}", uid, title, file_path, contents_digest);
for elem in index.iter() {

let title = title.expect("TODO remove expect");
if elem.file_path == path_to_search {

let entry = ListEntry{uid, title, file_path, contents_digest};
entry = Some(elem.clone());

Ok(())
}

}

match entry {
Some(e) => Ok(e),
None => Err(anyhow!("No index entry found"))
}

}
44 changes: 29 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ mod exporter;
//mod error;

//use error::Upsie;
use std::{ fs::File, io::Read, io::Write };
use std::path::{PathBuf};

//use database::IndexEntry;
//use chrono::prelude::*;

//pub const REVERSE_LINKS_HEADING_LEVEL : &str = "###";
//pub const REVERSE_LINKS_TEXT : &str = "Reverse Links";
use std::fs;

pub fn init_envs() {
util::envs::init();
Expand Down Expand Up @@ -97,17 +91,35 @@ pub fn command_new(new_nota_name: Option<&str>) {
}

/// move file to the NOTA location
pub fn command_add(mut in_file: File) {
pub fn command_add(in_file: PathBuf) {

let file_name = in_file.file_name().unwrap();

let nota_folder = util::envs::main_folder();

let mut new_file = PathBuf::from(nota_folder);

new_file.push(file_name);

//let info = parser::parse(in_file);
fs::copy(&in_file, &new_file).expect("TODO remove expects");

//let info = info.as_ref();
let info = parser::parse(&in_file).unwrap();

//debug!("Title: {}", info.title());
let info = info.as_ref();

//info.refs().into_iter().for_each(|uid| {
// debug!("Links to: {}", uid);
//});
let mut index = index::list::load().expect("TODO remove expects");

let index_entry = index::list::IndexEntry{
uid: 0,
original_title: Some(String::from(&info.title)),
file_path: new_file,
contents_digest: String::from(&info.contents_digest),
replaced_by: None
};

index::list::add_new_nota(&mut index, index_entry).expect("TODO remove expects");

index::list::save(&index);

}

Expand All @@ -116,7 +128,9 @@ pub fn command_update() {
}

pub fn command_list() {
panic!("Not Implemented")
let index = index::list::load().expect("TODO remove expect");

index::list::list(&index);
}

pub fn command_export() {
Expand Down
5 changes: 5 additions & 0 deletions src/links/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ pub fn init() -> Result<()> {

util::filesystem::create_folder(&links_path);

Ok(())
}

pub fn add() -> Result<()> {

Ok(())
}
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ extern crate log;

extern crate simple_logger;

use std::{ fs::File, io::Read, io::Write };

use std::fs::OpenOptions;
use std::path::{PathBuf};

fn main() {
let yaml = load_yaml!("cli.yml");
Expand Down Expand Up @@ -40,7 +39,7 @@ fn main() {

if let Some(matches_add) = matches.subcommand_matches("add") {
let file = matches_add.value_of("PATH").unwrap();
let mut file = OpenOptions::new().read(true).write(true).create(true).open(file).expect("Hum...");
let file = PathBuf::from(file);
nota::command_add(file);
}

Expand Down
Loading

0 comments on commit 62da975

Please sign in to comment.