|
| 1 | +use std::{collections::HashMap, io::Read}; |
| 2 | + |
| 3 | +use flate2::read::GzDecoder; |
1 | 4 | use include_dir::{Dir, include_dir}; |
2 | 5 | use serde::{Deserialize, Serialize}; |
3 | | -use std::collections::HashMap; |
4 | 6 |
|
| 7 | +// Gzipped parameter JSONs (built in build.rs) to reduce embedded size |
5 | 8 | static ASSETS_DIR: Dir = include_dir!( |
6 | | - "$CARGO_MANIFEST_DIR/src/lib/drivers/rest/autopilot/parameters/ardupilot_parameters", |
7 | | - "**/*.json" |
| 9 | + "$CARGO_MANIFEST_DIR/ardupilot_parameters_gz", |
| 10 | + "**/*.json.gz" |
8 | 11 | ); |
9 | 12 |
|
10 | 13 | // A function that caches it return value, should return a map of vehicle type by version |
@@ -43,12 +46,17 @@ pub fn get_parameters(vehicle_type: String, version: String) -> HashMap<String, |
43 | 46 | .get_dir(format!("{}-{}", vehicle_type, version)) |
44 | 47 | .and_then(|dir| { |
45 | 48 | dir.files() |
46 | | - .find(|file| file.path().extension().unwrap_or_default() == "json") |
| 49 | + .find(|f| f.path().extension().unwrap_or_default() == "gz") |
47 | 50 | }); |
48 | 51 |
|
49 | 52 | if let Some(file) = file { |
50 | | - let content = file.contents_utf8().unwrap(); |
51 | | - let mut json_value: serde_json::Value = serde_json::from_str(content).unwrap(); |
| 53 | + // Decompress the embedded .json.gz files |
| 54 | + let compressed = file.contents(); |
| 55 | + let mut decoder = GzDecoder::new(compressed); |
| 56 | + let mut content = Vec::new(); |
| 57 | + decoder.read_to_end(&mut content).unwrap(); |
| 58 | + let content = String::from_utf8(content).unwrap(); |
| 59 | + let mut json_value: serde_json::Value = serde_json::from_str(&content).unwrap(); |
52 | 60 |
|
53 | 61 | // TODO: Deal with the version number |
54 | 62 | if let Some(obj) = json_value.as_object_mut() { |
|
0 commit comments