Skip to content

Commit

Permalink
New format for legend
Browse files Browse the repository at this point in the history
  • Loading branch information
keesverruijt committed Aug 22, 2024
1 parent e121c46 commit 79a955b
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ impl Serialize for Colour {
#[skip_serializing_none]
#[derive(Serialize)]
struct Lookup {
key: u8,
value: Option<u8>,
r#type: PixelType,
colour: Colour,
}
Expand All @@ -138,7 +136,7 @@ struct RadarApi {
spokes: u16,
max_spoke_len: u16,
stream_url: String,
legend: Vec<Lookup>,
legend: HashMap<String, Lookup>,
}

impl RadarApi {
Expand All @@ -149,12 +147,18 @@ impl RadarApi {
spokes,
max_spoke_len,
stream_url,
legend: Vec::new(),
legend: HashMap::new(),
}
}

fn set_legend(&mut self, legend: Vec<Lookup>) {
self.legend = legend;
let mut l = HashMap::new();
let mut n = 0;
for lookup in legend {
l.insert(n.to_string(), lookup);
n += 1;
}
self.legend = l;
}
}

Expand All @@ -177,16 +181,12 @@ fn fake_legend() -> Vec<Lookup> {
a: history * 4,
};
legend.push(Lookup {
key: history,
value: Some(history),
r#type: PixelType::History,
colour,
});
}
for v in 0..13 {
legend.push(Lookup {
key: 32 + v,
value: Some(v),
r#type: PixelType::Normal,
colour: Colour {
r: (v as f32 * (200. / 13.)) as u8,
Expand All @@ -197,8 +197,6 @@ fn fake_legend() -> Vec<Lookup> {
});
}
legend.push(Lookup {
key: 32 + 13,
value: None,
r#type: PixelType::TargetBorder,
colour: Colour {
r: 200,
Expand All @@ -208,8 +206,6 @@ fn fake_legend() -> Vec<Lookup> {
},
});
legend.push(Lookup {
key: 32 + 13 + 1,
value: None,
r#type: PixelType::DopplerApproaching,
colour: Colour {
r: 0,
Expand All @@ -219,8 +215,6 @@ fn fake_legend() -> Vec<Lookup> {
},
});
legend.push(Lookup {
key: 32 + 13 + 2,
value: None,
r#type: PixelType::DopplerReceding,
colour: Colour {
r: 0x90,
Expand Down Expand Up @@ -289,3 +283,25 @@ where
Self(err.into())
}
}

#[cfg(test)]
mod tests {
use serde_json::json;

use super::{fake_legend, RadarApi};

#[test]
fn legend() {
let mut v = RadarApi::new(
"Navico Halo".to_owned(),
"Halo A",
2048,
1024,
"http://bla".to_string(),
);
v.set_legend(fake_legend());

let json = json!(v).to_string();
println!("{}", json);
}
}

0 comments on commit 79a955b

Please sign in to comment.