Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Mar 11, 2023
1 parent 5064211 commit e5644ef
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "neorg-breeze"
description = "A library for parsing entire directories of Norg files in a multithreaded fashion."
repository = "https://github.com/nvim-neorg/neorg-breeze"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.69"
crossbeam-channel = "0.5.7"
neorg-dirman = "0.1.0"
rusty_pool = "0.7.0"
tree-sitter = "0.20.9"
tree-sitter-norg = "0.1.1"
Empty file added src/c_functions.rs
Empty file.
74 changes: 74 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use tree_sitter::Tree;
use rusty_pool::Builder;
use std::io::Read;
use std::fs::File;
use anyhow::{Result, anyhow};

pub mod c_functions;

fn parse_file(filepath: &std::path::PathBuf) -> Result<Tree> {
let mut file = File::open(filepath)?;

let mut content = String::new();
file.read_to_string(&mut content)?;

let mut parser = tree_sitter::Parser::new();
parser.set_language(tree_sitter_norg::language())?;

parser.parse(content, None).ok_or_else(|| anyhow!(format!("Parsing for file {} timed out!", filepath.display())))
}

// TODO: Change Vec<Option<Tree>> to Result<Vec<Tree>, (Err, output.filter(|x| x.is_some()))>
//
pub fn parse_files(workspace: neorg_dirman::workspace::Workspace) -> Vec<Option<Tree>> {
let files = workspace.files();

let threadpool = Builder::new().name("neorg".into()).build();

let mut output: Vec<Option<Tree>> = vec![None; files.len()];
let file_count = files.len();

let (tx, rx) = crossbeam_channel::bounded(file_count);

for (i, file) in files.into_iter().enumerate() {
let tx_clone = tx.clone();

threadpool.execute(move || {
let parsed = parse_file(&file).unwrap();
tx_clone.send((i, parsed)).unwrap();
});
}

for _ in 0..file_count {
let (i, tree) = rx.recv().unwrap();
output[i] = Some(tree);
}

output
}

#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
use neorg_dirman::workspace::Workspace;

#[test]
fn test_parse_file() {
let filepath = PathBuf::from("test/example_workspace/file1.norg");
let tree = parse_file(&filepath).unwrap();

assert!(tree.root_node().kind() == "document");
}

#[test]
fn test_parse_files() {
let workspace = Workspace {
name: "example workspace".into(),
path: "test/example_workspace".into(),
};
let trees = parse_files(workspace);

assert!(trees[0].is_some());
}
}
1 change: 1 addition & 0 deletions test/example_workspace/.hidden-file.norg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* This is hidden hehe
50 changes: 50 additions & 0 deletions test/example_workspace/file1.norg
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
* File 1!
50 changes: 50 additions & 0 deletions test/example_workspace/file2.norg
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
* File 2!
50 changes: 50 additions & 0 deletions test/example_workspace/file3.norg
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
* File 3!
50 changes: 50 additions & 0 deletions test/example_workspace/file4.norg
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
* File 4!
50 changes: 50 additions & 0 deletions test/example_workspace/file5.norg
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
* File 5!
Loading

0 comments on commit e5644ef

Please sign in to comment.