| 1 | /// The goal of tracing generic parameter usage. |
| 2 | /// |
| 3 | /// Not all uses of type parameters imply a need to add bounds to a generated trait impl. |
| 4 | /// For example, a field of type `<Vec<T> as a::b::Trait>::Associated` does not need a |
| 5 | /// `where T: Serialize` bound in `serde`. |
| 6 | /// However, a proc macro that is attempting to generate a helper struct _would_ need to |
| 7 | /// know about this usage, or else the generated code would reference an unknown type `T` |
| 8 | /// and fail to compile. |
| 9 | #[derive (Debug, Copy, Clone, PartialEq, Eq)] |
| 10 | pub enum Purpose { |
| 11 | /// The tracing is being used to generate an `impl` block. |
| 12 | /// |
| 13 | /// Uses such as `syn::TypePath.qself` will _not_ be returned. |
| 14 | BoundImpl, |
| 15 | /// The tracing is being used to generate a new struct or enum. |
| 16 | /// |
| 17 | /// All uses will be returned. |
| 18 | Declare, |
| 19 | } |
| 20 | |
| 21 | /// Control struct for searching type parameters. |
| 22 | /// |
| 23 | /// This acts as the search context, preserving information that might have been |
| 24 | /// kept on a visitor in a different implementation. |
| 25 | /// Trait implementers are required to pass this through on any invocations they make. |
| 26 | /// |
| 27 | /// # Usage |
| 28 | /// For extensibility, `Options` hides all of its fields from consumers. |
| 29 | /// To create an instance, use the `From<Purpose>` trait implementation: |
| 30 | /// |
| 31 | /// ```rust |
| 32 | /// # use darling_core::usage::{Options, Purpose}; |
| 33 | /// let opts: Options = Purpose::BoundImpl.into(); |
| 34 | /// assert!(!opts.include_type_path_qself()); |
| 35 | /// ``` |
| 36 | #[derive (Debug, Clone)] |
| 37 | pub struct Options { |
| 38 | purpose: Purpose, |
| 39 | #[doc (hidden)] |
| 40 | __nonexhaustive: (), |
| 41 | } |
| 42 | |
| 43 | impl From<Purpose> for Options { |
| 44 | fn from(purpose: Purpose) -> Self { |
| 45 | Self { |
| 46 | purpose, |
| 47 | __nonexhaustive: (), |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl Options { |
| 53 | /// Returns `true` if the implementer of `UseTypeParams` should search |
| 54 | /// `<___ as ...>::...` when looking for type parameter uses. |
| 55 | pub fn include_type_path_qself(&self) -> bool { |
| 56 | self.purpose == Purpose::Declare |
| 57 | } |
| 58 | } |
| 59 | |