-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
455 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/metadata/snapshots/rust_norg__metadata__tests__arrays.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
45 changes: 45 additions & 0 deletions
45
src/metadata/snapshots/rust_norg__metadata__tests__common_metadata.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/metadata/snapshots/rust_norg__metadata__tests__keys.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
41 changes: 41 additions & 0 deletions
41
src/metadata/snapshots/rust_norg__metadata__tests__keys_and_values.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
Oops, something went wrong.