Skip to content

Commit a896f9f

Browse files
committed
Parsing main infoemations from xml
1 parent 779c954 commit a896f9f

File tree

5 files changed

+175
-16
lines changed

5 files changed

+175
-16
lines changed

src/main.rs

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
pub mod node;
22
use node::Node;
33

4+
pub mod way;
5+
use way::Way;
6+
7+
pub mod relation;
8+
use relation::Relation;
9+
410
pub mod main_info;
511
use main_info::MainInfo;
12+
use main_info::Attr;
13+
// use main_info::Tag;
614

715
pub mod argument;
816
use argument::Arguments;
917

1018
use quick_xml::events::Event;
1119
use quick_xml::Reader;
20+
1221
use std::env;
1322
use std::path::Path;
1423

@@ -21,22 +30,64 @@ fn main() {
2130

2231
// let mut count = 0;
2332
let mut buf = Vec::new();
24-
// The `Reader` does not implement `Iterator` because it outputs borrowed data (`Cow`s)
2533
match result {
26-
Ok(mut reader) => loop {
27-
match reader.read_event(&mut buf) {
28-
Ok(Event::Start(e)) => match e.name() {
29-
b"node" => println!(
30-
"attributes values: {:?}",
31-
e.attributes().map(|a| a.unwrap().value).collect::<Vec<_>>()
32-
),
34+
Ok(mut reader) =>{
35+
// Self closing tags
36+
reader.expand_empty_elements(true);
37+
38+
loop {
39+
match reader.read_event(&mut buf) {
40+
Ok(Event::Start(e)) => match e.name() {
41+
b"node" => {
42+
let mut node:Node = Node {..Default::default()};
43+
44+
e.attributes().for_each(|a| match a {
45+
Ok(attr)=>{
46+
47+
let formated_attr = Attr::from_quick_xml(attr);
48+
let value = formated_attr.value.clone();
49+
let name = formated_attr.name.clone();
50+
51+
if !node.main_info.set_attribute(formated_attr) {
52+
match name.as_ref() {
53+
"lat"=> node.lat = value.parse::<f32>().unwrap(),
54+
"lon"=> node.lng = value.parse::<f32>().unwrap(),
55+
_=>(),
56+
}
57+
}
58+
},
59+
Err(e)=>panic!("{:?}",e)
60+
});
61+
},
62+
b"way"=>{
63+
let mut way:Way = Way {..Default::default()};
64+
65+
e.attributes().for_each(|a| match a {
66+
Ok(attr)=>{
67+
way.main_info.set_attribute(Attr::from_quick_xml(attr));
68+
},
69+
Err(e)=>panic!("{:?}",e)
70+
});
71+
},
72+
b"relation"=>{
73+
let mut relation:Relation = Relation {..Default::default()};
74+
75+
e.attributes().for_each(|a| match a {
76+
Ok(attr)=>{
77+
relation.main_info.set_attribute(Attr::from_quick_xml(attr));
78+
},
79+
Err(e)=>panic!("{:?}",e)
80+
});
81+
82+
},
83+
_ => (),
84+
},
85+
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
86+
Ok(Event::Eof) => break,
3387
_ => (),
34-
},
35-
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
36-
Ok(Event::Eof) => break,
37-
_ => (),
88+
}
89+
buf.clear();
3890
}
39-
buf.clear();
4091
},
4192
Err(e) => panic!("Invalid file :- {:?}", e),
4293
}

src/main_info.rs

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,92 @@
1+
use quick_xml::events::attributes::Attribute;
2+
use std::str;
3+
14
pub struct MainInfo {
2-
pub changeset:i16,
3-
pub id: i16,
5+
pub changeset:i32,
6+
pub id: i32,
47
pub version: i8,
58
pub timestamp: String,
69
pub user: String,
7-
pub uid: i16,
10+
pub uid: i32,
811
pub visible: bool,
912
pub tags: Vec<UsedTag>,
1013
}
1114

15+
impl Default for MainInfo {
16+
fn default()->MainInfo{
17+
MainInfo {
18+
id: 00000,
19+
version: 8,
20+
changeset: 000000,
21+
timestamp: String::from("2011-01-12T14:23:49Z"),
22+
user: String::from("anonymous"),
23+
uid: 0000,
24+
visible: true,
25+
tags: vec!()
26+
}
27+
}
28+
}
29+
30+
impl MainInfo {
31+
pub fn set_attribute(&mut self, attr:crate::main_info::Attr)->bool{
32+
match attr.name.as_ref() {
33+
"id"=>{
34+
self.id = attr.value.parse::<i32>().unwrap();
35+
return true;
36+
} ,
37+
"version" => {
38+
self.version = attr.value.parse::<i8>().unwrap();
39+
return true;
40+
},
41+
"changeset"=>{
42+
self.changeset = attr.value.parse::<i32>().unwrap();
43+
return true;
44+
},
45+
"timestamp"=>{
46+
self.timestamp = attr.value;
47+
return true;
48+
},
49+
"user"=>{
50+
self.user = attr.value;
51+
return true;
52+
},
53+
"uid"=>{
54+
self.uid = attr.value.parse::<i32>().unwrap();
55+
return true;
56+
},
57+
"visible"=>{
58+
self.visible = attr.value=="true";
59+
return true;
60+
},
61+
_=>false
62+
}
63+
}
64+
}
65+
66+
pub struct Attr {
67+
pub name: String,
68+
pub value: String
69+
}
70+
71+
impl Attr {
72+
pub fn from_quick_xml (attr:Attribute)->Attr{
73+
let value = match str::from_utf8(& attr.value){
74+
Ok(ret_str)=>ret_str,
75+
Err(e)=>panic!("Attribute value error: {:?}",e)
76+
};
77+
78+
let name = match str::from_utf8(attr.key){
79+
Ok(ret_str)=>ret_str,
80+
Err(e)=>panic!("Attribute name error: {:?}",e)
81+
};
82+
83+
return Attr {
84+
value: value.to_owned(),
85+
name: name.to_owned()
86+
}
87+
}
88+
}
89+
1290
pub struct Tag {
1391
pub id: i16,
1492
pub name: String,

src/node.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,16 @@ pub struct Node {
22
pub main_info: crate::main_info::MainInfo,
33
pub lat: f32,
44
pub lng: f32
5+
}
6+
7+
impl Default for Node {
8+
fn default()->Node {
9+
Node {
10+
main_info: crate::main_info::MainInfo {
11+
..Default::default()
12+
},
13+
lat: 3232.3232323,
14+
lng: 232.232323232
15+
}
16+
}
517
}

src/relation.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ pub struct Relation {
99
pub members: Vec<Member>,
1010
pub main_info: crate::main_info::MainInfo
1111
}
12+
13+
impl Default for Relation {
14+
fn default()->Relation {
15+
Relation {
16+
members: vec![],
17+
main_info: crate::main_info::MainInfo { .. Default::default()}
18+
}
19+
}
20+
}

src/way.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
pub struct Way {
22
pub nodes: Vec<i8>,
33
pub main_info: crate::main_info::MainInfo,
4+
}
5+
6+
impl Default for Way {
7+
fn default()->Way {
8+
Way {
9+
main_info: crate::main_info::MainInfo {.. Default::default()},
10+
nodes: vec![]
11+
}
12+
}
413
}

0 commit comments

Comments
 (0)