Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
58
Task/XML-Output/Rust/xml-output.rust
Normal file
58
Task/XML-Output/Rust/xml-output.rust
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
extern crate xml;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::str;
|
||||
|
||||
use xml::writer::{EmitterConfig, XmlEvent};
|
||||
|
||||
fn characters_to_xml(characters: HashMap<String, String>) -> String {
|
||||
let mut output: Vec<u8> = Vec::new();
|
||||
let mut writer = EmitterConfig::new()
|
||||
.perform_indent(true)
|
||||
.create_writer(&mut output);
|
||||
|
||||
writer
|
||||
.write(XmlEvent::start_element("CharacterRemarks"))
|
||||
.unwrap();
|
||||
|
||||
for (character, line) in &characters {
|
||||
let element = XmlEvent::start_element("Character").attr("name", character);
|
||||
writer.write(element).unwrap();
|
||||
writer.write(XmlEvent::characters(line)).unwrap();
|
||||
writer.write(XmlEvent::end_element()).unwrap();
|
||||
}
|
||||
|
||||
writer.write(XmlEvent::end_element()).unwrap();
|
||||
str::from_utf8(&output).unwrap().to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::characters_to_xml;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn test_xml_output() {
|
||||
let mut input = HashMap::new();
|
||||
input.insert(
|
||||
"April".to_string(),
|
||||
"Bubbly: I'm > Tam and <= Emily".to_string(),
|
||||
);
|
||||
input.insert(
|
||||
"Tam O'Shanter".to_string(),
|
||||
"Burns: \"When chapman billies leave the street ...\"".to_string(),
|
||||
);
|
||||
input.insert("Emily".to_string(), "Short & shrift".to_string());
|
||||
|
||||
let output = characters_to_xml(input);
|
||||
|
||||
println!("{}", output);
|
||||
assert!(output.contains(
|
||||
"<Character name=\"Tam O'Shanter\">Burns: \"When chapman \
|
||||
billies leave the street ...\"</Character>"
|
||||
));
|
||||
assert!(output
|
||||
.contains("<Character name=\"April\">Bubbly: I'm > Tam and <= Emily</Character>"));
|
||||
assert!(output.contains("<Character name=\"Emily\">Short & shrift</Character>"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue