-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml_out.rs
97 lines (88 loc) · 2.39 KB
/
toml_out.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-FileCopyrightText: 2023 German Aerospace Center (DLR)
// SPDX-License-Identifier: Apache-2.0
use serde::Serialize;
use std::fs::File;
use std::io::Write;
use crate::behaviortree::ClonedSegmentation;
#[derive(Serialize)]
struct Segment {
lower: usize,
upper: usize,
rho: f32,
details: SegmentDetail,
}
#[derive(Serialize)]
struct SegmentDetail {
id: usize,
description: String,
}
#[derive(Serialize)]
struct NamedSegmentation(String, TomlSegmentation);
#[derive(Serialize)]
struct TomlSegmentation {
delta: usize,
robustness: f32,
segments: Vec<Segment>,
}
#[derive(Serialize)]
struct Segmentations {
segmentations: Vec<NamedSegmentation>,
}
pub fn generate_toml(
location: String,
delta: usize,
best_segmentation: ClonedSegmentation,
alternative_segmentation: Option<Vec<ClonedSegmentation>>,
) -> std::io::Result<()> {
let mut segmentations = Vec::<NamedSegmentation>::new();
// Adding best segmentation
segmentations.push(read_segmentation(
best_segmentation,
delta,
"best".to_string(),
));
// Adding the alternative segmentations
if let Some(alternatives) = alternative_segmentation {
alternatives.into_iter().enumerate().for_each(|(i, seg)| {
segmentations.push(read_segmentation(
seg,
delta,
format!("alternative_{}", i + 1),
))
})
}
let all_segmentations = Segmentations { segmentations };
let toml_string =
toml::to_string_pretty(&all_segmentations).expect("Failed to serialize to TOML");
let mut file = File::create(location)?;
file.write_all(toml_string.as_bytes())?;
Ok(())
}
fn read_segmentation(
segmentation: (f32, Vec<(crate::behaviortree::TbtNode, usize, usize, f32)>),
delta: usize,
name: String,
) -> NamedSegmentation {
let mut segments = Vec::<Segment>::new();
for (node, lower, upper, rho) in segmentation.1 {
let details = SegmentDetail {
id: node.get_index(),
description: node.pretty_print(false, 0),
};
let segment = Segment {
lower,
upper,
rho,
details,
};
segments.push(segment);
}
NamedSegmentation(
name,
TomlSegmentation {
delta,
robustness: segmentation.0,
segments,
},
)
}