|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
|
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
4 | 4 |
|
| 5 | +use std::str; |
5 | 6 | use std::fmt;
|
6 | 7 | use std::slice;
|
7 | 8 | use std::vec;
|
| 9 | +use serialize::{json}; |
| 10 | +use serialize::json::ToJson; |
8 | 11 |
|
9 | 12 |
|
10 | 13 | #[deriving(PartialEq)]
|
@@ -175,3 +178,201 @@ impl Iterator<ComponentValue> for MoveSkipWhitespaceIterator {
|
175 | 178 | None
|
176 | 179 | }
|
177 | 180 | }
|
| 181 | + |
| 182 | +/* ToJson implementations for all the AST nodes |
| 183 | + * |
| 184 | + * These need to be implemented here because they can't be implemented in a |
| 185 | + * crate that doesn't define these nodes |
| 186 | + */ |
| 187 | + |
| 188 | +macro_rules! JString { |
| 189 | + ($e: expr) => { json::String($e.to_string()) } |
| 190 | +} |
| 191 | + |
| 192 | +macro_rules! JList { |
| 193 | + ($($e: expr),*) => { json::List(vec!( $($e),* )) } |
| 194 | +} |
| 195 | + |
| 196 | +fn list_to_json(list: &Vec<(ComponentValue, SourceLocation)>) -> Vec<json::Json> { |
| 197 | + list.iter().map(|tuple| { |
| 198 | + match *tuple { |
| 199 | + (ref c, _) => c.to_json() |
| 200 | + } |
| 201 | + }).collect() |
| 202 | +} |
| 203 | + |
| 204 | +impl ToJson for Result<Rule, SyntaxError> { |
| 205 | + fn to_json(&self) -> json::Json { |
| 206 | + match *self { |
| 207 | + Ok(ref a) => a.to_json(), |
| 208 | + Err(ref b) => b.to_json(), |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | + |
| 214 | +impl ToJson for Result<DeclarationListItem, SyntaxError> { |
| 215 | + fn to_json(&self) -> json::Json { |
| 216 | + match *self { |
| 217 | + Ok(ref a) => a.to_json(), |
| 218 | + Err(ref b) => b.to_json(), |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | + |
| 224 | +impl ToJson for Result<Declaration, SyntaxError> { |
| 225 | + fn to_json(&self) -> json::Json { |
| 226 | + match *self { |
| 227 | + Ok(ref a) => a.to_json(), |
| 228 | + Err(ref b) => b.to_json(), |
| 229 | + } |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | + |
| 234 | +impl ToJson for Result<ComponentValue, SyntaxError> { |
| 235 | + fn to_json(&self) -> json::Json { |
| 236 | + match *self { |
| 237 | + Ok(ref a) => a.to_json(), |
| 238 | + Err(ref b) => b.to_json(), |
| 239 | + } |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | + |
| 244 | +impl ToJson for SyntaxError { |
| 245 | + fn to_json(&self) -> json::Json { |
| 246 | + json::List(vec!(JString!("error"), JString!(match self.reason { |
| 247 | + ErrEmptyInput => "empty", |
| 248 | + ErrExtraInput => "extra-input", |
| 249 | + _ => "invalid", |
| 250 | + }))) |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | + |
| 255 | +impl ToJson for super::Color { |
| 256 | + fn to_json(&self) -> json::Json { |
| 257 | + use super::{RGBA, CurrentColor}; |
| 258 | + |
| 259 | + match *self { |
| 260 | + RGBA(RGBA { red: r, green: g, blue: b, alpha: a }) => vec!(r, g, b, a).to_json(), |
| 261 | + CurrentColor => JString!("currentColor"), |
| 262 | + } |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | + |
| 267 | +impl ToJson for Rule { |
| 268 | + fn to_json(&self) -> json::Json { |
| 269 | + match *self { |
| 270 | + QualifiedRule(ref rule) => rule.to_json(), |
| 271 | + AtRule(ref rule) => rule.to_json(), |
| 272 | + } |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | + |
| 277 | +impl ToJson for DeclarationListItem { |
| 278 | + fn to_json(&self) -> json::Json { |
| 279 | + match *self { |
| 280 | + Declaration(ref declaration) => declaration.to_json(), |
| 281 | + DeclAtRule(ref at_rule) => at_rule.to_json(), |
| 282 | + } |
| 283 | + } |
| 284 | +} |
| 285 | + |
| 286 | +impl ToJson for ComponentValue { |
| 287 | + fn to_json(&self) -> json::Json { |
| 288 | + fn numeric(value: &NumericValue) -> Vec<json::Json> { |
| 289 | + match *value { |
| 290 | + NumericValue{representation: ref r, value: ref v, int_value: ref i} |
| 291 | + => vec!(r.to_json(), v.to_json(), |
| 292 | + JString!(match *i { Some(_) => "integer", _ => "number" })) |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + match *self { |
| 297 | + Ident(ref value) => JList!(JString!("ident"), value.to_json()), |
| 298 | + AtKeyword(ref value) => JList!(JString!("at-keyword"), value.to_json()), |
| 299 | + Hash(ref value) => JList!(JString!("hash"), value.to_json(), |
| 300 | + JString!("unrestricted")), |
| 301 | + IDHash(ref value) => JList!(JString!("hash"), value.to_json(), JString!("id")), |
| 302 | + String(ref value) => JList!(JString!("string"), value.to_json()), |
| 303 | + URL(ref value) => JList!(JString!("url"), value.to_json()), |
| 304 | + Delim('\\') => JString!("\\"), |
| 305 | + Delim(value) => json::String(str::from_char(value)), |
| 306 | + |
| 307 | + Number(ref value) => json::List(vec!(JString!("number")) + numeric(value)), |
| 308 | + Percentage(ref value) => json::List(vec!(JString!("percentage")) + numeric(value)), |
| 309 | + Dimension(ref value, ref unit) |
| 310 | + => json::List(vec!(JString!("dimension")) + numeric(value) + &[unit.to_json()]), |
| 311 | + |
| 312 | + UnicodeRange(start, end) |
| 313 | + => JList!(JString!("unicode-range"), start.to_json(), end.to_json()), |
| 314 | + |
| 315 | + WhiteSpace => JString!(" "), |
| 316 | + Colon => JString!(":"), |
| 317 | + Semicolon => JString!(";"), |
| 318 | + Comma => JString!(","), |
| 319 | + IncludeMatch => JString!("~="), |
| 320 | + DashMatch => JString!("|="), |
| 321 | + PrefixMatch => JString!("^="), |
| 322 | + SuffixMatch => JString!("$="), |
| 323 | + SubstringMatch => JString!("*="), |
| 324 | + Column => JString!("||"), |
| 325 | + CDO => JString!("<!--"), |
| 326 | + CDC => JString!("-->"), |
| 327 | + |
| 328 | + Function(ref name, ref arguments) |
| 329 | + => json::List(vec!(JString!("function"), name.to_json()) + |
| 330 | + arguments.iter().map(|a| a.to_json()).collect::<Vec<json::Json>>()), |
| 331 | + ParenthesisBlock(ref content) |
| 332 | + => json::List(vec!(JString!("()")) + content.iter().map(|c| c.to_json()).collect::<Vec<json::Json>>()), |
| 333 | + SquareBracketBlock(ref content) |
| 334 | + => json::List(vec!(JString!("[]")) + content.iter().map(|c| c.to_json()).collect::<Vec<json::Json>>()), |
| 335 | + CurlyBracketBlock(ref content) |
| 336 | + => json::List(vec!(JString!("{}")) + list_to_json(content)), |
| 337 | + |
| 338 | + BadURL => JList!(JString!("error"), JString!("bad-url")), |
| 339 | + BadString => JList!(JString!("error"), JString!("bad-string")), |
| 340 | + CloseParenthesis => JList!(JString!("error"), JString!(")")), |
| 341 | + CloseSquareBracket => JList!(JString!("error"), JString!("]")), |
| 342 | + CloseCurlyBracket => JList!(JString!("error"), JString!("}")), |
| 343 | + } |
| 344 | + } |
| 345 | +} |
| 346 | + |
| 347 | +impl ToJson for AtRule { |
| 348 | + fn to_json(&self) -> json::Json { |
| 349 | + match *self { |
| 350 | + AtRule{name: ref name, prelude: ref prelude, block: ref block, ..} |
| 351 | + => json::List(vec!(JString!("at-rule"), name.to_json(), |
| 352 | + prelude.to_json(), block.as_ref().map(list_to_json).to_json())) |
| 353 | + } |
| 354 | + } |
| 355 | +} |
| 356 | + |
| 357 | + |
| 358 | +impl ToJson for QualifiedRule { |
| 359 | + fn to_json(&self) -> json::Json { |
| 360 | + match *self { |
| 361 | + QualifiedRule{prelude: ref prelude, block: ref block, ..} |
| 362 | + => json::List(vec!(JString!("qualified rule"), |
| 363 | + prelude.to_json(), json::List(list_to_json(block)))) |
| 364 | + } |
| 365 | + } |
| 366 | +} |
| 367 | + |
| 368 | + |
| 369 | +impl ToJson for Declaration { |
| 370 | + fn to_json(&self) -> json::Json { |
| 371 | + match *self { |
| 372 | + Declaration{name: ref name, value: ref value, important: ref important, ..} |
| 373 | + => json::List(vec!(JString!("declaration"), name.to_json(), |
| 374 | + value.to_json(), important.to_json())) |
| 375 | + } |
| 376 | + } |
| 377 | +} |
| 378 | + |
0 commit comments