1 use std::fmt; 2 3 use syn::{Lit, NestedMeta}; 4 5 use {FromMeta, Result}; 6 7 use self::Override::*; 8 9 /// A value which can inherit a default value or have an explicit value specified. 10 /// 11 /// # Usage 12 /// This type is meant for attributes like `default` in `darling`, which can take the following forms: 13 /// 14 /// * `#[darling(default)]` 15 /// * `#[darling(default="path::to::fn")]` 16 /// 17 /// In a struct collecting input for this attribute, that would be written as: 18 /// 19 /// ```rust,ignore 20 /// # #[macro_use] 21 /// # extern crate darling; 22 /// # extern crate syn; 23 /// use darling::util::Override; 24 /// #[derive(FromField)] 25 /// #[darling(attributes(darling))] 26 /// pub struct Options { 27 /// default: Option<Override<syn::Path>>, 28 /// } 29 /// 30 /// impl Options { 31 /// fn hydrate(self) -> Option<syn::Path> { 32 /// self.default.map(|ov| ov.unwrap_or(syn::parse_path("::Default::default").unwrap())) 33 /// } 34 /// } 35 /// ``` 36 /// 37 /// The `word` format (with no associated value), would produce `Override::Inherit`, while a list 38 /// or value format would produce `Override::Explicit`. 39 #[derive(Debug, Clone, PartialEq, Eq)] 40 pub enum Override<T> { 41 /// Inherit the eventual value from an external source. 42 Inherit, 43 44 /// Explicitly set the value. 45 Explicit(T), 46 } 47 48 impl<T> Override<T> { 49 /// Converts from `Override<T>` to `Override<&T>`. 50 /// 51 /// Produces a new `Override`, containing a reference into the original, leaving the original in place. as_ref<'a>(&'a self) -> Override<&'a T>52 pub fn as_ref<'a>(&'a self) -> Override<&'a T> { 53 match *self { 54 Inherit => Inherit, 55 Explicit(ref val) => Explicit(val), 56 } 57 } 58 59 /// Converts from `Override<T>` to `Override<&mut T>`. 60 /// 61 /// Produces a new `Override`, containing a mutable reference into the original. as_mut<'a>(&'a mut self) -> Override<&'a mut T>62 pub fn as_mut<'a>(&'a mut self) -> Override<&'a mut T> { 63 match *self { 64 Inherit => Inherit, 65 Explicit(ref mut val) => Explicit(val), 66 } 67 } 68 69 /// Returns `true` if the override is an `Explicit` value. is_explicit(&self) -> bool70 pub fn is_explicit(&self) -> bool { 71 match *self { 72 Inherit => false, 73 Explicit(_) => true, 74 } 75 } 76 77 /// Converts from `Override<T>` to `Option<T>`. explicit(self) -> Option<T>78 pub fn explicit(self) -> Option<T> { 79 match self { 80 Inherit => None, 81 Explicit(val) => Some(val), 82 } 83 } 84 85 /// Unwraps an override, yielding the content of an `Explicit`. Otherwise, it returns `optb`. unwrap_or(self, optb: T) -> T86 pub fn unwrap_or(self, optb: T) -> T { 87 match self { 88 Inherit => optb, 89 Explicit(val) => val, 90 } 91 } 92 93 /// Unwraps an override, yielding the content of an `Explicit`. Otherwise, it calls `op`. unwrap_or_else<F>(self, op: F) -> T where F: FnOnce() -> T,94 pub fn unwrap_or_else<F>(self, op: F) -> T 95 where 96 F: FnOnce() -> T, 97 { 98 match self { 99 Inherit => op(), 100 Explicit(val) => val, 101 } 102 } 103 } 104 105 impl<T: Default> Override<T> { 106 /// Returns the contained value or the default value of `T`. unwrap_or_default(self) -> T107 pub fn unwrap_or_default(self) -> T { 108 self.unwrap_or_else(Default::default) 109 } 110 } 111 112 impl<T> Default for Override<T> { default() -> Self113 fn default() -> Self { 114 Inherit 115 } 116 } 117 118 impl<T> From<Option<T>> for Override<T> { from(v: Option<T>) -> Self119 fn from(v: Option<T>) -> Self { 120 match v { 121 None => Inherit, 122 Some(val) => Explicit(val), 123 } 124 } 125 } 126 127 impl<T: fmt::Display> fmt::Display for Override<T> { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result128 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 129 match *self { 130 Inherit => write!(f, "Inherit"), 131 Explicit(ref val) => write!(f, "Explicit `{}`", val), 132 } 133 } 134 } 135 136 /// Parses a `Meta`. A bare word will produce `Override::Inherit`, while 137 /// any value will be forwarded to `T::from_meta`. 138 impl<T: FromMeta> FromMeta for Override<T> { from_word() -> Result<Self>139 fn from_word() -> Result<Self> { 140 Ok(Inherit) 141 } 142 from_list(items: &[NestedMeta]) -> Result<Self>143 fn from_list(items: &[NestedMeta]) -> Result<Self> { 144 Ok(Explicit(FromMeta::from_list(items)?)) 145 } 146 from_value(lit: &Lit) -> Result<Self>147 fn from_value(lit: &Lit) -> Result<Self> { 148 Ok(Explicit(FromMeta::from_value(lit)?)) 149 } 150 } 151