From 8ea857b55d6bc0b5f825f526b92ef91a1e3bfc9b Mon Sep 17 00:00:00 2001 From: Nicolas BACQUEY Date: Mon, 2 Dec 2024 10:37:46 +0100 Subject: [PATCH] address comments and typos --- topiary-core/src/comments.rs | 35 ++++++++++++++++++++++------------- topiary-core/src/types.rs | 8 +++++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/topiary-core/src/comments.rs b/topiary-core/src/comments.rs index 6326cd7d..70a1b7fb 100644 --- a/topiary-core/src/comments.rs +++ b/topiary-core/src/comments.rs @@ -11,8 +11,10 @@ use crate::{ /// When you remove a block of text from the input, it changes the positions of every subsequent character. /// This is what this Diff instance does. -impl Diff for Position { - fn substract(self: &mut Self, other: &InputSection) -> FormatterResult<()> { +impl Diff for Position { + type ErrorType = FormatterError; + + fn subtract(self: &mut Self, other: &InputSection) -> FormatterResult<()> { if *self <= other.start { // The point is before the removed block: nothing happens. Ok(()) @@ -47,15 +49,17 @@ impl Diff for Position { } else { // The point is within the removed block: fail, as the point will not exist // in the new version of the input. - Err(FormatterError::Internal("Tried to substract a section from a point it contains".into(), None)) + Err(FormatterError::Internal("Tried to subtract a section from a point it contains".into(), None)) } } } -impl Diff for InputSection { - fn substract(self: &mut Self, other: &Self) -> FormatterResult<()> { - self.start.substract(other)?; - self.end.substract(other) +impl Diff for InputSection { + type ErrorType = FormatterError; + + fn subtract(self: &mut Self, other: &Self) -> FormatterResult<()> { + self.start.subtract(other)?; + self.end.subtract(other) } } @@ -79,7 +83,7 @@ fn find_comments<'a>(node: Node<'a>, input: &str, comments: & mut Vec for Commented { - fn substract(self: &mut Self, other: &InputSection) -> FormatterResult<()> { +impl Diff for Commented { + type ErrorType = FormatterError; + + fn subtract(self: &mut Self, other: &InputSection) -> FormatterResult<()> { match self { Commented::CommentedBefore(section) => { - section.substract(other) + section.subtract(other) }, Commented::CommentedAfter(section) => { - section.substract(other) + section.subtract(other) }, } } @@ -234,6 +240,9 @@ pub fn extract_comments<'a>( // 1) remove the comment from the input, // 2) register an InputEdit to modify the tree, // 3) edit the following anchors to account for the removed comment. + // + // The order is reversed so that all InputEdits can be applied in succession: + // one will not affect the others. for index in (0..anchors.len()).rev() { let AnchoredComment{comment, ..} = &anchors[index]; // 1) @@ -251,7 +260,7 @@ pub fn extract_comments<'a>( // 3) for following_index in index..anchors.len() { let AnchoredComment { commented: mut following_anchor, .. } = &anchors[following_index]; - following_anchor.substract(&comment.into()); + following_anchor.subtract(&comment.into()); } } for edit in edits { diff --git a/topiary-core/src/types.rs b/topiary-core/src/types.rs index f7e97c18..a8bb0708 100644 --- a/topiary-core/src/types.rs +++ b/topiary-core/src/types.rs @@ -47,9 +47,11 @@ impl From<&Node<'_>> for InputSection { } } -/// A generic trait to substract stuff from other stuff. The function can be partial. +/// A generic trait to subtract stuff from other stuff. The function can be partial. /// In practice, it will be used to update text positions within the input, /// when removing parts of it. -pub trait Diff { - fn substract(self: &mut Self, other: &T) -> Result<(), E>; +pub trait Diff { + type ErrorType; + + fn subtract(self: &mut Self, other: &T) -> Result<(), Self::ErrorType>; }