Simplify code

This commit is contained in:
Maximilian Ammann 2022-01-23 22:09:11 +01:00
parent 1d2f619bdc
commit 1024d62eb8
4 changed files with 26 additions and 34 deletions

View File

@ -1,5 +1,3 @@
extern crate protobuf_codegen_pure;
use std::path::PathBuf; use std::path::PathBuf;
fn main() { fn main() {

View File

@ -20,7 +20,7 @@ impl Decode<PropertyValue> for ProtoValue {
if self.has_bool_value() { if self.has_bool_value() {
PropertyValue::BoolValue(self.get_bool_value()) PropertyValue::BoolValue(self.get_bool_value())
} else if self.has_string_value() { } else if self.has_string_value() {
PropertyValue::StringValue(String::from(self.take_string_value())) PropertyValue::StringValue(self.take_string_value())
} else if self.has_float_value() { } else if self.has_float_value() {
PropertyValue::FloatValue(self.get_float_value()) PropertyValue::FloatValue(self.get_float_value())
} else if self.has_int_value() { } else if self.has_int_value() {
@ -68,7 +68,7 @@ impl Decode<GeometryPoint> for Vec<u32> {
let mut points = vec![]; let mut points = vec![];
let mut i = 0; let mut i = 0;
while i < self.len() - 1 { while i < self.len() {
let command = self[i] & 0x7; let command = self[i] & 0x7;
if command != CMD_MOVE_TO { if command != CMD_MOVE_TO {
@ -76,7 +76,7 @@ impl Decode<GeometryPoint> for Vec<u32> {
panic!("error") panic!("error")
} }
let count = (self[i] >> 3) as usize; let count = self[i] >> 3;
i += 1; i += 1;
for _ in 0..count { for _ in 0..count {
@ -86,12 +86,10 @@ impl Decode<GeometryPoint> for Vec<u32> {
} }
} }
if points.len() == 1 { match points.len() {
GeometryPoint::Point(points.remove(0)) 0 => GeometryPoint::Point(Point::new(0, 0)),
} else if points.len() > 1 { 1 => GeometryPoint::Point(points.remove(0)),
GeometryPoint::MultiPoint(MultiPoint::new(points)) _ => GeometryPoint::MultiPoint(MultiPoint::new(points)),
} else {
GeometryPoint::Point(Point::new(0, 0)) // point is at the origin
} }
} }
} }
@ -101,31 +99,29 @@ impl Decode<GeometryLineString> for Vec<u32> {
let mut commands = Vec::with_capacity(self.len()); // Create vec of maximum size let mut commands = Vec::with_capacity(self.len()); // Create vec of maximum size
let mut i = 0; let mut i = 0;
while i < self.len() - 1 { while i < self.len() {
let command = self[i] & 0x7; let command = self[i] & 0x7;
let count = (self[i] >> 3) as usize; let count = self[i] >> 3;
i += 1; i += 1;
match command { match command {
CMD_MOVE_TO => { CMD_MOVE_TO => {
for _ in 0..count { for _ in 0..count {
let x_index = i;
commands.push(Command::MoveTo(MoveTo { commands.push(Command::MoveTo(MoveTo {
x: self[x_index].zagzig(), x: self[i].zagzig(),
y: self[x_index + 1].zagzig(), y: self[i + 1].zagzig(),
})); }));
i += CMD_MOVE_TO_PARAMETERS; i += CMD_MOVE_TO_PARAMETERS;
} }
} }
CMD_LINE_TO => { CMD_LINE_TO => {
for _ in 0..count { for _ in 0..count {
let x_index = i;
commands.push(Command::LineTo(LineTo { commands.push(Command::LineTo(LineTo {
x: self[x_index].zagzig(), x: self[i].zagzig(),
y: self[x_index + 1].zagzig(), y: self[i + 1].zagzig(),
})); }));
i += CMD_MOVE_TO_PARAMETERS; i += CMD_LINE_TO_PARAMETERS;
} }
} }
CMD_CLOSE_PATH => { CMD_CLOSE_PATH => {
@ -149,7 +145,7 @@ impl Decode<GeometryPolygon> for Vec<u32> {
while i < self.len() { while i < self.len() {
let command = self[i] & 0x7; let command = self[i] & 0x7;
let count = (self[i] >> 3) as usize; let count = self[i] >> 3;
// parsed command and count => +1 // parsed command and count => +1
i += 1; i += 1;
@ -157,30 +153,29 @@ impl Decode<GeometryPolygon> for Vec<u32> {
match command { match command {
CMD_MOVE_TO => { CMD_MOVE_TO => {
for _ in 0..count { for _ in 0..count {
let x_index = i;
commands.push(Command::MoveTo(MoveTo { commands.push(Command::MoveTo(MoveTo {
x: self[x_index].zagzig(), x: self[i].zagzig(),
y: self[x_index + 1].zagzig(), y: self[i + 1].zagzig(),
})); }));
i += CMD_MOVE_TO_PARAMETERS; i += CMD_MOVE_TO_PARAMETERS;
} }
} }
CMD_LINE_TO => { CMD_LINE_TO => {
for _ in 0..count { for _ in 0..count {
let x_index = i;
commands.push(Command::LineTo(LineTo { commands.push(Command::LineTo(LineTo {
x: self[x_index].zagzig(), x: self[i].zagzig(),
y: self[x_index + 1].zagzig(), y: self[i + 1].zagzig(),
})); }));
i += CMD_MOVE_TO_PARAMETERS; i += CMD_LINE_TO_PARAMETERS;
} }
} }
CMD_CLOSE_PATH => { CMD_CLOSE_PATH => {
for _ in 0..count { if count != 1 {
panic!("error")
}
commands.push(Command::Close); commands.push(Command::Close);
i += CMD_CLOSE_PATH_PARAMETERS; i += CMD_CLOSE_PATH_PARAMETERS;
} }
}
_ => { _ => {
panic!("error") panic!("error")
} }

View File

@ -23,7 +23,7 @@ pub mod tile;
pub fn parse_tile<P: AsRef<Path>>(path: P) -> Result<Tile, Error> { pub fn parse_tile<P: AsRef<Path>>(path: P) -> Result<Tile, Error> {
let f = File::open(path)?; let f = File::open(path)?;
let mut reader = BufReader::new(f); let mut reader = BufReader::new(f);
parse_tile_reader(&mut reader).into() parse_tile_reader(&mut reader)
} }
pub fn parse_tile_reader<B: BufRead>(reader: &mut B) -> Result<Tile, Error> { pub fn parse_tile_reader<B: BufRead>(reader: &mut B) -> Result<Tile, Error> {

View File

@ -76,8 +76,7 @@ impl WorkerLoop {
match fetcher.fetch_tile(&coords).await { match fetcher.fetch_tile(&coords).await {
Ok(data) => { Ok(data) => {
info!("preparing tile {} with {}bytes", &coords, data.len()); info!("preparing tile {} with {}bytes", &coords, data.len());
let tile = parse_tile_bytes(bytemuck::cast_slice(data.as_slice())) let tile = parse_tile_bytes(data.as_slice()).expect("failed to load tile");
.expect("failed to load tile");
for layer in tile.layers() { for layer in tile.layers() {
if let Some((buffer, feature_vertices)) = layer.tesselate() { if let Some((buffer, feature_vertices)) = layer.tesselate() {