Add a utility function to check the first element in an sexp.
This commit is contained in:
parent
821eacf161
commit
a4c6e899ac
@ -1,4 +1,5 @@
|
|||||||
use super::sexp::Token;
|
use super::sexp::Token;
|
||||||
|
use super::util::assert_name;
|
||||||
use crate::compare::util::get_offsets;
|
use crate::compare::util::get_offsets;
|
||||||
use crate::parser::Comment;
|
use crate::parser::Comment;
|
||||||
use crate::parser::Document;
|
use crate::parser::Document;
|
||||||
@ -18,6 +19,7 @@ use crate::DynamicBlock;
|
|||||||
pub struct DiffResult {
|
pub struct DiffResult {
|
||||||
status: DiffStatus,
|
status: DiffStatus,
|
||||||
name: String,
|
name: String,
|
||||||
|
message: Option<String>,
|
||||||
children: Vec<DiffResult>,
|
children: Vec<DiffResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,11 +73,7 @@ pub fn compare_document<'s>(
|
|||||||
rust: &'s Document<'s>,
|
rust: &'s Document<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children.first().ok_or("Should have at least one child.")?;
|
assert_name(emacs, "org-data")?;
|
||||||
let first_child_text = first_child.as_atom()?;
|
|
||||||
if first_child_text != "org-data" {
|
|
||||||
return Err("Document should correspond to an org-data cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -112,6 +110,7 @@ pub fn compare_document<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "document".to_owned(),
|
name: "document".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -122,11 +121,7 @@ fn compare_section<'s>(
|
|||||||
rust: &'s Section<'s>,
|
rust: &'s Section<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children.first().ok_or("Should have at least one child.")?;
|
assert_name(emacs, "section")?;
|
||||||
let first_child_text = first_child.as_atom()?;
|
|
||||||
if first_child_text != "section" {
|
|
||||||
return Err("Section should correspond to a section cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -155,6 +150,7 @@ fn compare_section<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "section".to_owned(),
|
name: "section".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -165,11 +161,7 @@ fn compare_heading<'s>(
|
|||||||
rust: &'s Heading<'s>,
|
rust: &'s Heading<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children.first().ok_or("Should have at least one child.")?;
|
assert_name(emacs, "headline")?;
|
||||||
let first_child_text = first_child.as_atom()?;
|
|
||||||
if first_child_text != "headline" {
|
|
||||||
return Err("Heading should correspond to a headline cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -205,6 +197,7 @@ fn compare_heading<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "heading".to_owned(),
|
name: "heading".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -231,13 +224,7 @@ fn compare_paragraph<'s>(
|
|||||||
rust: &'s Paragraph<'s>,
|
rust: &'s Paragraph<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children
|
assert_name(emacs, "paragraph")?;
|
||||||
.first()
|
|
||||||
.ok_or("Should have at least one child.")?
|
|
||||||
.as_atom()?;
|
|
||||||
if first_child != "paragraph" {
|
|
||||||
return Err("Paragraph should correspond to a paragraph cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -264,6 +251,7 @@ fn compare_paragraph<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "paragraph".to_owned(),
|
name: "paragraph".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -274,13 +262,7 @@ fn compare_plain_list<'s>(
|
|||||||
rust: &'s PlainList<'s>,
|
rust: &'s PlainList<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children
|
assert_name(emacs, "plain-list")?;
|
||||||
.first()
|
|
||||||
.ok_or("Should have at least one child.")?
|
|
||||||
.as_atom()?;
|
|
||||||
if first_child != "plain-list" {
|
|
||||||
return Err("Paragraph should correspond to a paragraph cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -309,6 +291,7 @@ fn compare_plain_list<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "plain-list".to_owned(),
|
name: "plain-list".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -319,13 +302,7 @@ fn compare_plain_list_item<'s>(
|
|||||||
rust: &'s PlainListItem<'s>,
|
rust: &'s PlainListItem<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children
|
assert_name(emacs, "item")?;
|
||||||
.first()
|
|
||||||
.ok_or("Should have at least one child.")?
|
|
||||||
.as_atom()?;
|
|
||||||
if first_child != "item" {
|
|
||||||
return Err("PlainListItem should correspond to an item cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -354,6 +331,7 @@ fn compare_plain_list_item<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "plain-list-item".to_owned(),
|
name: "plain-list-item".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -364,25 +342,14 @@ fn compare_greater_block<'s>(
|
|||||||
rust: &'s GreaterBlock<'s>,
|
rust: &'s GreaterBlock<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children
|
assert_name(
|
||||||
.first()
|
emacs,
|
||||||
.ok_or("Should have at least one child.")?
|
match rust.name.to_lowercase().as_str() {
|
||||||
.as_atom()?;
|
"center" => "center-block",
|
||||||
match rust.name.to_lowercase().as_str() {
|
"quote" => "quote-block",
|
||||||
"center" => {
|
_ => todo!(),
|
||||||
if first_child != "center-block" {
|
},
|
||||||
return Err(
|
)?;
|
||||||
"Center greater blocks should correspond to a center-block cell.".into(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"quote" => {
|
|
||||||
if first_child != "quote-block" {
|
|
||||||
return Err("Quote greater blocks should correspond to a quote-block cell.".into());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => todo!(),
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -411,6 +378,7 @@ fn compare_greater_block<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "greater-block".to_owned(),
|
name: "greater-block".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -421,13 +389,7 @@ fn compare_dynamic_block<'s>(
|
|||||||
rust: &'s DynamicBlock<'s>,
|
rust: &'s DynamicBlock<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children
|
assert_name(emacs, "dynamic-block")?;
|
||||||
.first()
|
|
||||||
.ok_or("Should have at least one child.")?
|
|
||||||
.as_atom()?;
|
|
||||||
if first_child != "dynamic-block" {
|
|
||||||
return Err("Dynamic block should correspond to a dynamic-block cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -456,6 +418,7 @@ fn compare_dynamic_block<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "dynamic-block".to_owned(),
|
name: "dynamic-block".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -466,13 +429,7 @@ fn compare_footnote_definition<'s>(
|
|||||||
rust: &'s FootnoteDefinition<'s>,
|
rust: &'s FootnoteDefinition<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children
|
assert_name(emacs, "footnote-definition")?;
|
||||||
.first()
|
|
||||||
.ok_or("Should have at least one child.")?
|
|
||||||
.as_atom()?;
|
|
||||||
if first_child != "footnote-definition" {
|
|
||||||
return Err("Paragraph should correspond to a paragraph cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -501,6 +458,7 @@ fn compare_footnote_definition<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "footnote-definition".to_owned(),
|
name: "footnote-definition".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -511,13 +469,7 @@ fn compare_comment<'s>(
|
|||||||
rust: &'s Comment<'s>,
|
rust: &'s Comment<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children
|
assert_name(emacs, "comment")?;
|
||||||
.first()
|
|
||||||
.ok_or("Should have at least one child.")?
|
|
||||||
.as_atom()?;
|
|
||||||
if first_child != "comment" {
|
|
||||||
return Err("Comment should correspond to a comment cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -542,6 +494,7 @@ fn compare_comment<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "comment".to_owned(),
|
name: "comment".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -552,13 +505,7 @@ fn compare_drawer<'s>(
|
|||||||
rust: &'s Drawer<'s>,
|
rust: &'s Drawer<'s>,
|
||||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||||
let children = emacs.as_list()?;
|
let children = emacs.as_list()?;
|
||||||
let first_child = children
|
assert_name(emacs, "drawer")?;
|
||||||
.first()
|
|
||||||
.ok_or("Should have at least one child.")?
|
|
||||||
.as_atom()?;
|
|
||||||
if first_child != "drawer" {
|
|
||||||
return Err("Drawer should correspond to a drawer cell.".into());
|
|
||||||
}
|
|
||||||
let mut child_status = Vec::new();
|
let mut child_status = Vec::new();
|
||||||
let mut this_status = DiffStatus::Good;
|
let mut this_status = DiffStatus::Good;
|
||||||
|
|
||||||
@ -587,6 +534,7 @@ fn compare_drawer<'s>(
|
|||||||
Ok(DiffResult {
|
Ok(DiffResult {
|
||||||
status: this_status,
|
status: this_status,
|
||||||
name: "drawer".to_owned(),
|
name: "drawer".to_owned(),
|
||||||
|
message: None,
|
||||||
children: child_status,
|
children: child_status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use crate::parser::Source;
|
use crate::parser::Source;
|
||||||
|
|
||||||
|
use super::sexp::Token;
|
||||||
|
|
||||||
/// Check if the child string slice is a slice of the parent string slice.
|
/// Check if the child string slice is a slice of the parent string slice.
|
||||||
fn is_slice_of(parent: &str, child: &str) -> bool {
|
fn is_slice_of(parent: &str, child: &str) -> bool {
|
||||||
let parent_start = parent.as_ptr() as usize;
|
let parent_start = parent.as_ptr() as usize;
|
||||||
@ -19,3 +21,19 @@ pub fn get_offsets<'s, S: Source<'s>>(source: &'s str, rust_object: &'s S) -> (u
|
|||||||
let end = offset + rust_object_source.len();
|
let end = offset + rust_object_source.len();
|
||||||
(offset, end)
|
(offset, end)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn assert_name<'s>(emacs: &'s Token<'s>, name: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let children = emacs.as_list()?;
|
||||||
|
let first_child = children
|
||||||
|
.first()
|
||||||
|
.ok_or("Should have at least one child.")?
|
||||||
|
.as_atom()?;
|
||||||
|
if first_child != name {
|
||||||
|
Err(format!(
|
||||||
|
"Expected a {expected} cell, but found a {found} cell.",
|
||||||
|
expected = name,
|
||||||
|
found = first_child
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user