Compare commits

...

2 Commits

Author SHA1 Message Date
Ryan Rix 47307192a7 lift drawer parsing in Title to public interface in Drawer 2023-08-31 21:40:16 -07:00
Ryan Rix 92d69bf7a0 add dev deps 2023-08-31 21:36:17 -07:00
3 changed files with 41 additions and 24 deletions

10
shell.nix Normal file
View File

@ -0,0 +1,10 @@
# [[id:13995d53-320a-4955-996a-4a4a25319701][No heading:2]]
let
pkgs = import <nixpkgs> {};
in pkgs.mkShell {
packages = with pkgs; [ cargo rustc rust-analyzer rustfmt clippy ];
nativeBuildInputs = with pkgs; [ rustc cargo gcc ];
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
}
# No heading:2 ends here

View File

@ -1,13 +1,17 @@
use std::borrow::Cow;
use nom::{
bytes::complete::{tag, take_while1},
bytes::complete::{tag, take_until, take_while1},
character::complete::space0,
combinator::map,
multi::fold_many0,
sequence::delimited,
IResult,
};
use crate::parse::combinators::{blank_lines_count, eol, lines_till};
use crate::parse::combinators::{blank_lines_count, eol, line, lines_till};
use crate::elements::title::PropertiesMap;
/// Drawer Element
#[derive(Debug, Default, Clone)]
@ -29,6 +33,17 @@ impl Drawer<'_> {
parse_drawer(input).ok()
}
pub fn parse_drawer_content(content: &str) -> IResult<&str, PropertiesMap<'_>, ()> {
fold_many0(
parse_node_property,
PropertiesMap::new,
|mut acc: PropertiesMap, (name, value)| {
acc.pairs.push((name.into(), value.into()));
acc
},
)(content)
}
pub fn into_owned(self) -> Drawer<'static> {
Drawer {
name: self.name.into_owned().into(),
@ -74,6 +89,17 @@ pub fn parse_drawer_without_blank(input: &str) -> IResult<&str, (Drawer, &str),
))
}
#[inline]
fn parse_node_property(input: &str) -> IResult<&str, (&str, &str), ()> {
let (input, _) = blank_lines_count(input)?;
let input = input.trim_start();
let (input, name) = map(delimited(tag(":"), take_until(":"), tag(":")), |s: &str| {
s.trim_end_matches('+')
})(input)?;
let (input, value) = line(input)?;
Ok((input, (name, value.trim())))
}
#[test]
fn parse() {
assert_eq!(

View File

@ -6,18 +6,17 @@ use std::{borrow::Cow, iter::FromIterator};
use memchr::memrchr2;
use nom::{
branch::alt,
bytes::complete::{tag, take_until, take_while},
bytes::complete::{tag, take_while},
character::complete::{anychar, line_ending, space1},
combinator::{map, opt, verify},
error::{make_error, ErrorKind},
multi::fold_many0,
sequence::{delimited, preceded},
Err, IResult,
};
use crate::{
config::ParseConfig,
elements::{drawer::parse_drawer_without_blank, Planning, Timestamp},
elements::{drawer::parse_drawer_without_blank, Drawer, Planning, Timestamp},
parse::combinators::{blank_lines_count, line, one_word},
};
@ -262,28 +261,10 @@ fn parse_properties_drawer(input: &str) -> IResult<&str, PropertiesMap<'_>, ()>
if drawer.name != "PROPERTIES" {
return Err(Err::Error(make_error(input, ErrorKind::Tag)));
}
let (_, map) = fold_many0(
parse_node_property,
PropertiesMap::new,
|mut acc: PropertiesMap, (name, value)| {
acc.pairs.push((name.into(), value.into()));
acc
},
)(content)?;
let (_, map) = Drawer::parse_drawer_content(content)?;
Ok((input, map))
}
#[inline]
fn parse_node_property(input: &str) -> IResult<&str, (&str, &str), ()> {
let (input, _) = blank_lines_count(input)?;
let input = input.trim_start();
let (input, name) = map(delimited(tag(":"), take_until(":"), tag(":")), |s: &str| {
s.trim_end_matches('+')
})(input)?;
let (input, value) = line(input)?;
Ok((input, (name, value.trim())))
}
#[test]
fn parse_title_() {
use crate::config::DEFAULT_CONFIG;