use core::fmt;
use core::hash::Hash;
use super::LexicalItem;
use crate::Position;
pub trait Item {
type Id: Eq + Hash + Clone;
fn id(&self) -> &Self::Id;
fn position(&self) -> &str;
fn set_position(&mut self, position: &Position);
}
impl<T: Item> LexicalItem<T> {
pub(super) fn new(mut value: T, position: Position) -> Self {
value.set_position(&position);
Self {
value,
position,
previous: None,
next: None,
}
}
pub(super) fn reposition(&mut self, position: Position) {
self.value.set_position(&position);
self.position = position;
}
pub fn id(&self) -> &T::Id {
self.value.id()
}
pub fn value(&self) -> &T {
&self.value
}
pub fn position(&self) -> &Position {
&self.position
}
pub fn previous(&self) -> Option<&T::Id> {
self.previous.as_ref()
}
pub fn next(&self) -> Option<&T::Id> {
self.next.as_ref()
}
}
impl<T: Item + fmt::Debug> fmt::Debug for LexicalItem<T>
where
T::Id: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LexicalItem")
.field("value", &self.value)
.field("position", &self.position)
.field("previous", &self.previous)
.field("next", &self.next)
.finish()
}
}
impl<T: Item + Clone> Clone for LexicalItem<T> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
position: self.position,
previous: self.previous.clone(),
next: self.next.clone(),
}
}
}