use std::fmt::Debug;
use std::{cell::Cell, default::Default, str::FromStr};
pub trait Setting {
type Item: Copy;
fn set(&self, value: Self::Item);
fn set_string(&self, value: &str);
fn get(&self) -> Self::Item;
fn is_valid(&self, _value: &str) -> bool {
true
}
}
#[derive(Default)]
pub struct CellSetting<T>(Cell<T>);
impl<T: Copy + FromStr> Setting for CellSetting<T>
where
<T as FromStr>::Err: Debug,
{
type Item = T;
fn set(&self, value: Self::Item) {
self.0.set(value);
}
fn get(&self) -> Self::Item {
self.0.get()
}
fn set_string(&self, value: &str) {
let v = T::from_str(value).unwrap();
self.0.set(v);
}
}