|
|
// 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
|
|
|
}
|
|
|
} |