GitLab

+2 −42
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ mod arena;
mod children;
mod error;
mod iterator;
mod parent;
pub mod parse;
mod siblings;
#[cfg(test)]
@@ -33,6 +34,7 @@ use self::{
    children::Children,
    error::{TokenError, TokenResult},
    iterator::TokenIterator,
    parent::Parent,
    siblings::Siblings,
};

@@ -379,48 +381,6 @@ impl Display for Value {
    }
}

#[derive(Debug, Default)]
pub struct Parent {
    parent: Option<TokenId>,
}

impl Parent {
    fn set(&mut self, parent_id: TokenId) {
        self.parent = Some(parent_id);
    }

    fn delete(
        &mut self,
        child_id: TokenId,
        previous_id: Option<TokenId>,
        next_id: Option<TokenId>,
    ) {
        if let Some(parent_id) = self.parent {
            arena().mutate(parent_id, &|mut parent| {
                parent.delete_child(child_id, previous_id, next_id);
            });
        }
    }

    fn reinstate(
        &mut self,
        child_id: TokenId,
        previous_id: Option<TokenId>,
        next_id: Option<TokenId>,
    ) {
        if let Some(parent_id) = self.parent {
            arena().mutate(parent_id, &|mut parent| {
                parent.reinstate_child(child_id, previous_id, next_id);
            });
        }
    }

    fn replace(&mut self, _replacement: TokenId) {
        // TODO: If parent.first_child or parent.last_child is self,
        //       replace with replacement
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TokenType {
    Null,
+60 −0
Original line number Diff line number Diff line
// Copyright © 2018 Phil Booth
//
// This file is part of pbvi.
//
// pbvi is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// pbvi is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// pbvi. If not, see <https://www.gnu.org/licenses/>.

use super::{arena, TokenId};

#[derive(Debug, Default)]
pub struct Parent {
    parent: Option<TokenId>,
}

impl Parent {
    pub fn set(&mut self, parent_id: TokenId) {
        self.parent = Some(parent_id);
    }

    pub fn delete(
        &mut self,
        child_id: TokenId,
        previous_id: Option<TokenId>,
        next_id: Option<TokenId>,
    ) {
        if let Some(parent_id) = self.parent {
            arena().mutate(parent_id, &|mut parent| {
                parent.delete_child(child_id, previous_id, next_id);
            });
        }
    }

    pub fn reinstate(
        &mut self,
        child_id: TokenId,
        previous_id: Option<TokenId>,
        next_id: Option<TokenId>,
    ) {
        if let Some(parent_id) = self.parent {
            arena().mutate(parent_id, &|mut parent| {
                parent.reinstate_child(child_id, previous_id, next_id);
            });
        }
    }

    pub fn replace(&mut self, _replacement: TokenId) {
        // TODO: If parent.first_child or parent.last_child is self,
        //       replace with replacement
    }
}

Read the original on gitlab.com ↗