use super::*; /// A value that can be automatically determined. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum Smart { /// The value should be determined smartly based on the circumstances. Auto, /// A specific value. Custom(T), } impl Smart { /// Whether the value is `Auto`. pub fn is_auto(&self) -> bool { matches!(self, Self::Auto) } /// Whether this holds a custom value. pub fn is_custom(&self) -> bool { matches!(self, Self::Custom(_)) } /// Map the contained custom value with `f`. pub fn map(self, f: F) -> Smart where F: FnOnce(T) -> U, { match self { Self::Auto => Smart::Auto, Self::Custom(x) => Smart::Custom(f(x)), } } /// Keeps `self` if it contains a custom value, otherwise returns `other`. pub fn or(self, other: Smart) -> Self { match self { Self::Custom(x) => Self::Custom(x), Self::Auto => other, } } /// Returns the contained custom value or a provided default value. pub fn unwrap_or(self, default: T) -> T { match self { Self::Auto => default, Self::Custom(x) => x, } } /// Returns the contained custom value or computes a default value. pub fn unwrap_or_else(self, f: F) -> T where F: FnOnce() -> T, { match self { Self::Auto => f(), Self::Custom(x) => x, } } /// Returns the contained custom value or the default value. pub fn unwrap_or_default(self) -> T where T: Default, { self.unwrap_or_else(T::default) } } impl Default for Smart { fn default() -> Self { Self::Auto } }