Skip to main content

ChangeSet

Struct ChangeSet 

Source
pub struct ChangeSet<T, F>
where T: Copy, F: Flavor,
{ /* private fields */ }
Expand description

A recorded set of tree modifications.

You can use ChangeSet::modify to construct a new modified tree from an existing one.

Node ids and token ids are separate index spaces, so changes are recorded against an Element with ChangeSet::remove, or against an id with ChangeSet::remove_node or ChangeSet::remove_token.

§Examples

use syntree::edit::ChangeSet;

let tree = syntree::tree! {
    "root" => {
        "child" => {
            ("lit", 1),
            ("lit", 2),
        },
        ("whitespace", 3),
    }
};

let child = tree.first().and_then(|n| n.first()).ok_or("missing child")?;

let mut change_set = ChangeSet::new();
change_set.remove(child);

assert_eq!(
    change_set.modify(&tree)?,
    syntree::tree! {
        "root" => {
            ("whitespace", 3)
        }
    }
);

let lit = child.first().ok_or("missing lit")?;

let mut change_set = ChangeSet::new();
change_set.remove(lit);

assert_eq!(
    change_set.modify(&tree)?,
    syntree::tree! {
        "root" => {
            "child" => {
                ("lit", 2),
            },
            ("whitespace", 3)
        }
    }
);

Implementations§

Source§

impl<T, F> ChangeSet<T, F>
where T: Copy, F: Flavor,

Source

pub fn new() -> Self

Construct a new empty ChangeSet.

Source§

impl<T, F> ChangeSet<T, F>
where T: Copy, F: Flavor,

Source

pub fn remove(&mut self, element: Element<'_, T, F>)

Register the removal of a node or token in the changeset. Removing a node removes everything inside of it. Only one kind of modification for a given element will be preserved.

§Examples
use syntree::edit::ChangeSet;

let tree = syntree::tree! {
    "root" => {
        "child" => {
            ("lit", 1),
            ("lit", 2),
        },
        ("whitespace", 3),
    }
};

let child = tree.first().and_then(|n| n.first()).ok_or("missing child")?;

let mut change_set = ChangeSet::new();
change_set.remove(child);

assert_eq!(
    change_set.modify(&tree)?,
    syntree::tree! {
        "root" => {
            ("whitespace", 3)
        }
    }
);
Source

pub fn remove_node(&mut self, id: F::Pointer)

Register the removal of the interior node with the given id, and everything inside of it.

§Examples
use syntree::edit::ChangeSet;

let mut tree = syntree::Builder::new();

tree.open("root")?;
let child = tree.open("child")?;
tree.token("lit", 1)?;
tree.close()?;
tree.token("whitespace", 3)?;
tree.close()?;

let tree = tree.build()?;

let mut change_set = ChangeSet::new();
change_set.remove_node(child);

assert_eq!(
    change_set.modify(&tree)?,
    syntree::tree! {
        "root" => {
            ("whitespace", 3)
        }
    }
);
Source

pub fn remove_token(&mut self, id: F::Pointer)

Register the removal of the token with the given id.

§Examples
use syntree::edit::ChangeSet;

let mut tree = syntree::Builder::new();

tree.open("root")?;
tree.token("lit", 1)?;
let ws = tree.token("whitespace", 3)?;
tree.close()?;

let tree = tree.build()?;

let mut change_set = ChangeSet::new();
change_set.remove_token(ws);

assert_eq!(
    change_set.modify(&tree)?,
    syntree::tree! {
        "root" => {
            ("lit", 1)
        }
    }
);
Source

pub fn modify( &mut self, tree: &Tree<T, F>, ) -> Result<Tree<T, F>, Error<F::Error>>

Construct a modified tree where the recorded modifications have been applied.

Spans are compacted: the tokens which remain are laid out back-to-back from offset zero, so removed tokens and gaps left by explicit spans do not leave holes. Interior nodes which are left without children are kept as empty nodes.

§Errors

Errors with Error::Overflow in case we run out of node identifiers.

§Examples
use syntree::edit::ChangeSet;

let tree = syntree::tree! {
    "root" => {
        "child" => {
            ("lit", 1),
            ("lit", 2),
        },
        ("whitespace", 3),
    }
};

let child = tree.first().and_then(|n| n.first()).ok_or("missing child")?;
let mut change_set = ChangeSet::new();
change_set.remove(child);

assert_eq!(
    change_set.modify(&tree)?,
    syntree::tree! {
        "root" => {
            ("whitespace", 3)
        }
    }
);

Trait Implementations§

Source§

impl<T, F> Default for ChangeSet<T, F>
where T: Copy, F: Flavor,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, F> Forget for ChangeSet<T, F>

§

impl<T, F> Freeze for ChangeSet<T, F>
where *const u8 is !null: Freeze, PhantomData<(<F as Flavor>::Pointer, Change)>: Freeze, PhantomData<T>: Freeze,

§

impl<T, F> Move for ChangeSet<T, F>

§

impl<T, F> RefUnwindSafe for ChangeSet<T, F>

§

impl<T, F> Send for ChangeSet<T, F>
where T: Send, RawTable<(<F as Flavor>::Pointer, Change)>: Send,

§

impl<T, F> Sync for ChangeSet<T, F>
where T: Sync, RawTable<(<F as Flavor>::Pointer, Change)>: Sync,

§

impl<T, F> Unpin for ChangeSet<T, F>
where T: Unpin, *const u8 is !null: Unpin, <F as Flavor>::Pointer: Unpin,

§

impl<T, F> UnsafeUnpin for ChangeSet<T, F>

§

impl<T, F> UnwindSafe for ChangeSet<T, F>
where HashMap<<F as Flavor>::Pointer, Change>: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.