(replacement: &Yaml)
| 363 | |
| 364 | impl Replacement { |
| 365 | fn build(replacement: &Yaml) -> Result<Replacement> { |
| 366 | // Replacement -- single key/value (see below for allowed values) |
| 367 | let dictionary = replacement.as_hash(); |
| 368 | if dictionary.is_none() { |
| 369 | bail!(" expected a key/value pair. Found {}.", yaml_to_string(replacement, 0)); |
| 370 | }; |
| 371 | let dictionary = dictionary.unwrap(); |
| 372 | if dictionary.is_empty() { |
| 373 | bail!("No key/value pairs found for key 'replace'.\n\ |
| 374 | Suggestion: are the following lines indented properly?"); |
| 375 | } |
| 376 | if dictionary.len() > 1 { |
| 377 | bail!("Should only be one key/value pair for the replacement.\n \ |
| 378 | Suggestion: are the following lines indented properly?\n \ |
| 379 | The key/value pairs found are\n{}", yaml_to_string(replacement, 2)); |
| 380 | } |
| 381 | |
| 382 | // get the single value |
| 383 | let (key, value) = dictionary.iter().next().unwrap(); |
| 384 | let key = key.as_str().ok_or("replacement key(e.g, 't') is not a string")?; |
| 385 | match key { |
| 386 | "t" | "T" => { |
| 387 | return Ok( Replacement::Text( as_str_checked(value)?.to_string() ) ); |
| 388 | }, |
| 389 | "ct" | "CT" => { |
| 390 | return Ok( Replacement::Text( CONCAT_INDICATOR.to_string() + as_str_checked(value)? ) ); |
| 391 | }, |
| 392 | "ot" | "OT" => { |
| 393 | return Ok( Replacement::Text( OPTIONAL_INDICATOR.to_string() + as_str_checked(value)? + OPTIONAL_INDICATOR ) ); |
| 394 | }, |
| 395 | "x" => { |
| 396 | return Ok( Replacement::XPath( MyXPath::build(value) |
| 397 | .chain_err(|| "while trying to evaluate value of 'x:'")? ) ); |
| 398 | }, |
| 399 | "pause" | "rate" | "pitch" | "volume" | "audio" | "gender" | "voice" | "spell" | "SPELL" | "bookmark" | "pronounce" | "PRONOUNCE" => { |
| 400 | return Ok( Replacement::TTS( TTS::build(&key.to_ascii_lowercase(), value)? ) ); |
| 401 | }, |
| 402 | "intent" => { |
| 403 | return Ok( Replacement::Intent( Intent::build(value)? ) ); |
| 404 | }, |
| 405 | "test" => { |
| 406 | return Ok( Replacement::Test( Box::new( TestArray::build(value)? ) ) ); |
| 407 | }, |
| 408 | "with" => { |
| 409 | return Ok( Replacement::With( With::build(value)? ) ); |
| 410 | }, |
| 411 | "set_variables" => { |
| 412 | return Ok( Replacement::SetVariables( SetVariables::build(value)? ) ); |
| 413 | }, |
| 414 | "insert" => { |
| 415 | return Ok( Replacement::Insert( InsertChildren::build(value)? ) ); |
| 416 | }, |
| 417 | "translate" => { |
| 418 | return Ok( Replacement::Translate( TranslateExpression::build(value) |
| 419 | .chain_err(|| "while trying to evaluate value of 'speak:'")? ) ); |
| 420 | }, |
| 421 | _ => { |
| 422 | bail!("Unknown 'replace' command ({}) with value: {}", key, yaml_to_string(value, 0)); |
no test coverage detected