serialization to RON, filtering out empty packages, deduplicating dependencies,
recover when failing to find field type
This commit is contained in:
parent
8886be1cb9
commit
953bfa10ef
|
@ -17,4 +17,8 @@ lazy_static = "1.4.0"
|
||||||
once_cell = "1.17.1"
|
once_cell = "1.17.1"
|
||||||
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
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"
|
|
@ -1,8 +1,10 @@
|
||||||
use std::collections::{btree_map::Entry, BTreeMap};
|
use std::collections::{btree_map::Entry, BTreeMap};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
|
use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
|
||||||
|
use ron::ser::PrettyConfig;
|
||||||
|
|
||||||
use crate::global_tables::objects::GOBJECTS;
|
use crate::global_tables::objects::GOBJECTS;
|
||||||
use crate::sdk::repr::*;
|
use crate::sdk::repr::*;
|
||||||
|
@ -38,10 +40,31 @@ impl Sdk {
|
||||||
|
|
||||||
output
|
output
|
||||||
})
|
})
|
||||||
|
// remove empty packages
|
||||||
|
.filter(|pkg| {
|
||||||
|
pkg.as_ref()
|
||||||
|
.map(|(_, pkg)| !pkg.types.is_empty())
|
||||||
|
.unwrap_or(true)
|
||||||
|
})
|
||||||
.collect::<anyhow::Result<BTreeMap<_, _>>>()?;
|
.collect::<anyhow::Result<BTreeMap<_, _>>>()?;
|
||||||
|
|
||||||
Ok(Self { packages })
|
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>> {
|
fn get_packages() -> anyhow::Result<Vec<Package>> {
|
||||||
let objects = GOBJECTS
|
let objects = GOBJECTS
|
||||||
.read()
|
.read()
|
||||||
|
@ -152,7 +175,7 @@ impl Class {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let (fields, methods) = Self::process_children(&strct)?;
|
let (fields, methods) = Self::process_children(&strct);
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
obj_ref: strct.as_uobject().object_ref(),
|
obj_ref: strct.as_uobject().object_ref(),
|
||||||
|
@ -169,7 +192,7 @@ impl Class {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// returns tuple of (fields, methods)
|
/// 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 {
|
enum ClassChild {
|
||||||
Field(ClassField),
|
Field(ClassField),
|
||||||
Method(ClassMethod),
|
Method(ClassMethod),
|
||||||
|
@ -191,20 +214,25 @@ impl Class {
|
||||||
|
|
||||||
Ok(child)
|
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()),
|
(Vec::new(), Vec::new()),
|
||||||
|(mut fields, mut methods), child| {
|
|(mut fields, mut methods), child| {
|
||||||
if let Some(child) = child {
|
|
||||||
match child {
|
match child {
|
||||||
ClassChild::Field(field) => fields.push(field),
|
ClassChild::Field(field) => fields.push(field),
|
||||||
ClassChild::Method(method) => methods.push(method),
|
ClassChild::Method(method) => methods.push(method),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
(fields, methods)
|
(fields, methods)
|
||||||
},
|
},
|
||||||
)?;
|
);
|
||||||
|
|
||||||
Ok((fields, methods))
|
(fields, methods)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_dependent_types(&self) -> Vec<ObjectRef> {
|
pub fn get_dependent_types(&self) -> Vec<ObjectRef> {
|
||||||
|
@ -314,6 +342,7 @@ impl Package {
|
||||||
let dependencies = types
|
let dependencies = types
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|(_, ty)| ty.get_dependent_types().into_iter().map(|obj| obj.package))
|
.flat_map(|(_, ty)| ty.get_dependent_types().into_iter().map(|obj| obj.package))
|
||||||
|
.sorted()
|
||||||
.dedup()
|
.dedup()
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue