use super::{Format, FormattedValueError, GenericFormattedValue};
use super::{FormattedValue, SpecificFormattedValue};
pub trait CompatibleFormattedValue<V: FormattedValue> {
type Original: FormattedValue;
fn try_into_checked(self, other: V) -> Result<Self::Original, FormattedValueError>;
fn try_into_checked_explicit(
self,
format: Format,
) -> Result<Self::Original, FormattedValueError>;
}
impl<T, V> CompatibleFormattedValue<V> for T
where
V: SpecificFormattedValue,
T: SpecificFormattedValue<FullRange = V::FullRange>,
{
type Original = Self;
fn try_into_checked(self, _other: V) -> Result<Self, FormattedValueError> {
skip_assert_initialized!();
Ok(self)
}
fn try_into_checked_explicit(
self,
_format: Format,
) -> Result<Self::Original, FormattedValueError> {
skip_assert_initialized!();
Ok(self)
}
}
impl<T: SpecificFormattedValue> CompatibleFormattedValue<GenericFormattedValue> for T {
type Original = Self;
fn try_into_checked(self, other: GenericFormattedValue) -> Result<Self, FormattedValueError> {
skip_assert_initialized!();
if self.format() == other.format() {
Ok(self)
} else {
Err(FormattedValueError(self.format()))
}
}
fn try_into_checked_explicit(
self,
format: Format,
) -> Result<Self::Original, FormattedValueError> {
skip_assert_initialized!();
if self.format() == format {
Ok(self)
} else {
Err(FormattedValueError(self.format()))
}
}
}
impl<V: SpecificFormattedValue> CompatibleFormattedValue<V> for GenericFormattedValue {
type Original = Self;
fn try_into_checked(self, _other: V) -> Result<Self, FormattedValueError> {
skip_assert_initialized!();
if self.format() == V::default_format() {
Ok(self)
} else {
Err(FormattedValueError(self.format()))
}
}
fn try_into_checked_explicit(
self,
_format: Format,
) -> Result<Self::Original, FormattedValueError> {
skip_assert_initialized!();
if self.format() == V::default_format() {
Ok(self)
} else {
Err(FormattedValueError(self.format()))
}
}
}