Apply more suggestions.

This commit is contained in:
Tom Alexander 2023-10-16 18:29:21 -04:00
parent 4b6c717812
commit 3069711447
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
22 changed files with 86 additions and 114 deletions

View File

@ -348,10 +348,7 @@ mod tests {
let input = r#" (foo "b(a)r" baz ) "#;
let (remaining, parsed) = sexp(input).expect("Parse the input");
assert_eq!(remaining, "");
assert!(match parsed {
Token::List(_) => true,
_ => false,
});
assert!(matches!(parsed, Token::List(_)));
let children = match parsed {
Token::List(children) => children,
_ => panic!("Should be a list."),
@ -364,14 +361,14 @@ mod tests {
r#"foo"#
);
assert_eq!(
match children.iter().nth(1) {
match children.get(1) {
Some(Token::Atom(body)) => *body,
_ => panic!("Second child should be an atom."),
},
r#""b(a)r""#
);
assert_eq!(
match children.iter().nth(2) {
match children.get(2) {
Some(Token::Atom(body)) => *body,
_ => panic!("Third child should be an atom."),
},

View File

@ -117,10 +117,7 @@ fn get_emacs_standard_properties<'b, 's>(
emacs: &'b Token<'s>,
) -> Result<EmacsStandardProperties, Box<dyn std::error::Error>> {
let children = emacs.as_list()?;
let attributes_child = children
.iter()
.nth(1)
.ok_or("Should have an attributes child.")?;
let attributes_child = children.get(1).ok_or("Should have an attributes child.")?;
let attributes_map = attributes_child.as_map()?;
let standard_properties = attributes_map.get(":standard-properties");
Ok(if standard_properties.is_some() {

View File

@ -20,8 +20,7 @@ impl FileAccessInterface for LocalFileAccessInterface {
fn read_file(&self, path: &str) -> Result<String, std::io::Error> {
let final_path = self
.working_directory
.as_ref()
.map(PathBuf::as_path)
.as_deref()
.map(|pb| pb.join(path))
.unwrap_or_else(|| PathBuf::from(path));
Ok(std::fs::read_to_string(final_path)?)

View File

@ -126,14 +126,10 @@ impl<'g, 's> Default for GlobalSettings<'g, 's> {
}
}
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub enum HeadlineLevelFilter {
Odd,
#[default]
OddEven,
}
impl Default for HeadlineLevelFilter {
fn default() -> Self {
HeadlineLevelFilter::OddEven
}
}

View File

@ -64,7 +64,7 @@ impl<'a, T> Iterator for IterList<'a, T> {
fn next(&mut self) -> Option<Self::Item> {
let ret = self.next;
self.next = self.next.map(|this| this.get_parent()).flatten();
self.next = self.next.and_then(|link| link.get_parent());
ret
}
}

View File

@ -7,7 +7,7 @@ pub(crate) use parser_with_context;
macro_rules! bind_context {
($target:expr, $context:expr) => {
move |i| $target($context, i)
|i| $target($context, i)
};
}
pub(crate) use bind_context;

View File

@ -1,3 +1,4 @@
#[allow(clippy::module_inception)]
mod error;
pub(crate) use error::CustomError;
pub(crate) use error::MyError;

View File

@ -16,7 +16,7 @@ use nom::sequence::tuple;
use super::object_parser::standard_set_object;
use super::util::confine_context;
use crate::context::parser_with_context;
use crate::context::bind_context;
use crate::context::Context;
use crate::context::ContextElement;
use crate::context::GlobalSettings;
@ -64,7 +64,7 @@ where
eof,
)),
|(_, _, objects, _, _)| objects,
)))(kw.key.into())
)))(kw.key)
.expect("Parser should always succeed.");
ret.insert(
translated_name,
@ -85,8 +85,9 @@ where
map_parser(
recognize(many_till(anychar, peek(tuple((tag("]"), eof))))),
confine_context(|i| {
all_consuming(many0(parser_with_context!(standard_set_object)(
&initial_context,
all_consuming(many0(bind_context!(
standard_set_object,
&initial_context
)))(i)
}),
),
@ -98,9 +99,9 @@ where
.expect("Object parser should always succeed.");
// TODO: This should be omitting footnote references
let (_remaining, objects) =
all_consuming(many0(parser_with_context!(standard_set_object)(
&initial_context,
let (_remaining, objects) = all_consuming(many0(bind_context!(
standard_set_object,
&initial_context
)))(kw.value.into())
.expect("Object parser should always succeed.");
@ -121,7 +122,7 @@ where
fn translate_name<'g, 's>(global_settings: &'g GlobalSettings<'g, 's>, name: &'s str) -> String {
let name_until_optval = name
.split_once("[")
.split_once('[')
.map(|(before, _after)| before)
.unwrap_or(name);
for (src, dst) in global_settings.element_keyword_translation_alist {

View File

@ -66,8 +66,7 @@ where
}
let (remaining, _ws) = space0(remaining)?;
let (remaining, (value, (call, inside_header, arguments, end_header))) =
consumed(babel_call_value)(remaining)?;
let (remaining, (value, babel_call_value)) = consumed(babel_call_value)(remaining)?;
let (remaining, _ws) = tuple((space0, org_line_ending))(remaining)?;
let (remaining, _trailing_ws) =
@ -83,33 +82,36 @@ where
affiliated_keywords,
),
value: Into::<&str>::into(value).trim_end(),
call: call.map(Into::<&str>::into),
inside_header: inside_header.map(Into::<&str>::into),
arguments: arguments.map(Into::<&str>::into),
end_header: end_header.map(Into::<&str>::into),
call: babel_call_value.call.map(Into::<&str>::into),
inside_header: babel_call_value.inside_header.map(Into::<&str>::into),
arguments: babel_call_value.arguments.map(Into::<&str>::into),
end_header: babel_call_value.end_header.map(Into::<&str>::into),
},
))
}
#[derive(Debug)]
struct BabelCallValue<'s> {
call: Option<OrgSource<'s>>,
inside_header: Option<OrgSource<'s>>,
arguments: Option<OrgSource<'s>>,
end_header: Option<OrgSource<'s>>,
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn babel_call_value<'s>(
input: OrgSource<'s>,
) -> Res<
OrgSource<'s>,
(
Option<OrgSource<'s>>,
Option<OrgSource<'s>>,
Option<OrgSource<'s>>,
Option<OrgSource<'s>>,
),
> {
fn babel_call_value<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, BabelCallValue<'s>> {
let (remaining, call) = opt(babel_call_call)(input)?;
let (remaining, inside_header) = opt(inside_header)(remaining)?;
let (remaining, arguments) = opt(arguments)(remaining)?;
let (remaining, end_header) = opt(end_header)(remaining)?;
Ok((
remaining,
(call, inside_header, arguments.flatten(), end_header),
BabelCallValue {
call,
inside_header,
arguments: arguments.flatten(),
end_header,
},
))
}

View File

@ -205,6 +205,7 @@ fn _global_suffix_end<'b, 'g, 'r, 's>(
#[cfg(test)]
mod tests {
use super::*;
use crate::context::bind_context;
use crate::context::Context;
use crate::context::GlobalSettings;
use crate::context::List;
@ -219,7 +220,7 @@ mod tests {
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let paragraph_matcher = parser_with_context!(element(true))(&initial_context);
let paragraph_matcher = bind_context!(element(true), &initial_context);
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
let first_paragraph = match first_paragraph {
Element::Paragraph(paragraph) => paragraph,

View File

@ -200,7 +200,7 @@ where
let (remaining, output) = inner(input)?;
if remaining.get_bracket_depth() - pre_bracket_depth != 0 {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"UnbalancedBrackets".into(),
"UnbalancedBrackets",
))));
}
Ok((remaining, output))

View File

@ -36,7 +36,7 @@ pub(crate) fn comment<'b, 'g, 'r, 's>(
) -> Res<OrgSource<'s>, Comment<'s>> {
if immediate_in_section(context, "comment") {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
"Cannot nest objects of the same element",
))));
}
let parser_context = ContextElement::Context("comment");
@ -104,6 +104,7 @@ pub(crate) fn detect_comment<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()>
#[cfg(test)]
mod tests {
use super::*;
use crate::context::bind_context;
use crate::context::Context;
use crate::context::ContextElement;
use crate::context::GlobalSettings;
@ -119,7 +120,7 @@ mod tests {
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let comment_matcher = parser_with_context!(comment)(&initial_context);
let comment_matcher = bind_context!(comment, &initial_context);
let (remaining, first_comment) = comment_matcher(input).expect("Parse first comment");
assert_eq!(
Into::<&str>::into(remaining),

View File

@ -11,6 +11,7 @@ use super::in_buffer_settings::scan_for_in_buffer_settings;
use super::org_source::OrgSource;
use super::section::zeroth_section;
use super::util::get_consumed;
use crate::context::bind_context;
use crate::context::parser_with_context;
use crate::context::Context;
use crate::context::ContextElement;
@ -30,7 +31,7 @@ use crate::types::Object;
///
/// This is a main entry point for Organic. It will parse the full contents of the input string as an org-mode document without an underlying file attached.
#[allow(dead_code)]
pub fn parse<'s>(input: &'s str) -> Result<Document<'s>, Box<dyn std::error::Error>> {
pub fn parse(input: &str) -> Result<Document<'_>, Box<dyn std::error::Error>> {
parse_file_with_settings::<&Path>(input, &GlobalSettings::default(), None)
}
@ -40,10 +41,10 @@ pub fn parse<'s>(input: &'s str) -> Result<Document<'s>, Box<dyn std::error::Err
///
/// file_path is not used for reading the file contents. It is only used for determining the document category and filling in the path attribute on the Document.
#[allow(dead_code)]
pub fn parse_file<'s, P: AsRef<Path>>(
input: &'s str,
pub fn parse_file<P: AsRef<Path>>(
input: &str,
file_path: Option<P>,
) -> Result<Document<'s>, Box<dyn std::error::Error>> {
) -> Result<Document<'_>, Box<dyn std::error::Error>> {
parse_file_with_settings(input, &GlobalSettings::default(), file_path)
}
@ -77,7 +78,7 @@ pub fn parse_file_with_settings<'g, 's, P: AsRef<Path>>(
let initial_context = Context::new(global_settings, List::new(&initial_context));
let wrapped_input = OrgSource::new(input);
let mut doc =
all_consuming(parser_with_context!(document_org_source)(&initial_context))(wrapped_input)
all_consuming(bind_context!(document_org_source, &initial_context))(wrapped_input)
.map_err(|err| err.to_string())
.map(|(_remaining, parsed_document)| parsed_document)?;
if let Some(file_path) = file_path {
@ -101,10 +102,7 @@ pub fn parse_file_with_settings<'g, 's, P: AsRef<Path>>(
///
/// This will not prevent additional settings from being learned during parsing, for example when encountering a "#+TODO".
#[allow(dead_code)]
fn document<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: &'s str,
) -> Res<&'s str, Document<'s>> {
fn document<'s>(context: RefContext<'_, '_, '_, 's>, input: &'s str) -> Res<&'s str, Document<'s>> {
let (remaining, doc) = document_org_source(context, input.into()).map_err(convert_error)?;
Ok((Into::<&str>::into(remaining), doc))
}
@ -137,8 +135,7 @@ fn document_org_source<'b, 'g, 'r, 's>(
scan_for_in_buffer_settings(setup_file.into()).map_err(|err| {
eprintln!("{}", err);
nom::Err::Error(CustomError::MyError(MyError(
"TODO: make this take an owned string so I can dump err.to_string() into it."
.into(),
"TODO: make this take an owned string so I can dump err.to_string() into it.",
)))
})?;
final_settings.extend(setup_file_settings);
@ -148,8 +145,7 @@ fn document_org_source<'b, 'g, 'r, 's>(
.map_err(|err| {
eprintln!("{}", err);
nom::Err::Error(CustomError::MyError(MyError(
"TODO: make this take an owned string so I can dump err.to_string() into it."
.into(),
"TODO: make this take an owned string so I can dump err.to_string() into it.",
)))
})?;
let new_context = context.with_global_settings(&new_settings);

View File

@ -49,7 +49,7 @@ where
{
if immediate_in_section(context, "drawer") {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
"Cannot nest objects of the same element",
))));
}
start_of_line(remaining)?;

View File

@ -55,7 +55,7 @@ where
{
if immediate_in_section(context, "dynamic block") {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
"Cannot nest objects of the same element",
))));
}
@ -79,10 +79,7 @@ where
let parser_context = context.with_additional_node(&contexts[0]);
let parser_context = parser_context.with_additional_node(&contexts[1]);
let parser_context = parser_context.with_additional_node(&contexts[2]);
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let parameters = parameters.map(|(_ws, parameters)| parameters);
let element_matcher = parser_with_context!(element(true))(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
not(exit_matcher)(remaining)?;

View File

@ -301,9 +301,7 @@ fn _detect_element<'b, 'g, 'r, 's>(
input
);
if let Ok((_, _)) = detect_comment(input) {
return Ok((input, ()));
}
element!(detect_comment, input);
ak_element!(
detect_fixed_width_area,
@ -326,6 +324,6 @@ fn _detect_element<'b, 'g, 'r, 's>(
}
Err(nom::Err::Error(CustomError::MyError(MyError(
"No element detected.".into(),
"No element detected.",
))))
}

View File

@ -60,21 +60,16 @@ fn name<'b, 'g, 'r, 's>(
let result = tuple((
tag::<_, _, CustomError<_>>(entity.name),
alt((
verify(map(tag("{}"), |_| true), |_| !entity.name.ends_with(" ")),
verify(map(tag("{}"), |_| true), |_| !entity.name.ends_with(' ')),
map(peek(recognize(entity_end)), |_| false),
)),
))(input);
match result {
Ok((remaining, (ent, use_brackets))) => {
if let Ok((remaining, (ent, use_brackets))) = result {
return Ok((remaining, (entity, ent, use_brackets)));
}
Err(_) => {}
}
}
Err(nom::Err::Error(CustomError::MyError(MyError(
"NoEntity".into(),
))))
Err(nom::Err::Error(CustomError::MyError(MyError("NoEntity"))))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]

View File

@ -49,7 +49,7 @@ where
{
if immediate_in_section(context, "footnote definition") {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
"Cannot nest objects of the same element",
))));
}
start_of_line(remaining)?;
@ -157,6 +157,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::context::bind_context;
use crate::context::Context;
use crate::context::GlobalSettings;
use crate::context::List;
@ -174,7 +175,7 @@ line footnote.",
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let footnote_definition_matcher = parser_with_context!(element(true))(&initial_context);
let footnote_definition_matcher = bind_context!(element(true), &initial_context);
let (remaining, first_footnote_definition) =
footnote_definition_matcher(input).expect("Parse first footnote_definition");
let (remaining, second_footnote_definition) =
@ -211,7 +212,7 @@ not in the footnote.",
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let footnote_definition_matcher = parser_with_context!(element(true))(&initial_context);
let footnote_definition_matcher = bind_context!(element(true), &initial_context);
let (remaining, first_footnote_definition) =
footnote_definition_matcher(input).expect("Parse first footnote_definition");
assert_eq!(Into::<&str>::into(remaining), "not in the footnote.");

View File

@ -177,7 +177,7 @@ fn _footnote_definition_end<'b, 'g, 'r, 's>(
if current_depth > 0 {
// Its impossible for the next character to end the footnote reference definition if we're any amount of brackets deep
return Err(nom::Err::Error(CustomError::MyError(MyError(
"NoFootnoteReferenceDefinitionEnd".into(),
"NoFootnoteReferenceDefinitionEnd",
))));
}
if current_depth < 0 {

View File

@ -61,10 +61,10 @@ where
let (remaining, (_begin, name)) = tuple((
tag_no_case("#+begin_"),
verify(name, |name: &OrgSource<'_>| {
match Into::<&str>::into(name).to_lowercase().as_str() {
"comment" | "example" | "export" | "src" | "verse" => false,
_ => true,
}
!matches!(
Into::<&str>::into(name).to_lowercase().as_str(),
"comment" | "example" | "export" | "src" | "verse",
)
}),
))(remaining)?;
let name = Into::<&str>::into(name);
@ -233,7 +233,7 @@ fn greater_block_body<'c, 'b, 'g, 'r, 's>(
) -> Res<OrgSource<'s>, (&'s str, Vec<Element<'s>>)> {
if in_section(context, context_name) {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
"Cannot nest objects of the same element",
))));
}
let exit_with_name = greater_block_end(name);
@ -288,7 +288,7 @@ fn parameters<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
recognize(many_till(anychar, peek(tuple((space0, line_ending)))))(input)
}
fn greater_block_end<'c>(name: &'c str) -> impl ContextMatcher + 'c {
fn greater_block_end(name: &str) -> impl ContextMatcher + '_ {
move |context, input: OrgSource<'_>| _greater_block_end(context, input, name)
}

View File

@ -206,11 +206,7 @@ fn headline<'b, 'g, 'r, 's>(
.map(|(_, (_, title))| title)
.unwrap_or(Vec::new()),
tags: maybe_tags
.map(|(_ws, tags)| {
tags.into_iter()
.map(|single_tag| Into::<&str>::into(single_tag))
.collect()
})
.map(|(_ws, tags)| tags.into_iter().map(Into::<&str>::into).collect())
.unwrap_or(Vec::new()),
is_footnote_section,
},
@ -265,12 +261,9 @@ fn heading_keyword<'b, 'g, 'r, 's>(
.map(String::as_str)
{
let result = tag::<_, _, CustomError<_>>(todo_keyword)(input);
match result {
Ok((remaining, ent)) => {
if let Ok((remaining, ent)) = result {
return Ok((remaining, (TodoKeywordType::Todo, ent)));
}
Err(_) => {}
}
}
for todo_keyword in global_settings
.complete_todo_keywords
@ -278,20 +271,17 @@ fn heading_keyword<'b, 'g, 'r, 's>(
.map(String::as_str)
{
let result = tag::<_, _, CustomError<_>>(todo_keyword)(input);
match result {
Ok((remaining, ent)) => {
if let Ok((remaining, ent)) = result {
return Ok((remaining, (TodoKeywordType::Done, ent)));
}
Err(_) => {}
}
}
Err(nom::Err::Error(CustomError::MyError(MyError(
"NoTodoKeyword".into(),
"NoTodoKeyword",
))))
}
}
fn priority_cookie<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PriorityCookie> {
fn priority_cookie(input: OrgSource<'_>) -> Res<OrgSource<'_>, PriorityCookie> {
let (remaining, (_, priority_character, _)) = tuple((
tag("[#"),
verify(anychar, |c| c.is_alphanumeric()),
@ -299,7 +289,7 @@ fn priority_cookie<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PriorityCooki
))(input)?;
let cookie = PriorityCookie::try_from(priority_character).map_err(|_| {
nom::Err::Error(CustomError::MyError(MyError(
"Failed to cast priority cookie to number.".into(),
"Failed to cast priority cookie to number.",
)))
})?;
Ok((remaining, cookie))

View File

@ -132,7 +132,7 @@ fn _header_end<'b, 'g, 'r, 's>(
if current_depth > 0 {
// Its impossible for the next character to end the header if we're any amount of bracket deep
return Err(nom::Err::Error(CustomError::MyError(MyError(
"NoHeaderEnd".into(),
"NoHeaderEnd",
))));
}
if current_depth < 0 {