use crate::core::visitor::{Visit, VisitResult, Visitor};
use std::ops::Deref;
#[derive(Debug)]
pub struct TemplateVariable<T> {
value: T,
custom: bool,
}
impl<T: Clone> Clone for TemplateVariable<T> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
custom: self.custom,
}
}
}
impl<T: PartialEq> PartialEq for TemplateVariable<T> {
fn eq(&self, other: &Self) -> bool {
self.value.eq(&other.value)
}
}
impl<T: Eq> Eq for TemplateVariable<T> {}
impl<T: Copy> Copy for TemplateVariable<T> {}
impl<T: Default> Default for TemplateVariable<T> {
fn default() -> Self {
Self {
value: T::default(),
custom: false,
}
}
}
impl<T: Clone> TemplateVariable<T> {
pub fn clone_inner(&self) -> T {
self.value.clone()
}
}
impl<T> TemplateVariable<T> {
pub fn new(value: T) -> Self {
Self {
value,
custom: false,
}
}
pub fn new_custom(value: T) -> Self {
Self {
value,
custom: true,
}
}
pub fn set(&mut self, value: T) -> T {
self.custom = true;
std::mem::replace(&mut self.value, value)
}
pub fn get(&self) -> &T {
&self.value
}
pub fn is_custom(&self) -> bool {
self.custom
}
}
impl<T> Deref for TemplateVariable<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T> Visit for TemplateVariable<T>
where
T: Visit,
{
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
visitor.enter_region(name)?;
self.value.visit("Value", visitor)?;
self.custom.visit("IsCustom", visitor)?;
visitor.leave_region()
}
}