feat: provide a static ParseConfig

This commit is contained in:
PoiScript 2019-09-14 13:50:25 +08:00
parent 54163520d6
commit 098433b5db
10 changed files with 37 additions and 35 deletions

View file

@ -3,9 +3,9 @@
/// Parse configuration
#[derive(Clone, Debug)]
pub struct ParseConfig {
/// Headline's TODO keywords, todo type
/// Headline's todo keywords, todo type
pub todo_keywords: Vec<String>,
/// Headline's TODO keywords, done type
/// Headline's todo keywords, done type
pub done_keywords: Vec<String>,
}
@ -17,3 +17,7 @@ impl Default for ParseConfig {
}
}
}
lazy_static::lazy_static! {
pub static ref DEFAULT_CONFIG: ParseConfig = ParseConfig::default();
}

View file

@ -1,13 +1,13 @@
use std::borrow::Cow;
use crate::parsers::{eol, line, take_lines_while};
use nom::{
bytes::complete::{tag, take_while1},
sequence::delimited,
IResult,
};
use crate::parsers::{eol, line, take_lines_while};
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "ser", derive(serde::Serialize))]
#[derive(Debug)]

View file

@ -1,13 +1,13 @@
use std::borrow::Cow;
use crate::parsers::{line, take_lines_while};
use nom::{
bytes::complete::tag_no_case,
character::complete::{alpha1, space1},
IResult,
};
use crate::parsers::{line, take_lines_while};
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "ser", derive(serde::Serialize))]
#[derive(Debug)]

View file

@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::iter::once;
use memchr::memchr_iter;
use std::iter::once;
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "ser", derive(serde::Serialize))]

View file

@ -1,6 +1,7 @@
use nom::{bytes::complete::take_while_m_n, IResult};
use std::usize;
use nom::{bytes::complete::take_while_m_n, IResult};
use crate::parsers::eol;
pub(crate) fn parse_rule(input: &str) -> IResult<&str, ()> {

View file

@ -1,6 +1,7 @@
//! Headline Title
use std::borrow::Cow;
use std::collections::HashMap;
use memchr::memrchr;
use nom::{
@ -13,9 +14,8 @@ use nom::{
sequence::{delimited, preceded},
Err, IResult,
};
use std::collections::HashMap;
use crate::config::ParseConfig;
use crate::config::{ParseConfig, DEFAULT_CONFIG};
use crate::elements::{Drawer, Planning};
use crate::parsers::{line, skip_empty_lines, take_one_word};
@ -196,15 +196,10 @@ impl Title<'_> {
}
}
#[cfg(test)]
lazy_static::lazy_static! {
static ref CONFIG: ParseConfig = ParseConfig::default();
}
#[test]
fn parse_headline_() {
assert_eq!(
parse_headline("**** DONE [#A] COMMENT Title :tag:a2%:", &CONFIG),
parse_headline("**** DONE [#A] COMMENT Title :tag:a2%:", &DEFAULT_CONFIG),
Ok((
"",
(
@ -217,27 +212,27 @@ fn parse_headline_() {
))
);
assert_eq!(
parse_headline("**** ToDO [#A] COMMENT Title", &CONFIG),
parse_headline("**** ToDO [#A] COMMENT Title", &DEFAULT_CONFIG),
Ok(("", (4, None, None, "ToDO [#A] COMMENT Title", vec![])))
);
assert_eq!(
parse_headline("**** T0DO [#A] COMMENT Title", &CONFIG),
parse_headline("**** T0DO [#A] COMMENT Title", &DEFAULT_CONFIG),
Ok(("", (4, None, None, "T0DO [#A] COMMENT Title", vec![])))
);
assert_eq!(
parse_headline("**** DONE [#1] COMMENT Title", &CONFIG),
parse_headline("**** DONE [#1] COMMENT Title", &DEFAULT_CONFIG),
Ok(("", (4, Some("DONE"), None, "[#1] COMMENT Title", vec![],)))
);
assert_eq!(
parse_headline("**** DONE [#a] COMMENT Title", &CONFIG),
parse_headline("**** DONE [#a] COMMENT Title", &DEFAULT_CONFIG),
Ok(("", (4, Some("DONE"), None, "[#a] COMMENT Title", vec![],)))
);
assert_eq!(
parse_headline("**** Title :tag:a2%", &CONFIG),
parse_headline("**** Title :tag:a2%", &DEFAULT_CONFIG),
Ok(("", (4, None, None, "Title :tag:a2%", vec![],)))
);
assert_eq!(
parse_headline("**** Title tag:a2%:", &CONFIG),
parse_headline("**** Title tag:a2%:", &DEFAULT_CONFIG),
Ok(("", (4, None, None, "Title tag:a2%:", vec![],)))
);
@ -278,14 +273,14 @@ fn parse_properties_drawer_() {
// #[test]
// fn is_commented() {
// assert!(Title::parse("* COMMENT Title", &CONFIG)
// assert!(Title::parse("* COMMENT Title", &DEFAULT_CONFIG)
// .1
// .is_commented());
// assert!(!Title::parse("* Title", &CONFIG).1.is_commented());
// assert!(!Title::parse("* C0MMENT Title", &CONFIG)
// assert!(!Title::parse("* Title", &DEFAULT_CONFIG).1.is_commented());
// assert!(!Title::parse("* C0MMENT Title", &DEFAULT_CONFIG)
// .1
// .is_commented());
// assert!(!Title::parse("* comment Title", &CONFIG)
// assert!(!Title::parse("* comment Title", &DEFAULT_CONFIG)
// .1
// .is_commented());
// }

View file

@ -1,10 +1,12 @@
use super::write_datetime;
use crate::elements::Element;
use jetscii::{bytes, BytesConst};
use std::fmt;
use std::io::{Error, Write};
use std::marker::PhantomData;
use jetscii::{bytes, BytesConst};
use crate::elements::Element;
use crate::export::write_datetime;
pub struct Escape<S: AsRef<str>>(pub S);
impl<S: AsRef<str>> fmt::Display for Escape<S> {

View file

@ -1,7 +1,8 @@
use super::write_datetime;
use crate::elements::{Element, Timestamp};
use std::io::{Error, Write};
use crate::elements::{Element, Timestamp};
use crate::export::write_datetime;
pub trait OrgHandler<E: From<Error>> {
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), E> {
use Element::*;

View file

@ -4,8 +4,7 @@ use std::borrow::Cow;
use crate::config::ParseConfig;
use crate::elements::{Element, Title};
use crate::parsers::{parse_container, Container, OwnedArena};
use crate::Org;
use crate::OrgizeError;
use crate::{Org, OrgizeError};
#[derive(Copy, Clone, Debug)]
pub struct HeadlineNode {

View file

@ -1,7 +1,7 @@
use indextree::{Arena, NodeEdge, NodeId};
use std::io::{Error, Write};
use crate::config::ParseConfig;
use crate::config::{ParseConfig, DEFAULT_CONFIG};
use crate::elements::{Element, Title};
use crate::export::*;
use crate::node::{DocumentNode, HeadlineNode};
@ -29,7 +29,7 @@ impl<'a> Org<'a> {
/// Create a new Org struct from parsing `text`, using the default ParseConfig
pub fn parse(text: &'a str) -> Org<'a> {
Org::parse_with_config(text, &ParseConfig::default())
Org::parse_with_config(text, &DEFAULT_CONFIG)
}
/// Create a new Org struct from parsing `text`, using a custom ParseConfig