style_traits/specified_value_info.rs
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
7use crate::arc_slice::ArcSlice;
8use crate::owned_slice::OwnedSlice;
9use servo_arc::Arc;
10use std::ops::Range;
11use std::sync::Arc as StdArc;
12use 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)]
21pub 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.
31pub 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`.
62pub 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
79impl SpecifiedValueInfo for bool {}
80impl SpecifiedValueInfo for f32 {}
81impl SpecifiedValueInfo for i8 {}
82impl SpecifiedValueInfo for i32 {}
83impl SpecifiedValueInfo for u8 {}
84impl SpecifiedValueInfo for u16 {}
85impl SpecifiedValueInfo for u32 {}
86impl SpecifiedValueInfo for usize {}
87impl SpecifiedValueInfo for str {}
88impl SpecifiedValueInfo for String {}
89impl SpecifiedValueInfo for crate::owned_str::OwnedStr {}
90
91#[cfg(feature = "servo")]
92impl SpecifiedValueInfo for ::stylo_atoms::Atom {}
93#[cfg(feature = "servo")]
94impl SpecifiedValueInfo for ::url::Url {}
95
96impl<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
103impl<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
110macro_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}
120impl_generic_specified_value_info!(Option<T>);
121impl_generic_specified_value_info!(OwnedSlice<T>);
122impl_generic_specified_value_info!(Vec<T>);
123impl_generic_specified_value_info!(ThinVec<T>);
124impl_generic_specified_value_info!(Arc<T>);
125impl_generic_specified_value_info!(StdArc<T>);
126impl_generic_specified_value_info!(ArcSlice<T>);
127impl_generic_specified_value_info!(Range<Idx>);
128
129impl<T1, T2> SpecifiedValueInfo for (T1, T2)
130where
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}