Skip to content

Commit

Permalink
feat: metadata parser (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored Dec 1, 2024
1 parent 5804104 commit 64ae1e1
Show file tree
Hide file tree
Showing 9 changed files with 455 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum NorgParseError {
Stage2(Vec<Simple<NorgToken>>),
Stage3(Vec<Simple<NorgBlock>>),
Stage4(Vec<Simple<NorgASTFlat>>),
Meta(Simple<char>),
}

impl From<Vec<Simple<char>>> for NorgParseError {
Expand All @@ -34,3 +35,9 @@ impl From<Vec<Simple<NorgASTFlat>>> for NorgParseError {
NorgParseError::Stage4(error)
}
}

impl From<Simple<char>> for NorgParseError {
fn from(error: Simple<char>) -> Self {
NorgParseError::Meta(error)
}
}
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use crate::stage_3::*;
pub use crate::stage_4::NorgAST;

mod error;
pub mod metadata;
mod stage_1;
mod stage_2;
mod stage_3;
Expand Down Expand Up @@ -101,11 +102,11 @@ mod tests {
* Back to regular heading
",
]
.into_iter()
.map(|example| example.to_string() + "\n")
.map(|str| parse_tree(&str))
.try_collect()
.unwrap();
.into_iter()
.map(|example| example.to_string() + "\n")
.map(|str| parse_tree(&str))
.try_collect()
.unwrap();
assert_yaml_snapshot!(headings_tree_examples);
}

Expand Down
135 changes: 135 additions & 0 deletions src/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use chumsky::Parser;
pub use stage_1::NorgMeta;

use crate::error::NorgParseError;

pub mod stage_1;

/// Parses the given input string to produce an AST for the metadata
pub fn parse_metadata(input: &str) -> Result<NorgMeta, NorgParseError> {
let processed = format!("{{\n{}\n}}\n", input.trim());
Ok(stage_1::meta_parser().parse(processed)?)
}

#[cfg(test)]
mod tests {
use insta::assert_yaml_snapshot;
use itertools::Itertools;

use crate::metadata::parse_metadata;

#[test]
fn common_metadata() {
let examples: Vec<_> = [
"
title: Sunday November 17, 2024
description: We Cooked
authors: benlubas
categories: journal
created: 2024-11-18
updated: 2024-11-18T17:58:21-0500
version: 1.1.1
",
"
title: Neorg Extras
description: Extra lua code to configure Neorg
authors: benlubas
categories: [
neorg
nvim
config
]
tangle: {
languages: {
lua: ~/github/.dotfiles/nvim/lua/benlubas/neorg/extras.lua
}
delimiter: heading
}
created: 2024-05-03T13:36:42-0500
updated: 2024-10-27T11:12:32-0500
version: 1.1.1
",
]
.into_iter()
.map(|example| example.to_string() + "\n")
.map(|str| parse_metadata(&str))
.try_collect()
.unwrap();

assert_yaml_snapshot!(examples);
}

#[test]
fn arrays() {
let examples: Vec<_> = [
"empty_arr: []
arr: [
]",
"
categories: [
one
two
45
]",
"
arr: [
arrays can contain everything
5
-5
6.02e27
nil
{
x: y
a: [
b
]
}
[]
[
hi
hi
]
]",
"arr:[]\na2:[\n]x: y",
]
.into_iter()
.map(|example| example.to_string() + "\n")
.map(|str| parse_metadata(&str))
.try_collect()
.unwrap();

assert_yaml_snapshot!(examples);
}

#[test]
fn keys_and_values() {
let examples: Vec<_> = [
"key: value",
"x:y",
"x :y",
"x:5",
"x:-4",
"str:-4b",
"nil:nil",
"nil:",
"still_nil:
x: y",
"
key: value with : in it
key_2: value with: in it
",
"keys: {
in:
objects: []
}"
]
.into_iter()
.map(|example| example.to_string() + "\n")
.map(|str| parse_metadata(&str))
.try_collect()
.unwrap();

assert_yaml_snapshot!(examples);
}
}
40 changes: 40 additions & 0 deletions src/metadata/snapshots/rust_norg__metadata__tests__arrays.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: src/metadata/mod.rs
expression: examples
---
- Object:
arr:
Array: []
empty_arr:
Array: []
- Object:
categories:
Array:
- Str: one
- Str: two
- Num: 45
- Object:
arr:
Array:
- Str: arrays can contain everything
- Num: 5
- Num: -5
- Num: 6020000000000000000000000000
- Nil
- Object:
a:
Array:
- Str: b
x:
Str: y
- Array: []
- Array:
- Str: hi
- Str: hi
- Object:
a2:
Array: []
arr:
Array: []
x:
Str: y
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: src/metadata/mod.rs
expression: examples
---
- Object:
authors:
Str: benlubas
categories:
Str: journal
created:
Str: 2024-11-18
description:
Str: We Cooked
title:
Str: "Sunday November 17, 2024"
updated:
Str: "2024-11-18T17:58:21-0500"
version:
Str: 1.1.1
- Object:
authors:
Str: benlubas
categories:
Array:
- Str: neorg
- Str: nvim
- Str: config
created:
Str: "2024-05-03T13:36:42-0500"
description:
Str: Extra lua code to configure Neorg
tangle:
Object:
delimiter:
Str: heading
languages:
Object:
lua:
Str: ~/github/.dotfiles/nvim/lua/benlubas/neorg/extras.lua
title:
Str: Neorg Extras
updated:
Str: "2024-10-27T11:12:32-0500"
version:
Str: 1.1.1
38 changes: 38 additions & 0 deletions src/metadata/snapshots/rust_norg__metadata__tests__keys.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
source: src/metadata/mod.rs
expression: examples
---
- Object:
key:
Str: value
- Object:
x:
Str: y
- Object:
x:
Num: 5
- Object:
x:
Num: -4
- Object:
str:
Str: "-4b"
- Object:
nil: Nil
- Object:
nil: Nil
- Object:
still_nil: Nil
x:
Str: y
- Object:
key:
Str: "value with : in it"
key_2:
Str: "value with: in it"
- Object:
keys:
Object:
in: Nil
objects:
Array: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: src/metadata/mod.rs
expression: examples
---
- Object:
key:
Str: value
- Object:
x:
Str: y
- Object:
x:
Str: y
- Object:
x:
Num: 5
- Object:
x:
Num: -4
- Object:
str:
Str: "-4b"
- Object:
nil: Nil
- Object:
nil: Nil
- Object:
still_nil: Nil
x:
Str: y
- Object:
key:
Str: "value with : in it"
key_2:
Str: "value with: in it"
- Object:
keys:
Object:
in: Nil
objects:
Array: []
Loading

0 comments on commit 64ae1e1

Please sign in to comment.