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