From f5699ce830f14e8c6dc5862e62a4f61a6f70c367 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 31 Oct 2023 16:25:52 -0400 Subject: [PATCH] Remove PartialEq from Object. --- src/compare/diff.rs | 4 +- src/parser/babel_call.rs | 4 +- src/parser/citation.rs | 48 +++++++++++++--------- src/parser/org_source.rs | 2 +- src/parser/radio_link.rs | 86 ++++++++++++++++++++++++++------------ src/types/object.rs | 89 ++++++++++++++++++++-------------------- 6 files changed, 136 insertions(+), 97 deletions(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index e9da204..6054b74 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -128,7 +128,7 @@ pub struct DiffResult<'b, 's> { emacs_token: &'b Token<'s>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub(crate) enum DiffStatus { Good, Bad, @@ -164,7 +164,7 @@ impl<'b, 's> DiffEntry<'b, 's> { fn is_immediately_bad(&self) -> bool { match self { - DiffEntry::DiffResult(diff) => diff.status == DiffStatus::Bad, + DiffEntry::DiffResult(diff) => matches!(diff.status, DiffStatus::Bad), DiffEntry::DiffLayer(_) => false, } } diff --git a/src/parser/babel_call.rs b/src/parser/babel_call.rs index 0310505..9574277 100644 --- a/src/parser/babel_call.rs +++ b/src/parser/babel_call.rs @@ -225,7 +225,7 @@ fn impl_balanced_bracket< let contents_end = remaining; let (remaining, _) = end_parser(remaining)?; - let contents = if contents_start != contents_end { + let contents = if Into::<&str>::into(contents_start) != Into::<&str>::into(contents_end) { Some(contents_start.get_until(contents_end)) } else { None @@ -244,7 +244,7 @@ mod tests { let input = OrgSource::new("()"); let (remaining, call) = opt(babel_call_call)(input)?; assert_eq!(Into::<&str>::into(remaining), "()"); - assert_eq!(call, None); + assert!(matches!(call, None)); Ok(()) } } diff --git a/src/parser/citation.rs b/src/parser/citation.rs index ffccb20..e7f7afe 100644 --- a/src/parser/citation.rs +++ b/src/parser/citation.rs @@ -210,12 +210,12 @@ mod tests { use crate::context::GlobalSettings; use crate::context::List; use crate::parser::element_parser::element; - use crate::types::CitationReference; use crate::types::Element; use crate::types::GetStandardProperties; + use crate::types::StandardProperties; #[test] - fn citation_simple() { + fn citation_simple() -> Result<(), Box> { let input = OrgSource::new("[cite:@foo]"); let global_settings = GlobalSettings::default(); let initial_context = ContextElement::document_context(); @@ -232,23 +232,31 @@ mod tests { "[cite:@foo]" ); assert_eq!(first_paragraph.children.len(), 1); - assert_eq!( - first_paragraph - .children - .first() - .expect("Len already asserted to be 1"), - &Object::Citation(Citation { - source: "[cite:@foo]", - style: None, - prefix: vec![], - suffix: vec![], - children: vec![CitationReference { - source: "@foo", - key: "foo", - prefix: vec![], - suffix: vec![] - }] - }) - ); + + match first_paragraph + .children + .first() + .expect("Len already asserted to be 1.") + { + Object::Citation(inner) => { + assert_eq!(inner.get_source(), "[cite:@foo]"); + assert_eq!(inner.children.len(), 1); + assert!(inner.prefix.is_empty()); + assert!(inner.suffix.is_empty()); + assert!(inner.style.is_none()); + let citation_reference = inner + .children + .first() + .expect("Len already asserted to be 1."); + assert_eq!(citation_reference.get_source(), "@foo"); + assert_eq!(citation_reference.key, "foo"); + assert!(citation_reference.prefix.is_empty()); + assert!(citation_reference.suffix.is_empty()); + } + _ => { + return Err("Child should be a citation.".into()); + } + }; + Ok(()) } } diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index ca2d1a4..83c4f16 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -12,7 +12,7 @@ use nom::Slice; pub(crate) type BracketDepth = i16; -#[derive(Copy, Clone, PartialEq)] +#[derive(Copy, Clone)] pub(crate) struct OrgSource<'s> { full_source: &'s str, start: usize, diff --git a/src/parser/radio_link.rs b/src/parser/radio_link.rs index 03b27eb..4c77890 100644 --- a/src/parser/radio_link.rs +++ b/src/parser/radio_link.rs @@ -179,7 +179,7 @@ mod tests { use crate::types::PlainText; #[test] - fn plain_text_radio_target() { + fn plain_text_radio_target() -> Result<(), Box> { let input = OrgSource::new("foo bar baz"); let radio_target_match = vec![Object::PlainText(PlainText { source: "bar" })]; let global_settings = GlobalSettings { @@ -200,21 +200,31 @@ mod tests { "foo bar baz" ); assert_eq!(first_paragraph.children.len(), 3); - assert_eq!( - first_paragraph - .children - .get(1) - .expect("Len already asserted to be 3"), - &Object::RadioLink(RadioLink { - source: "bar ", - children: vec![Object::PlainText(PlainText { source: "bar" })], - path: "bar" - }) - ); + match first_paragraph + .children + .get(1) + .expect("Len already asserted to be 3.") + { + Object::RadioLink(inner) => { + assert_eq!(inner.get_standard_properties().get_source(), "bar "); + assert_eq!(inner.path, "bar"); + assert_eq!(inner.children.len(), 1); + let child = inner + .children + .get(0) + .expect("Length already asserted to be 1."); + assert!(matches!(child, Object::PlainText(_))); + assert_eq!(child.get_standard_properties().get_source(), "bar"); + } + _ => { + return Err("Child should be a radio link.".into()); + } + }; + Ok(()) } #[test] - fn bold_radio_target() { + fn bold_radio_target() -> Result<(), Box> { let input = OrgSource::new("foo *bar* baz"); let radio_target_match = vec![Object::Bold(Bold { source: "*bar*", @@ -239,19 +249,41 @@ mod tests { "foo *bar* baz" ); assert_eq!(first_paragraph.children.len(), 3); - assert_eq!( - first_paragraph - .children - .get(1) - .expect("Len already asserted to be 3"), - &Object::RadioLink(RadioLink { - source: "*bar* ", - children: vec![Object::Bold(Bold { - source: "*bar* ", - children: vec![Object::PlainText(PlainText { source: "bar" })] - })], - path: "*bar* " - }) - ); + match first_paragraph + .children + .get(1) + .expect("Len already asserted to be 3.") + { + Object::RadioLink(inner) => { + assert_eq!(inner.get_standard_properties().get_source(), "*bar* "); + assert_eq!(inner.path, "*bar* "); + assert_eq!(inner.children.len(), 1); + let child = inner + .children + .get(0) + .expect("Length already asserted to be 1."); + assert!(matches!(child, Object::Bold(_))); + assert_eq!(child.get_standard_properties().get_source(), "*bar* "); + } + _ => { + return Err("Child should be a radio link.".into()); + } + }; + Ok(()) + + // assert_eq!( + // first_paragraph + // .children + // .get(1) + // .expect("Len already asserted to be 3"), + // &Object::RadioLink(RadioLink { + // source: "*bar* ", + // children: vec![Object::Bold(Bold { + // source: "*bar* ", + // children: vec![Object::PlainText(PlainText { source: "bar" })] + // })], + // path: "*bar* " + // }) + // ); } } diff --git a/src/types/object.rs b/src/types/object.rs index 870a1df..f900ccb 100644 --- a/src/types/object.rs +++ b/src/types/object.rs @@ -9,8 +9,7 @@ use super::util::to_lowercase; use super::GetStandardProperties; use super::StandardProperties; -// TODO: Why did we make Object implement PartialEq again? Was it just for tests? -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub enum Object<'s> { Bold(Bold<'s>), Italic(Italic<'s>), @@ -41,48 +40,48 @@ pub enum Object<'s> { Timestamp(Timestamp<'s>), } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Bold<'s> { pub source: &'s str, pub children: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Italic<'s> { pub source: &'s str, pub children: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Underline<'s> { pub source: &'s str, pub children: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct StrikeThrough<'s> { pub source: &'s str, pub children: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Code<'s> { pub source: &'s str, pub contents: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Verbatim<'s> { pub source: &'s str, pub contents: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct PlainText<'s> { pub source: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct RegularLink<'s> { pub source: &'s str, pub link_type: LinkType<'s>, @@ -105,21 +104,21 @@ pub struct RegularLink<'s> { pub application: Option>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct RadioTarget<'s> { pub source: &'s str, pub value: &'s str, pub children: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct RadioLink<'s> { pub source: &'s str, pub path: &'s str, pub children: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct PlainLink<'s> { pub source: &'s str, pub link_type: LinkType<'s>, @@ -129,7 +128,7 @@ pub struct PlainLink<'s> { pub application: Option<&'s str>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct AngleLink<'s> { pub source: &'s str, pub link_type: LinkType<'s>, @@ -147,7 +146,7 @@ pub struct AngleLink<'s> { pub application: Option<&'s str>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct OrgMacro<'s> { pub source: &'s str, @@ -164,7 +163,7 @@ pub struct OrgMacro<'s> { pub value: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Entity<'s> { pub source: &'s str, pub name: &'s str, @@ -177,27 +176,27 @@ pub struct Entity<'s> { pub use_brackets: bool, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct LatexFragment<'s> { pub source: &'s str, pub value: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct ExportSnippet<'s> { pub source: &'s str, pub backend: &'s str, pub contents: Option<&'s str>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct FootnoteReference<'s> { pub source: &'s str, pub label: Option<&'s str>, pub definition: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Citation<'s> { pub source: &'s str, pub style: Option<&'s str>, @@ -206,7 +205,7 @@ pub struct Citation<'s> { pub children: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct CitationReference<'s> { pub source: &'s str, pub key: &'s str, @@ -214,7 +213,7 @@ pub struct CitationReference<'s> { pub suffix: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct InlineBabelCall<'s> { pub source: &'s str, pub value: &'s str, @@ -224,7 +223,7 @@ pub struct InlineBabelCall<'s> { pub end_header: Option<&'s str>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct InlineSourceBlock<'s> { pub source: &'s str, pub language: &'s str, @@ -232,31 +231,31 @@ pub struct InlineSourceBlock<'s> { pub value: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct LineBreak<'s> { pub source: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Target<'s> { pub source: &'s str, pub value: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct StatisticsCookie<'s> { pub source: &'s str, pub value: &'s str, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Subscript<'s> { pub source: &'s str, pub use_brackets: bool, pub children: Vec>, } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct Superscript<'s> { pub source: &'s str, pub use_brackets: bool, @@ -264,7 +263,7 @@ pub struct Superscript<'s> { } // TODO: Perhaps there is an optimization of converting to unix time we can do to shrink this struct. (ref: clippy::large_enum_variant on Element) -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct Timestamp<'s> { pub source: &'s str, pub timestamp_type: TimestampType, @@ -277,7 +276,7 @@ pub struct Timestamp<'s> { pub warning_delay: Option, } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub enum TimestampType { Diary, Active, @@ -286,7 +285,7 @@ pub enum TimestampType { InactiveRange, } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub enum TimestampRangeType { None, DateRange, @@ -299,19 +298,19 @@ pub type DayOfMonthInner = u8; pub type HourInner = u8; pub type MinuteInner = u8; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct Year(YearInner); -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct Month(MonthInner); -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct DayOfMonth(DayOfMonthInner); -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct Hour(HourInner); -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct Minute(MinuteInner); impl Year { @@ -386,7 +385,7 @@ impl Minute { } } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct Date<'s> { year: Year, month: Month, @@ -444,7 +443,7 @@ impl<'s> Date<'s> { } } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct Time<'s> { hour: Hour, minute: Minute, @@ -478,20 +477,20 @@ impl<'s> Time<'s> { } } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub enum RepeaterType { Cumulative, CatchUp, Restart, } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub enum WarningDelayType { All, First, } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub enum TimeUnit { Hour, Day, @@ -502,14 +501,14 @@ pub enum TimeUnit { pub type RepeaterWarningDelayValueType = u16; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct Repeater { pub repeater_type: RepeaterType, pub value: RepeaterWarningDelayValueType, pub unit: TimeUnit, } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct WarningDelay { pub warning_delay_type: WarningDelayType, pub value: RepeaterWarningDelayValueType, @@ -718,7 +717,7 @@ impl<'s> Timestamp<'s> { } } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub enum LinkType<'s> { File, Protocol(Cow<'s, str>), @@ -787,7 +786,7 @@ impl<'s> OrgMacro<'s> { } } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub enum FootnoteReferenceType { Standard, Inline,