Use fold instead of join in PolyLine rendering (#83)
* Use fold instead of join in PolyLine rendering * Add test
This commit is contained in:
parent
e40727e914
commit
8a981efd1d
|
|
@ -1,6 +1,8 @@
|
|||
use super::style;
|
||||
use super::LayerData;
|
||||
|
||||
use std::fmt::Write;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PolyLine {
|
||||
points: Vec<kurbo::Point>,
|
||||
|
|
@ -18,7 +20,23 @@ impl PolyLine {
|
|||
|
||||
impl LayerData for PolyLine {
|
||||
fn render(&self) -> String {
|
||||
let points = self.points.iter().map(|p| format!("{},{}", p.x, p.y)).collect::<Vec<_>>().join(" ");
|
||||
format!(r#"<polyline points="{}" {}" />"#, points, self.style.render())
|
||||
if self.points.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
let points = self.points.iter().fold(String::new(), |mut acc, p| {
|
||||
let _ = write!(&mut acc, " {:.3} {:.3}", p.x, p.y);
|
||||
acc
|
||||
});
|
||||
format!(r#"<polyline points="{}" {}/>"#, &points[1..], self.style.render())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn polyline_should_render() {
|
||||
let polyline = PolyLine {
|
||||
points: vec![kurbo::Point::new(3.0, 4.12354), kurbo::Point::new(1.0, 5.54)],
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(crate::color::Color::GREEN, 0.4)), None),
|
||||
};
|
||||
|
||||
assert_eq!(r#"<polyline points="3.000 4.124 1.000 5.540" style="stroke: #00FF00FF;stroke-width:0.4;"/>"#, polyline.render());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue