specified_value_info.rs (5485B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 //! Value information for devtools. 6 7 use crate::arc_slice::ArcSlice; 8 use crate::owned_slice::OwnedSlice; 9 use servo_arc::Arc; 10 use std::ops::Range; 11 use std::sync::Arc as StdArc; 12 use thin_vec::ThinVec; 13 14 /// Type of value that a property supports. This is used by Gecko's 15 /// devtools to make sense about value it parses, and types listed 16 /// here should match InspectorPropertyType in InspectorUtils.webidl. 17 /// 18 /// XXX This should really be a bitflags rather than a namespace mod, 19 /// but currently we cannot use bitflags in const. 20 #[allow(non_snake_case)] 21 pub mod CssType { 22 /// <color> 23 pub const COLOR: u8 = 1 << 0; 24 /// <gradient> 25 pub const GRADIENT: u8 = 1 << 1; 26 /// <timing-function> 27 pub const TIMING_FUNCTION: u8 = 1 << 2; 28 } 29 30 /// See SpecifiedValueInfo::collect_completion_keywords. 31 pub type KeywordsCollectFn<'a> = &'a mut dyn FnMut(&[&'static str]); 32 33 /// Information of values of a given specified value type. 34 /// 35 /// This trait is derivable with `#[derive(SpecifiedValueInfo)]`. 36 /// 37 /// The algorithm traverses the type definition. For `SUPPORTED_TYPES`, 38 /// it puts an or'ed value of `SUPPORTED_TYPES` of all types it finds. 39 /// For `collect_completion_keywords`, it recursively invokes this 40 /// method on types found, and lists all keyword values and function 41 /// names following the same rule as `ToCss` in that method. 42 /// 43 /// Some attributes of `ToCss` can affect the behavior, specifically: 44 /// * If `#[css(function)]` is found, the content inside the annotated 45 /// variant (or the whole type) isn't traversed, only the function 46 /// name is listed in `collect_completion_keywords`. 47 /// * If `#[css(skip)]` is found, the content inside the variant or 48 /// field is ignored. 49 /// * Values listed in `#[css(if_empty)]`, `#[parse(aliases)]`, and 50 /// `#[css(keyword)]` are added into `collect_completion_keywords`. 51 /// 52 /// In addition to `css` attributes, it also has `value_info` helper 53 /// attributes, including: 54 /// * `#[value_info(ty = "TYPE")]` can be used to specify a constant 55 /// from `CssType` to `SUPPORTED_TYPES`. 56 /// * `#[value_info(other_values = "value1,value2")]` can be used to 57 /// add other values related to a field, variant, or the type itself 58 /// into `collect_completion_keywords`. 59 /// * `#[value_info(starts_with_keyword)]` can be used on variants to 60 /// add the name of a non-unit variant (serialized like `ToCss`) into 61 /// `collect_completion_keywords`. 62 pub trait SpecifiedValueInfo { 63 /// Supported CssTypes by the given value type. 64 /// 65 /// XXX This should be typed CssType when that becomes a bitflags. 66 /// Currently we cannot do so since bitflags cannot be used in constant. 67 const SUPPORTED_TYPES: u8 = 0; 68 69 /// Collect value starting words for the given specified value type. 70 /// This includes keyword and function names which can appear at the 71 /// beginning of a value of this type. 72 /// 73 /// Caller should pass in a callback function to accept the list of 74 /// values. The callback function can be called multiple times, and 75 /// some values passed to the callback may be duplicate. 76 fn collect_completion_keywords(_f: KeywordsCollectFn) {} 77 } 78 79 impl SpecifiedValueInfo for bool {} 80 impl SpecifiedValueInfo for f32 {} 81 impl SpecifiedValueInfo for i8 {} 82 impl SpecifiedValueInfo for i32 {} 83 impl SpecifiedValueInfo for u8 {} 84 impl SpecifiedValueInfo for u16 {} 85 impl SpecifiedValueInfo for u32 {} 86 impl SpecifiedValueInfo for usize {} 87 impl SpecifiedValueInfo for str {} 88 impl SpecifiedValueInfo for String {} 89 impl SpecifiedValueInfo for crate::owned_str::OwnedStr {} 90 91 #[cfg(feature = "servo")] 92 impl SpecifiedValueInfo for ::stylo_atoms::Atom {} 93 #[cfg(feature = "servo")] 94 impl SpecifiedValueInfo for ::url::Url {} 95 96 impl<T: SpecifiedValueInfo + ?Sized> SpecifiedValueInfo for Box<T> { 97 const SUPPORTED_TYPES: u8 = T::SUPPORTED_TYPES; 98 fn collect_completion_keywords(f: KeywordsCollectFn) { 99 T::collect_completion_keywords(f); 100 } 101 } 102 103 impl<T: SpecifiedValueInfo> SpecifiedValueInfo for [T] { 104 const SUPPORTED_TYPES: u8 = T::SUPPORTED_TYPES; 105 fn collect_completion_keywords(f: KeywordsCollectFn) { 106 T::collect_completion_keywords(f); 107 } 108 } 109 110 macro_rules! impl_generic_specified_value_info { 111 ($ty:ident<$param:ident>) => { 112 impl<$param: SpecifiedValueInfo> SpecifiedValueInfo for $ty<$param> { 113 const SUPPORTED_TYPES: u8 = $param::SUPPORTED_TYPES; 114 fn collect_completion_keywords(f: KeywordsCollectFn) { 115 $param::collect_completion_keywords(f); 116 } 117 } 118 }; 119 } 120 impl_generic_specified_value_info!(Option<T>); 121 impl_generic_specified_value_info!(OwnedSlice<T>); 122 impl_generic_specified_value_info!(Vec<T>); 123 impl_generic_specified_value_info!(ThinVec<T>); 124 impl_generic_specified_value_info!(Arc<T>); 125 impl_generic_specified_value_info!(StdArc<T>); 126 impl_generic_specified_value_info!(ArcSlice<T>); 127 impl_generic_specified_value_info!(Range<Idx>); 128 129 impl<T1, T2> SpecifiedValueInfo for (T1, T2) 130 where 131 T1: SpecifiedValueInfo, 132 T2: SpecifiedValueInfo, 133 { 134 const SUPPORTED_TYPES: u8 = T1::SUPPORTED_TYPES | T2::SUPPORTED_TYPES; 135 136 fn collect_completion_keywords(f: KeywordsCollectFn) { 137 T1::collect_completion_keywords(f); 138 T2::collect_completion_keywords(f); 139 } 140 }