File size: 5,784 Bytes
05e776e 49ec522 05e776e 49ec522 05e776e 49ec522 05e776e 49ec522 05e776e 49ec522 05e776e 49ec522 05e776e 49ec522 05e776e 49ec522 05e776e 49ec522 05e776e | 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | //! Compatibility reporting only: parsing/encoding remain authoritative validators.
use mb3_dat::{Version, World};
use std::collections::HashSet;
#[derive(Clone, Copy, Debug)]
pub enum Target {
Txt,
Obj,
Dat(Version),
}
impl Target {
pub fn name(self) -> &'static str {
match self {
Self::Txt => "txt",
Self::Obj => "obj",
Self::Dat(Version::V201501280) => "dat-201501280",
Self::Dat(Version::V201512200) => "dat-201512200",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Issue {
pub code: &'static str,
pub count: usize,
pub lossy: bool,
}
#[derive(Clone, Debug)]
pub struct Report {
pub target: Target,
pub issues: Vec<Issue>,
}
impl Report {
pub fn blocked(&self) -> bool {
self.issues.iter().any(|i| !i.lossy)
}
pub fn compatible(&self) -> bool {
self.issues.is_empty()
}
pub fn json(&self) -> String {
let issues = self
.issues
.iter()
.map(|i| {
format!(
"{{\"code\":\"{}\",\"count\":{},\"lossy\":{}}}",
i.code, i.count, i.lossy
)
})
.collect::<Vec<_>>()
.join(",");
format!(
"{{\"schema\":1,\"target\":\"{}\",\"compatible\":{},\"issues\":[{}]}}",
self.target.name(),
self.compatible(),
issues
)
}
}
pub fn analyze(world: &World, target: Target) -> Report {
let mut issues = Vec::new();
let planes = || world.brushes.iter().flat_map(|b| &b.planes);
match target {
Target::Obj => {
if !world.computed {
issues.push(Issue {
code: "computed_required",
count: 1,
lossy: false,
});
}
let n = planes().count().saturating_add(world.patches.len());
if n != 0 {
issues.push(Issue {
code: "editable_geometry",
count: n,
lossy: true,
});
}
let n = world
.string_properties
.len()
.saturating_add(world.uv_properties.len())
.saturating_add(
planes()
.map(|p| p.strings.len().saturating_add(p.uv.len()))
.fold(0usize, usize::saturating_add),
);
if n != 0 {
issues.push(Issue {
code: "properties",
count: n,
lossy: true,
});
}
let n = world
.patches
.iter()
.filter_map(|p| p.computed.as_ref())
.map(|p| p.normals.len())
.fold(0usize, usize::saturating_add);
if n != 0 {
issues.push(Issue {
code: "stored_normals",
count: n,
lossy: true,
});
}
}
Target::Dat(version) => {
let n = world
.string_properties
.iter()
.chain(&world.uv_properties)
.filter(|s| !s.is_ascii())
.count()
.saturating_add(
planes()
.flat_map(|p| &p.strings)
.filter(|p| !p.value.is_ascii())
.count(),
);
if n != 0 {
issues.push(Issue {
lossy: false,
code: "non_ascii",
count: n,
});
}
if version == Version::V201501280 {
let n = world
.string_properties
.len()
.saturating_add(world.uv_properties.len())
.saturating_add(
planes()
.map(|p| p.strings.len().saturating_add(p.uv.len()))
.fold(0usize, usize::saturating_add),
);
if n != 0 {
issues.push(Issue {
lossy: false,
code: "legacy_properties",
count: n,
});
}
}
}
Target::Txt => {
let mut n = 0usize;
for names in [&world.string_properties, &world.uv_properties] {
let mut seen = HashSet::new();
for name in names {
if !seen.insert(name) {
n = n.saturating_add(1);
}
}
}
if n != 0 {
issues.push(Issue {
lossy: false,
code: "duplicate_names",
count: n,
});
}
}
}
Report { target, issues }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn grouped_reports_and_no_mutation() {
let mut w = World {
string_properties: vec!["é".into(), "é".into()],
..World::default()
};
let original = w.clone();
assert_eq!(analyze(&w, Target::Txt).issues[0].count, 1);
let r = analyze(&w, Target::Dat(Version::V201501280));
assert_eq!(r.issues.len(), 2);
assert!(!r.compatible());
assert!(r.json().contains("\"schema\":1"));
assert_eq!(w, original);
w.string_properties.clear();
assert!(analyze(&w, Target::Txt).compatible());
}
}
|