1use crate::{Constant, IntOrRange, PropertyKind};
2
3pub type Bool = bool;
4pub type Int = u32;
5pub type Double = f64;
6
7#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum ListOp {
10 Times,
11 Divide,
12 Or,
13 And,
14 Plus,
15 Minus,
16}
17
18parse_enum! {
19 ListOp,
20 (Times, "times"),
21 (Divide, "divide"),
22 (Or, "or"),
23 (And, "and"),
24 (Plus, "plus"),
25 (Minus, "minus"),
26}
27
28#[derive(Copy, Clone, Debug, PartialEq, Eq)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
30pub enum UnaryOp {
31 Not,
32
33 // these operator not exists in document page but exists in dtd
34 Cecil,
35 Floor,
36 Round,
37 Trunc,
38}
39
40parse_enum! {
41 UnaryOp,
42 (Not, "not"),
43 (Cecil, "cecil"),
44 (Floor, "floor"),
45 (Round, "round"),
46 (Trunc, "trunc"),
47}
48
49#[derive(Copy, Clone, Debug, PartialEq, Eq)]
50#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
51pub enum BinaryOp {
52 Eq,
53 NotEq,
54 Less,
55 LessEq,
56 More,
57 MoreEq,
58 Contains,
59 NotContains,
60}
61
62parse_enum! {
63 BinaryOp,
64 (Eq, "eq"),
65 (NotEq, "not_eq"),
66 (Less, "less"),
67 (LessEq, "less_eq"),
68 (More, "more"),
69 (MoreEq, "more_eq"),
70 (Contains, "contains"),
71 (NotContains, "not_contains"),
72}
73
74#[derive(Copy, Clone, Debug, PartialEq, Eq)]
75#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
76pub enum TernaryOp {
77 If,
78}
79
80parse_enum! {
81 TernaryOp,
82 (If, "if"),
83}
84
85#[derive(Clone, Debug, PartialEq)]
86#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
87pub enum Expression {
88 Simple(Value),
89 Unary(UnaryOp, Box<Self>),
90 Binary(BinaryOp, Box<[Self; 2]>),
91 Ternary(TernaryOp, Box<[Self; 3]>),
92 List(ListOp, Vec<Self>),
93 Matrix(Box<[Self; 4]>),
94}
95
96#[derive(Clone, Copy, Debug, PartialEq, Eq)]
97#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
98pub enum PropertyTarget {
99 Default,
100 Font,
101 Pattern,
102}
103
104parse_enum! {
105 PropertyTarget,
106 (Default, "default"),
107 (Font, "font"),
108 (Pattern, "pattern"),
109}
110
111impl Default for PropertyTarget {
112 fn default() -> Self {
113 PropertyTarget::Default
114 }
115}
116
117pub type CharSet = Vec<IntOrRange>;
118
119/// Runtime typed fontconfig value
120#[derive(Clone, Debug, PartialEq)]
121#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
122pub enum Value {
123 /// `<int>0</int>`
124 Int(Int),
125 /// `<double>1.5</double>`
126 Double(Double),
127 /// `<string>str</string>`
128 String(String),
129 /// `<const>hintslight</const>`
130 Constant(Constant),
131 /// `<bool>false</bool>`
132 Bool(Bool),
133 /// This element holds the two [`Value::Int`] elements of a range representation.
134 Range(Int, Int),
135 /// This element holds at least one [`Value::String`] element of a RFC-3066-style languages or more.
136 LangSet(String),
137 /// This element holds at least one [`Value::Int`] element of an Unicode code point or more.
138 CharSet(CharSet),
139 /// `<name target="font">pixelsize</name>`
140 Property(PropertyTarget, PropertyKind),
141}
142
143macro_rules! from_value {
144 ($($name:ident,)+) => {
145 $(
146 impl From<$name> for Value {
147 fn from(v: $name) -> Value {
148 Value::$name(v)
149 }
150 }
151 )+
152 };
153}
154
155from_value! {
156 Int,
157 Bool,
158 Double,
159 Constant,
160 CharSet,
161}
162
163impl<'a> From<&'a str> for Value {
164 fn from(s: &'a str) -> Self {
165 Value::String(s.into())
166 }
167}
168
169impl From<String> for Value {
170 fn from(s: String) -> Self {
171 Value::String(s)
172 }
173}
174
175impl From<(PropertyTarget, PropertyKind)> for Value {
176 fn from((target: PropertyTarget, kind: PropertyKind): (PropertyTarget, PropertyKind)) -> Self {
177 Value::Property(target, kind)
178 }
179}
180
181impl<V> From<V> for Expression
182where
183 Value: From<V>,
184{
185 fn from(v: V) -> Self {
186 Expression::Simple(Value::from(v))
187 }
188}
189