From 6b1e2a26beea0433a60b75f9e77376d1fc0333ed Mon Sep 17 00:00:00 2001 From: PoiScript Date: Fri, 11 Jan 2019 23:11:07 +0800 Subject: [PATCH] chore: impl Debug for all struct and enum --- src/elements/block.rs | 3 ++- src/elements/dyn_block.rs | 3 ++- src/elements/fn_def.rs | 3 ++- src/elements/keyword.rs | 20 +++++++++++++------- src/elements/rule.rs | 3 ++- src/headline.rs | 3 ++- src/objects/cookie.rs | 7 +++++-- src/objects/fn_ref.rs | 3 ++- src/objects/fragment.rs | 3 ++- src/objects/inline_call.rs | 3 ++- src/objects/inline_src.rs | 3 ++- src/objects/link.rs | 7 +++++-- src/objects/macros.rs | 3 ++- src/objects/snippet.rs | 7 +++++-- src/objects/target.rs | 16 ++++++++++++---- src/objects/timestamp.rs | 2 ++ src/parser.rs | 3 ++- 17 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/elements/block.rs b/src/elements/block.rs index a68b829..37b718b 100644 --- a/src/elements/block.rs +++ b/src/elements/block.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Block; impl Block { diff --git a/src/elements/dyn_block.rs b/src/elements/dyn_block.rs index e748542..ad7f0a3 100644 --- a/src/elements/dyn_block.rs +++ b/src/elements/dyn_block.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct DynBlock<'a> { pub name: &'a str, pub para: &'a str, diff --git a/src/elements/fn_def.rs b/src/elements/fn_def.rs index 4be6a59..0bcfb33 100644 --- a/src/elements/fn_def.rs +++ b/src/elements/fn_def.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct FnDef<'a> { pub label: &'a str, pub contents: &'a str, diff --git a/src/elements/keyword.rs b/src/elements/keyword.rs index d58beb6..ebb1f0d 100644 --- a/src/elements/keyword.rs +++ b/src/elements/keyword.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Keyword<'a> { pub key: &'a str, pub value: &'a str, @@ -6,11 +7,14 @@ pub struct Keyword<'a> { impl<'a> Keyword<'a> { pub fn parse(src: &'a str) -> Option<(Keyword<'a>, usize)> { - starts_with!(src, "#+"); + if cfg!(test) { + starts_with!(src, "#+"); + } let key = until_while!(src, 2, b':', |c: u8| c.is_ascii_uppercase() || c == b'_'); - let end = eol!(src); + // includes the eol character + let end = src.find('\n').map(|i| i + 1).unwrap_or(src.len()); Some(( Keyword { @@ -22,14 +26,16 @@ impl<'a> Keyword<'a> { } } -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct AffKeyword<'a> { pub key: AffKeywordKey<'a>, pub option: Option<&'a str>, pub value: &'a str, } -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub enum AffKeywordKey<'a> { Caption, Header, @@ -141,13 +147,13 @@ fn parse() { ) ); assert_eq!( - Keyword::parse("#+KEY:VALUE").unwrap(), + Keyword::parse("#+KEY:VALUE\n").unwrap(), ( Keyword { key: "KEY", value: "VALUE", }, - "#+KEY:VALUE".len() + "#+KEY:VALUE\n".len() ) ); assert!(Keyword::parse("#+KE Y: VALUE").is_none()); diff --git a/src/elements/rule.rs b/src/elements/rule.rs index ee1f2fc..27e3b35 100644 --- a/src/elements/rule.rs +++ b/src/elements/rule.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Rule; impl Rule { diff --git a/src/headline.rs b/src/headline.rs index 8497cb5..5cf2f60 100644 --- a/src/headline.rs +++ b/src/headline.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Headline<'a> { pub level: usize, pub priority: Option, diff --git a/src/objects/cookie.rs b/src/objects/cookie.rs index 2311405..d1513dc 100644 --- a/src/objects/cookie.rs +++ b/src/objects/cookie.rs @@ -1,11 +1,14 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Cookie<'a> { value: &'a str, } impl<'a> Cookie<'a> { pub fn parse(src: &'a str) -> Option<(Cookie<'a>, usize)> { - starts_with!(src, '['); + if cfg!(test) { + starts_with!(src, '['); + } let num1 = until_while!(src, 1, |c| c == b'%' || c == b'/', |c: u8| c .is_ascii_digit()); diff --git a/src/objects/fn_ref.rs b/src/objects/fn_ref.rs index 2b1f777..e4a502a 100644 --- a/src/objects/fn_ref.rs +++ b/src/objects/fn_ref.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct FnRef<'a> { label: Option<&'a str>, definition: Option<&'a str>, diff --git a/src/objects/fragment.rs b/src/objects/fragment.rs index a5fa8d0..21c0fa6 100644 --- a/src/objects/fragment.rs +++ b/src/objects/fragment.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Fragment<'a> { value: &'a str, } diff --git a/src/objects/inline_call.rs b/src/objects/inline_call.rs index ecbbdde..efe8d1b 100644 --- a/src/objects/inline_call.rs +++ b/src/objects/inline_call.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct InlineCall<'a> { pub name: &'a str, pub args: &'a str, diff --git a/src/objects/inline_src.rs b/src/objects/inline_src.rs index 83d3942..c4b2c3c 100644 --- a/src/objects/inline_src.rs +++ b/src/objects/inline_src.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct InlineSrc<'a> { pub lang: &'a str, pub option: Option<&'a str>, diff --git a/src/objects/link.rs b/src/objects/link.rs index de33b08..27ee9c7 100644 --- a/src/objects/link.rs +++ b/src/objects/link.rs @@ -1,4 +1,5 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Link<'a> { pub path: &'a str, pub desc: Option<&'a str>, @@ -6,7 +7,9 @@ pub struct Link<'a> { impl<'a> Link<'a> { pub fn parse(src: &'a str) -> Option<(Link<'a>, usize)> { - starts_with!(src, "[["); + if cfg!(test) { + starts_with!(src, "[["); + } let path = until_while!(src, 2, b']', |c| c != b']' && c != b'<' diff --git a/src/objects/macros.rs b/src/objects/macros.rs index c00db66..0d97b88 100644 --- a/src/objects/macros.rs +++ b/src/objects/macros.rs @@ -1,6 +1,7 @@ use jetscii::Substring; -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Macros<'a> { pub name: &'a str, pub args: Option<&'a str>, diff --git a/src/objects/snippet.rs b/src/objects/snippet.rs index 6d63616..51f4316 100644 --- a/src/objects/snippet.rs +++ b/src/objects/snippet.rs @@ -1,6 +1,7 @@ use jetscii::Substring; -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Snippet<'a> { pub name: &'a str, pub value: &'a str, @@ -8,7 +9,9 @@ pub struct Snippet<'a> { impl<'a> Snippet<'a> { pub fn parse(src: &'a str) -> Option<(Snippet<'a>, usize)> { - starts_with!(src, "@@"); + if cfg!(test) { + starts_with!(src, "@@"); + } let name = until_while!(src, 2, b':', |c: u8| c.is_ascii_alphanumeric() || c == b'-'); diff --git a/src/objects/target.rs b/src/objects/target.rs index 2f8bf92..dfc5e8b 100644 --- a/src/objects/target.rs +++ b/src/objects/target.rs @@ -1,10 +1,14 @@ -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] // TODO: text-markup, entities, latex-fragments, subscript and superscript pub struct RadioTarget<'a>(&'a str); impl<'a> RadioTarget<'a> { pub fn parse(src: &'a str) -> Option<(RadioTarget<'a>, usize)> { - starts_with!(src, "<<<"); + if cfg!(test) { + starts_with!(src, "<<<"); + } + expect!(src, 3, |c| c != b' '); let end = until_while!(src, 3, b'>', |c| c != b'<' && c != b'\n'); @@ -17,12 +21,16 @@ impl<'a> RadioTarget<'a> { } } -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Target<'a>(&'a str); impl<'a> Target<'a> { pub fn parse(src: &'a str) -> Option<(Target<'a>, usize)> { - starts_with!(src, "<<"); + if cfg!(test) { + starts_with!(src, "<<"); + } + expect!(src, 2, |c| c != b' '); let end = until_while!(src, 2, b'>', |c| c != b'<' && c != b'\n'); diff --git a/src/objects/timestamp.rs b/src/objects/timestamp.rs index db20dbc..8856f98 100644 --- a/src/objects/timestamp.rs +++ b/src/objects/timestamp.rs @@ -1,3 +1,5 @@ +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Time<'a> { pub date: &'a str, } diff --git a/src/parser.rs b/src/parser.rs index f855290..941d306 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -24,7 +24,8 @@ pub enum Container { Underline { end: usize }, } -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub enum Event<'a> { StartHeadline(Headline<'a>), EndHeadline,