Browse Source

Show xml as chars

Johann Woelper 8 năm trước cách đây
mục cha
commit
d1b84f4e87
1 tập tin đã thay đổi với 26 bổ sung3 xóa
  1. 26 3
      src/main.rs

+ 26 - 3
src/main.rs

@@ -6,9 +6,12 @@ use chrono::Duration;
 use chrono::TimeZone;
 use chrono::Utc;
 use std::time::Duration as stdDuration;
+use std::io::Cursor;
+use std::str;
 
-use quick_xml::events::Event;
+use quick_xml::events::{Event, BytesEnd, BytesStart};
 use quick_xml::Reader;
+use quick_xml::Writer;
 
 #[derive(Debug, Clone)]
 struct Point {
@@ -50,6 +53,19 @@ impl Track {
         sum
     }
 
+    fn to_xml(&self) {
+
+        let mut writer = Writer::new(Cursor::new(Vec::new()));
+        let mut elem = BytesStart::owned(b"trkpt".to_vec(), "trkpt".len());
+        elem.push_attribute(("lat", "13"));
+        assert!(writer.write_event(Event::Start(elem)).is_ok());
+        assert!(writer.write_event(Event::End(BytesEnd::borrowed(b"trkpt"))).is_ok());
+
+        let result = writer.into_inner().into_inner();
+        println!("{:?}", str::from_utf8( &result).unwrap());
+        
+    }
+
     fn time(&self) -> chrono::Duration {
         // let timespan = &self.points[0].time.signed_duration_since(self.points[1].time);
         let mut sum = Duration::from_std(stdDuration::new(0, 0)).unwrap();
@@ -74,6 +90,9 @@ impl Track {
     }
 
     fn parse(&mut self) {
+
+        let global_average_speed = self.speed();
+
         let mut fifo_track = Track {
             ..Default::default()
         };
@@ -92,9 +111,13 @@ impl Track {
             fifo_track.points.push(pt.clone());
             analyzed_track.points.push(pt.clone());
             fifo_track.truncate_by_length(0.1);
-            println!("Speed {:?} @ Km {:?}", fifo_track.speed(), analyzed_track.len());
-
+            let speed = fifo_track.speed();
+            if speed < global_average_speed*0.4 {
+                bad_track.points.push(pt.clone());
+                // println!("Track seems bad @ Km {:.1} {:.2} Km/h", analyzed_track.len(), speed);
+            }
         }
+        bad_track.to_xml();
     }
 }