Skip to content

Commit

Permalink
Config: rapify matches cfgconvert (#575)
Browse files Browse the repository at this point in the history
* config rapify matches cfgconvert

* cleanup comments
  • Loading branch information
BrettMayson authored Oct 15, 2023
1 parent 3f21131 commit c56252a
Show file tree
Hide file tree
Showing 38 changed files with 125 additions and 46 deletions.
8 changes: 5 additions & 3 deletions libs/config/src/analyze/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ impl Analyze for Class {
fn valid(&self, project: Option<&ProjectConfig>) -> bool {
match self {
Self::External { .. } => true,
Self::Local { properties, .. } => properties.iter().all(|p| p.valid(project)),
Self::Local { properties, .. } | Self::Root { properties, .. } => {
properties.iter().all(|p| p.valid(project))
}
}
}

Expand All @@ -20,7 +22,7 @@ impl Analyze for Class {
) -> Vec<Box<dyn Code>> {
match self {
Self::External { .. } => vec![],
Self::Local { properties, .. } => properties
Self::Local { properties, .. } | Self::Root { properties, .. } => properties
.iter()
.flat_map(|p| p.warnings(project, processed))
.collect::<Vec<_>>(),
Expand All @@ -30,7 +32,7 @@ impl Analyze for Class {
fn errors(&self, project: Option<&ProjectConfig>, processed: &Processed) -> Vec<Box<dyn Code>> {
match self {
Self::External { .. } => vec![],
Self::Local { properties, .. } => properties
Self::Local { properties, .. } | Self::Root { properties, .. } => properties
.iter()
.flat_map(|p| p.errors(project, processed))
.collect::<Vec<_>>(),
Expand Down
10 changes: 9 additions & 1 deletion libs/config/src/analyze/codes/ce7_missing_parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ impl Code for MissingParent {

fn generate_processed_report(&self, processed: &Processed) -> Option<String> {
let parent = self.class.parent()?;
let map = processed.mapping(self.class.name().span.start).unwrap();
let map = processed
.mapping(
self.class
.name()
.expect("parent existed to create error")
.span
.start,
)
.unwrap();
let token = map.token();
let parent_map = processed.mapping(parent.span.start).unwrap();
let parent_token = parent_map.token();
Expand Down
31 changes: 27 additions & 4 deletions libs/config/src/analyze/codes/cw1_parent_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,36 @@ impl Code for ParentCase {
fn help(&self) -> Option<String> {
Some(format!(
"change the parent case to match the parent definition: `{}`",
self.parent.name().as_str()
self.parent
.name()
.expect("parent existed to create error")
.as_str()
))
}

fn generate_processed_report(&self, processed: &Processed) -> Option<String> {
let class_parent = self.class.parent()?;
let map = processed.mapping(self.class.name().span.start).unwrap();
let map = processed
.mapping(
self.class
.name()
.expect("parent existed to create error")
.span
.start,
)
.unwrap();
let token = map.token();
let class_parent_map = processed.mapping(class_parent.span.start).unwrap();
let class_parent_token = class_parent_map.token();
let parent_map = processed.mapping(self.parent.name().span.start).unwrap();
let parent_map = processed
.mapping(
self.parent
.name()
.expect("parent existed to create error")
.span
.start,
)
.unwrap();
let parent_token = parent_map.token();
let mut out = Vec::new();
let mut colors = ColorGenerator::new();
Expand Down Expand Up @@ -74,7 +93,11 @@ impl Code for ParentCase {
.with_help(format!(
"change the {} to match the parent definition `{}`",
"parent case".fg(color_class),
self.parent.name().as_str().fg(color_parent)
self.parent
.name()
.expect("parent existed to create error")
.as_str()
.fg(color_parent)
))
.finish()
.write_for_stdout(sources(processed.sources_adrianne()), &mut out)
Expand Down
8 changes: 7 additions & 1 deletion libs/config/src/analyze/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ fn external_missing_error(
for property in properties {
if let Property::Class(c) = property {
match c {
Class::Root { properties } => {
errors.extend(external_missing_error(properties, defined));
}
Class::External { name } => {
let name = name.value.to_lowercase();
if !defined.contains(&name) {
Expand Down Expand Up @@ -92,6 +95,9 @@ fn external_parent_case_warn(
for property in properties {
if let Property::Class(c) = property {
match c {
Class::Root { .. } => {
panic!("Root class should not be in the config");
}
Class::External { name } => {
let name = name.value.to_lowercase();
defined.entry(name).or_insert_with(|| c.clone());
Expand All @@ -106,7 +112,7 @@ fn external_parent_case_warn(
let parent_lower = parent.value.to_lowercase();
if parent_lower != name_lower {
if let Some(parent_class) = defined.get(&parent_lower) {
if parent_class.name().value != parent.value {
if parent_class.name().map(|p| &p.value) != Some(&parent.value) {
warnings.push(Box::new(ParentCase::new(
c.clone(),
parent_class.clone(),
Expand Down
12 changes: 9 additions & 3 deletions libs/config/src/model/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use super::Ident;
#[derive(Debug, Clone, PartialEq)]
/// A class definition
pub enum Class {
/// The root class definition
Root {
/// The children of the class
properties: Vec<Property>,
},
/// A local class definition
///
/// ```cpp
Expand Down Expand Up @@ -42,17 +47,18 @@ pub enum Class {
impl Class {
#[must_use]
/// Get the name of the class
pub const fn name(&self) -> &Ident {
pub const fn name(&self) -> Option<&Ident> {
match self {
Self::External { name } | Self::Local { name, .. } => name,
Self::External { name } | Self::Local { name, .. } => Some(name),
Self::Root { .. } => None,
}
}

#[must_use]
/// Get the parent of the class
pub const fn parent(&self) -> Option<&Ident> {
match self {
Self::External { .. } => None,
Self::External { .. } | Self::Root { .. } => None,
Self::Local { parent, .. } => parent.as_ref(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion libs/config/src/model/ident.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Range;

#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// An identifier
///
/// ```cpp
Expand Down
7 changes: 5 additions & 2 deletions libs/config/src/model/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ pub enum Property {
impl Property {
#[must_use]
/// Get the name of the property
pub const fn name(&self) -> &Ident {
///
/// # Panics
/// If this is a [`Class::Root`], which should never occur
pub fn name(&self) -> &Ident {
match self {
Self::Class(c) => c.name(),
Self::Class(c) => c.name().expect("root should not be a property"),
Self::MissingSemicolon(name, _) | Self::Delete(name) | Self::Entry { name, .. } => name,
}
}
Expand Down
24 changes: 11 additions & 13 deletions libs/config/src/rapify/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ impl Rapify for Class {
) -> Result<usize, std::io::Error> {
let mut written = 0;
match self {
Self::Local {
parent, properties, ..
} => {
Self::Local { properties, .. } | Self::Root { properties } => {
let parent = self.parent();
if let Some(parent) = &parent {
output.write_cstring(parent.as_str())?;
written += parent.as_str().len() + 1;
Expand All @@ -30,7 +29,7 @@ impl Rapify for Class {
.iter()
.map(|p| p.name().len() + 1 + p.rapified_length())
.sum::<usize>();
let mut class_offset = offset + written + properties_len;
let mut class_offset = offset + written + properties_len + 4;
let mut class_bodies: Vec<Cursor<Box<[u8]>>> = Vec::new();
let pre_properties = written;

Expand Down Expand Up @@ -69,6 +68,9 @@ impl Rapify for Class {

assert_eq!(written - pre_properties, properties_len);

output.write_u32::<LittleEndian>(class_offset as u32)?;
written += 4;

for cursor in class_bodies {
output.write_all(cursor.get_ref())?;
written += cursor.get_ref().len();
Expand All @@ -86,25 +88,21 @@ impl Rapify for Class {
fn rapified_length(&self) -> usize {
match self {
Self::External { .. } => 0,
Self::Local {
name: _,
parent,
properties,
} => {
let parent_length = parent.as_ref().map_or(0, Ident::len);
Self::Local { properties, .. } | Self::Root { properties, .. } => {
let parent_length = self.parent().map_or(0, Ident::len);
parent_length
+ 1
+ 1 // parent null terminator
+ 4 // offset to next class
+ compressed_int_len(properties.len() as u32)
+ properties
.iter()
.map(|p| {
p.name().len()
+ 1
+ 1 // name null terminator
+ p.rapified_length()
+ match p {
Property::Class(c) => c.rapified_length(),
_ => 0,
// Property::Delete(i) => i.to_string().len() + 1,
}
})
.sum::<usize>()
Expand Down
11 changes: 4 additions & 7 deletions libs/config/src/rapify/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ impl Rapify for Config {
output.write_all(b"\0raP")?;
output.write_all(b"\0\0\0\0\x08\0\0\0")?;

let root_class = Class::Local {
name: crate::Ident::default(),
parent: None,
let root_class = Class::Root {
properties: self.0.clone(),
};
let buffer: Box<[u8]> = vec![0; root_class.rapified_length()].into_boxed_slice();
Expand All @@ -31,15 +29,14 @@ impl Rapify for Config {
output.write_all(cursor.get_ref())?;

output.write_all(b"\0\0\0\0")?;
assert_eq!(written + 20, self.rapified_length());
Ok(written + 20)
}

fn rapified_length(&self) -> usize {
let root_class = Class::Local {
name: crate::Ident::default(),
parent: None,
let root_class = Class::Root {
properties: self.0.clone(),
};
20 + root_class.rapified_length()
root_class.rapified_length() + 20 // metadata
}
}
3 changes: 2 additions & 1 deletion libs/config/src/rapify/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Rapify for Property {
Value::UnexpectedArray(_) | Value::Invalid(_) => unreachable!(),
},
Self::Class(c) => match c {
Class::Root { .. } => panic!("root should not be a property"),
Class::Local { .. } => 4,
Class::External { .. } => 0,
},
Expand Down Expand Up @@ -48,7 +49,7 @@ impl Property {
Value::UnexpectedArray(_) | Value::Invalid(_) => unreachable!(),
},
Self::Class(c) => match c {
Class::Local { .. } => vec![0],
Class::Local { .. } | Class::Root { .. } => vec![0],
Class::External { .. } => vec![3],
},
Self::Delete(_) => {
Expand Down
16 changes: 15 additions & 1 deletion libs/config/tests/rapify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,34 @@ fn check(dir: &str) {
};
let parsed = parsed.unwrap();
let mut expected = Vec::new();
std::fs::File::open(folder.join("expected.bin"))
let expected_path = folder.join("expected.bin");
if !expected_path.exists() {
let mut file = std::fs::File::create(&expected_path).unwrap();
parsed.config().rapify(&mut file, 0).unwrap();
panic!("expected file did not exist, created it");
};
std::fs::File::open(expected_path)
.unwrap()
.read_to_end(&mut expected)
.unwrap();
let mut output = Vec::new();
let written = parsed.config().rapify(&mut output, 0).unwrap();
assert_eq!(written, parsed.config().rapified_length());
assert_eq!(output, expected);
let vanilla_path = folder.join("cfgconvert.bin");
if vanilla_path.exists() {
let mut expected = Vec::new();
let mut file = std::fs::File::open(&vanilla_path).unwrap();
file.read_to_end(&mut expected).unwrap();
assert_eq!(output, expected);
};
}

bootstrap!(ace_main);
bootstrap!(cba_multiline);
bootstrap!(delete_class);
bootstrap!(external_class);
bootstrap!(inheritence_array_extend);
bootstrap!(join_digit);
bootstrap!(join_in_ident);
bootstrap!(join);
Expand Down
Binary file added libs/config/tests/rapify/ace_main/cfgconvert.bin
Binary file not shown.
Binary file modified libs/config/tests/rapify/ace_main/expected.bin
Binary file not shown.
Binary file not shown.
Binary file modified libs/config/tests/rapify/cba_multiline/expected.bin
Binary file not shown.
Binary file not shown.
Binary file modified libs/config/tests/rapify/delete_class/expected.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions libs/config/tests/rapify/delete_class/source.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class thing {
value = 123;
delete hello;
value = 123;
delete hello;
};
delete world;
Binary file not shown.
Binary file modified libs/config/tests/rapify/external_class/expected.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion libs/config/tests/rapify/external_class/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class thing: test {
value = 12;
};

class thing {
class thing2 {
class things;
};
class another: thing {};
Expand Down
Binary file not shown.
Binary file not shown.
22 changes: 22 additions & 0 deletions libs/config/tests/rapify/inheritence_array_extend/source.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class external;
class local: external {
extend[] += {
"item1",
"item2"
};
complete[] = {
"item3",
"item4"
};
};
class solo {
extend[] += {
"item1",
"item2"
};
complete[] = {
"item3",
"item4",
"item5"
};
};
Binary file modified libs/config/tests/rapify/join/expected.bin
Binary file not shown.
3 changes: 1 addition & 2 deletions libs/config/tests/rapify/join/source.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#define A my
#define B class

class A##B
{
class A##B {
value = 1;
test[] = {1,3};
};
Binary file modified libs/config/tests/rapify/join_digit/expected.bin
Binary file not shown.
Binary file not shown.
Binary file modified libs/config/tests/rapify/join_in_ident/expected.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion libs/config/tests/rapify/join_in_ident/source.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define DOUBLES(a,b) a##b

class thing {
DOUBLES(hello,world) = "test";
DOUBLES(hello,world) = "test";
};
Binary file added libs/config/tests/rapify/nested_array/cfgconvert.bin
Binary file not shown.
Binary file modified libs/config/tests/rapify/nested_array/expected.bin
Binary file not shown.
Binary file added libs/config/tests/rapify/numbers/cfgconvert.bin
Binary file not shown.
Binary file modified libs/config/tests/rapify/numbers/expected.bin
Binary file not shown.
6 changes: 3 additions & 3 deletions libs/config/tests/rapify/numbers/source.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class numbers {
hex = 0x1A;
dec = 42;
float = 3.14;
hex = 0x1A;
dec = 42;
float = 3.14;
};
Binary file not shown.
Binary file modified libs/config/tests/rapify/procedural_texture/expected.bin
Binary file not shown.
Binary file modified libs/config/tests/rapify/single_class/expected.bin
Binary file not shown.

0 comments on commit c56252a

Please sign in to comment.