serialization to RON, filtering out empty packages, deduplicating dependencies,

recover when failing to find field type
This commit is contained in:
Janis 2023-06-21 18:55:12 +02:00
parent 8886be1cb9
commit 953bfa10ef
2 changed files with 44 additions and 11 deletions

View file

@ -17,4 +17,8 @@ lazy_static = "1.4.0"
once_cell = "1.17.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = "1.0"
serde_json_any_key = "2.0.0"
ron = "0.8.0"

View file

@ -1,8 +1,10 @@
use std::collections::{btree_map::Entry, BTreeMap};
use std::path::Path;
use anyhow::Context;
use itertools::Itertools;
use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use ron::ser::PrettyConfig;
use crate::global_tables::objects::GOBJECTS;
use crate::sdk::repr::*;
@ -38,10 +40,31 @@ impl Sdk {
output
})
// remove empty packages
.filter(|pkg| {
pkg.as_ref()
.map(|(_, pkg)| !pkg.types.is_empty())
.unwrap_or(true)
})
.collect::<anyhow::Result<BTreeMap<_, _>>>()?;
Ok(Self { packages })
}
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}
pub fn to_ron(&self) -> Result<String, ron::Error> {
ron::ser::to_string_pretty(self, PrettyConfig::new().compact_arrays(true))
}
pub fn save_to_path<P: AsRef<Path>>(&self, path: P) -> Result<(), anyhow::Error> {
self.to_ron()
.context("failed to serialize")
.and_then(|text| std::fs::write(path, text).context("failed to write to file."))
}
fn get_packages() -> anyhow::Result<Vec<Package>> {
let objects = GOBJECTS
.read()
@ -152,7 +175,7 @@ impl Class {
}
});
let (fields, methods) = Self::process_children(&strct)?;
let (fields, methods) = Self::process_children(&strct);
Ok(Self {
obj_ref: strct.as_uobject().object_ref(),
@ -169,7 +192,7 @@ impl Class {
}
/// returns tuple of (fields, methods)
fn process_children(strct: &UStruct) -> anyhow::Result<(Vec<ClassField>, Vec<ClassMethod>)> {
fn process_children(strct: &UStruct) -> (Vec<ClassField>, Vec<ClassMethod>) {
enum ClassChild {
Field(ClassField),
Method(ClassMethod),
@ -191,20 +214,25 @@ impl Class {
Ok(child)
})
.fold_ok(
.filter_map(|prop| match prop {
Ok(field) => field,
Err(err) => {
log::warn!("skipping field because: {err}");
None
}
})
.fold(
(Vec::new(), Vec::new()),
|(mut fields, mut methods), child| {
if let Some(child) = child {
match child {
ClassChild::Field(field) => fields.push(field),
ClassChild::Method(method) => methods.push(method),
}
match child {
ClassChild::Field(field) => fields.push(field),
ClassChild::Method(method) => methods.push(method),
}
(fields, methods)
},
)?;
);
Ok((fields, methods))
(fields, methods)
}
pub fn get_dependent_types(&self) -> Vec<ObjectRef> {
@ -314,6 +342,7 @@ impl Package {
let dependencies = types
.iter()
.flat_map(|(_, ty)| ty.get_dependent_types().into_iter().map(|obj| obj.package))
.sorted()
.dedup()
.collect::<Vec<_>>();