Skip to content

Commit 429fc10

Browse files
committed
Formatting
1 parent 7eaa639 commit 429fc10

2 files changed

Lines changed: 56 additions & 44 deletions

File tree

build.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ impl PythonBindGenerator {
668668
let snake_case_name = &variable_info[2];
669669

670670
self.file_contents.push(Cow::Owned(format!(
671-
" {}Type::{variable_value} => flat::{}::{variable_name}(Box::from(&*borrow.{snake_case_name}.as_ref().unwrap())),",
671+
" {}Type::{variable_value} => flat::{}::{variable_name}(Box::from(borrow.{snake_case_name}.as_ref().unwrap())),",
672672
self.struct_name,
673673
self.struct_t_name,
674674
)));
@@ -780,55 +780,62 @@ impl PythonBindGenerator {
780780
}
781781

782782
self.write_string(format!("impl From<&Py<{}>> for {impl_type} {{", self.struct_name));
783-
self.write_string(format!(" fn from(py_type: &Py<{}>) -> Self {{", self.struct_name));
784-
self.write_str(" Python::with_gil(|py| {");
785-
self.write_str(" let borrow = py_type.borrow(py);");
786783

787-
if is_box_type {
788-
self.write_string(format!(" Self::new(flat::{} {{", self.struct_t_name));
784+
if self.types.is_empty() {
785+
self.write_string(format!(" fn from(_: &Py<{}>) -> Self {{", self.struct_name));
786+
self.write_str(" Self::default()");
789787
} else {
790-
self.write_str(" Self {");
791-
}
788+
self.write_string(format!(" fn from(py_type: &Py<{}>) -> Self {{", self.struct_name));
789+
self.write_str(" Python::with_gil(|py| {");
790+
self.write_str(" let borrow = py_type.borrow(py);");
792791

793-
for variable_info in &self.types {
794-
let variable_name = &variable_info[0];
795-
let variable_type = variable_info[1].as_str();
792+
if is_box_type {
793+
self.write_string(format!(" Self::new(flat::{} {{", self.struct_t_name));
794+
} else {
795+
self.write_str(" Self {");
796+
}
796797

797-
if variable_type.starts_with("Vec<") {
798-
let inner_type = variable_type.trim_start_matches("Vec<").trim_end_matches('>');
799-
if Self::BASE_TYPES.contains(&inner_type) {
798+
for variable_info in &self.types {
799+
let variable_name = &variable_info[0];
800+
let variable_type = variable_info[1].as_str();
801+
802+
if variable_type.starts_with("Vec<") {
803+
let inner_type = variable_type.trim_start_matches("Vec<").trim_end_matches('>');
804+
if Self::BASE_TYPES.contains(&inner_type) {
805+
self.file_contents
806+
.push(Cow::Owned(format!(" {variable_name}: borrow.{variable_name},")));
807+
} else {
808+
self.file_contents.push(Cow::Owned(format!(
809+
" {variable_name}: borrow.{variable_name}.iter().map(Into::into).collect(),",
810+
)));
811+
}
812+
} else if variable_type.starts_with("Option<") {
813+
self.file_contents.push(Cow::Owned(format!(
814+
" {variable_name}: borrow.{variable_name}.as_ref().map(Into::into),",
815+
)));
816+
} else if variable_type == "String" {
817+
self.file_contents.push(Cow::Owned(format!(
818+
" {variable_name}: borrow.{variable_name}.clone(),",
819+
)));
820+
} else if Self::BASE_TYPES.contains(&variable_type) {
800821
self.file_contents
801822
.push(Cow::Owned(format!(" {variable_name}: borrow.{variable_name},")));
802823
} else {
803824
self.file_contents.push(Cow::Owned(format!(
804-
" {variable_name}: borrow.{variable_name}.iter().map(Into::into).collect(),",
825+
" {variable_name}: (&borrow.{variable_name}).into(),",
805826
)));
806827
}
807-
} else if variable_type.starts_with("Option<") {
808-
self.file_contents.push(Cow::Owned(format!(
809-
" {variable_name}: borrow.{variable_name}.as_ref().map(Into::into),",
810-
)));
811-
} else if variable_type == "String" {
812-
self.file_contents.push(Cow::Owned(format!(
813-
" {variable_name}: borrow.{variable_name}.clone(),",
814-
)));
815-
} else if Self::BASE_TYPES.contains(&variable_type) {
816-
self.file_contents
817-
.push(Cow::Owned(format!(" {variable_name}: borrow.{variable_name},")));
828+
}
829+
830+
if is_box_type {
831+
self.write_str(" })");
818832
} else {
819-
self.file_contents.push(Cow::Owned(format!(
820-
" {variable_name}: (&borrow.{variable_name}).into(),",
821-
)));
833+
self.write_str(" }");
822834
}
823-
}
824835

825-
if is_box_type {
826836
self.write_str(" })");
827-
} else {
828-
self.write_str(" }");
829837
}
830838

831-
self.write_str(" })");
832839
self.write_str(" }");
833840
self.write_str("}");
834841
self.write_str("");
@@ -1084,8 +1091,10 @@ impl PythonBindGenerator {
10841091
let variable_name = &variable_info[0];
10851092
let variable_type = variable_info[1].as_str();
10861093

1087-
if variable_type.starts_with("Vec<") {
1094+
if variable_type == "String" {
10881095
format!("{variable_name}={{:?}}")
1096+
} else if variable_type.starts_with("Vec<") {
1097+
format!("{variable_name}=[{{}}]")
10891098
} else {
10901099
format!("{variable_name}={{}}")
10911100
}
@@ -1119,7 +1128,8 @@ impl PythonBindGenerator {
11191128
self.file_contents
11201129
.push(Cow::Borrowed(" .map(|x| x.borrow(py).__repr__(py))"));
11211130
self.file_contents
1122-
.push(Cow::Borrowed(" .collect::<Box<[_]>>(),"));
1131+
.push(Cow::Borrowed(" .collect::<Vec<String>>()"));
1132+
self.file_contents.push(Cow::Borrowed(" .join(\", \"),"));
11231133
} else if variable_type.ends_with('T') || variable_type.starts_with("Box<") {
11241134
self.file_contents.push(Cow::Owned(format!(
11251135
" self.{variable_name}.borrow(py).__repr__(py),"

pytest.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
color = Color(255, 0, 0)
77
print(repr(color))
88
print(color)
9+
eval(repr(color))
910
print()
1011

1112
controller = ControllerState(throttle=1)
@@ -16,29 +17,30 @@
1617
ready_message = ReadyMessage(True, wants_game_messages=True)
1718
print(repr(ready_message))
1819
print(ready_message)
20+
eval(repr(ready_message))
1921
print()
2022

21-
print(repr(StopCommand()))
22-
print(StopCommand(True))
23-
print()
24-
25-
dgs = DesiredGameState(game_info_state=DesiredGameInfoState(game_speed=Float(1), end_match=Bool()))
23+
dgs = DesiredGameState(
24+
game_info_state=DesiredGameInfoState(game_speed=Float(1), end_match=Bool())
25+
)
2626
dgs.game_info_state.world_gravity_z = Float(-650)
2727
dgs.game_info_state.end_match.val = True
28-
dgs.console_commands.append(ConsoleCommand("freeze"))
28+
dgs.console_commands = [ConsoleCommand("freeze")]
2929
dgs.ball_state = DesiredBallState()
3030

3131
print(repr(dgs))
3232
print(dgs)
33+
eval(repr(dgs))
3334
print()
3435

36+
print(repr(RenderType()))
37+
3538
render_type = RenderType(Line3D(Vector3(0, 0, 0), Vector3(1, 1, 1), Color(255)))
3639
render_type.line_3_d.color.a = 150
3740

38-
print(repr(RenderType()))
39-
4041
print(repr(render_type))
4142
print(render_type)
43+
eval(repr(render_type))
4244
print()
4345

4446
num_trials = 1_000_000

0 commit comments

Comments
 (0)