Skip to content

Commit b9a58b3

Browse files
committed
Add manual default overrides
Closes #2
1 parent f8def00 commit b9a58b3

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codegen/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ impl PythonBindType {
6060
"RenderType",
6161
];
6262

63+
pub const DEFAULT_OVERRIDES: [(&'static str, &'static str, &'static str); 1] =
64+
[("Color", "a", "255")];
65+
6366
fn new(path: &Path) -> Option<Self> {
6467
// get the filename without the extension
6568
let filename = path.file_stem().unwrap().to_str().unwrap();

codegen/pyi.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
274274
for (variable_info, python_type) in bind.types.iter().zip(&python_types) {
275275
let variable_name = variable_info.name.as_str();
276276

277+
if let Some((field, value)) = bind.default_override {
278+
if field == variable_name {
279+
write_fmt!(
280+
file,
281+
" {variable_name}: {python_type} = {value},"
282+
);
283+
continue;
284+
}
285+
}
286+
277287
let default_value = match variable_info.raw_type.as_str() {
278288
"bool" => Cow::Borrowed("False"),
279289
"i32" | "u32" | "f32" | "u8" => Cow::Borrowed("0"),

codegen/structs.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct StructBindGenerator {
5454
has_complex_pack: bool,
5555
pub is_frozen: bool,
5656
is_no_set: bool,
57+
pub default_override: Option<(&'static str, &'static str)>,
5758
}
5859

5960
macro_rules! write_str {
@@ -103,6 +104,17 @@ impl StructBindGenerator {
103104
file_contents.push(Cow::Borrowed("use pyo3::{prelude::*, types::*};"));
104105
file_contents.push(Cow::Borrowed(""));
105106

107+
let default_override =
108+
PythonBindType::DEFAULT_OVERRIDES
109+
.iter()
110+
.find_map(|&(name, field, value)| {
111+
if name == struct_name.as_str() {
112+
Some((field, value))
113+
} else {
114+
None
115+
}
116+
});
117+
106118
Some(Self {
107119
filename,
108120
struct_name,
@@ -113,6 +125,7 @@ impl StructBindGenerator {
113125
has_complex_pack,
114126
is_frozen,
115127
is_no_set,
128+
default_override,
116129
})
117130
}
118131

@@ -297,6 +310,13 @@ impl StructBindGenerator {
297310
for variable_info in &self.types {
298311
let variable_name = variable_info.name.as_str();
299312

313+
if let Some((field, value)) = self.default_override {
314+
if field == variable_name {
315+
signature_parts.push(format!("{variable_name}={value}"));
316+
continue;
317+
}
318+
}
319+
300320
let sig_part = match &variable_info.rust_type {
301321
RustType::Option(_, _) => {
302322
if variable_info.is_special_base.is_some() {
@@ -955,6 +975,13 @@ impl Generator for StructBindGenerator {
955975
for variable_info in &self.types {
956976
let variable_name = variable_info.name.as_str();
957977

978+
if let Some((field, value)) = self.default_override {
979+
if field == variable_name {
980+
write_fmt!(self, " {variable_name}: {value},");
981+
continue;
982+
}
983+
}
984+
958985
let end = match &variable_info.rust_type {
959986
RustType::Vec(InnerVecType::U8) => Cow::Borrowed("PyBytes::new(py, &[]).unbind()"),
960987
RustType::Vec(InnerVecType::Custom(_)) => {

pytest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ def random_script_config():
8282
Color(255),
8383
)
8484
)
85+
8586
if isinstance(render_type.variety.item, Line3D):
87+
assert render_type.variety.item.color.r == 255
88+
assert render_type.variety.item.color.a == 255
8689
render_type.variety.item.color.a = 150
90+
assert render_type.variety.item.color.a == 150
8791
else:
8892
raise ValueError("Expected Line3D")
8993

0 commit comments

Comments
 (0)