Skip to content

Commit f2b8437

Browse files
committed
Make __str__ return the output of __repr__
Support for `Vec<String>` type
1 parent 534a050 commit f2b8437

5 files changed

Lines changed: 27 additions & 8 deletions

File tree

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.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rlbot-flatbuffers-py"
3-
version = "0.4.3"
3+
version = "0.5.0"
44
edition = "2021"
55
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
66
repository = "https://github.com/VirxEC/rlbot_flatbuffers_py"

codegen/pyi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
107107
python_types.push("bytes".to_string());
108108
write_fmt!(file, " {variable_name}: bytes");
109109
}
110+
RustType::Vec(InnerVecType::String) => {
111+
python_types.push("Sequence[str]".to_string());
112+
write_fmt!(file, " {variable_name}: Sequence[str]");
113+
}
110114
RustType::Vec(InnerVecType::Custom(inner_type)) => {
111115
python_types.push(format!("Sequence[{inner_type}]"));
112116
write_fmt!(file, " {variable_name}: Sequence[{inner_type}]");

codegen/structs.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{borrow::Cow, fs, iter::repeat, path::Path};
44
#[derive(Debug, PartialEq, Eq)]
55
pub enum InnerVecType {
66
U8,
7+
String,
78
Custom(String),
89
}
910

@@ -157,6 +158,8 @@ impl StructBindGenerator {
157158
let (rust_type, inner_type) = if raw_type.starts_with("Vec<") {
158159
if raw_type == "Vec<u8>" {
159160
(RustType::Vec(InnerVecType::U8), None)
161+
} else if raw_type == "Vec<String>" {
162+
(RustType::Vec(InnerVecType::String), None)
160163
} else {
161164
let inner_type = raw_type
162165
.trim_start_matches("Vec<")
@@ -309,6 +312,7 @@ impl StructBindGenerator {
309312
} else {
310313
format!("Vec<Py<super::{inner_type}>>")
311314
}),
315+
RustType::Vec(InnerVecType::String) => Cow::Borrowed("Vec<String>"),
312316
RustType::Vec(InnerVecType::U8) => Cow::Borrowed("Py<PyBytes>"),
313317
RustType::Box(inner_type) => Cow::Owned(if self.is_frozen {
314318
format!("super::{inner_type}")
@@ -375,8 +379,8 @@ impl StructBindGenerator {
375379
}
376380

377381
fn generate_str_method(&mut self) {
378-
write_str!(self, " pub fn __str__(&self) -> String {");
379-
write_str!(self, " format!(\"{self:?}\")");
382+
write_str!(self, " pub fn __str__(&self, py: Python) -> String {");
383+
write_str!(self, " self.__repr__(py)");
380384
write_str!(self, " }");
381385
}
382386

@@ -404,7 +408,7 @@ impl StructBindGenerator {
404408
match &variable_info.rust_type {
405409
RustType::String => format!("{variable_name}={{:?}}"),
406410
RustType::Vec(InnerVecType::U8) => format!("{variable_name}=bytes([{{}}])"),
407-
RustType::Vec(InnerVecType::Custom(_)) => format!("{variable_name}=[{{}}]"),
411+
RustType::Vec(_) => format!("{variable_name}=[{{}}]"),
408412
_ => format!("{variable_name}={{}}"),
409413
}
410414
})
@@ -424,6 +428,10 @@ impl StructBindGenerator {
424428
write_str!(self, " .iter()");
425429
write_str!(self, " .map(ToString::to_string)");
426430
}
431+
InnerVecType::String => {
432+
write_str!(self, " .iter()");
433+
write_str!(self, " .map(|s| format!(\"{s:?}\"))");
434+
}
427435
InnerVecType::Custom(_) => {
428436
write_str!(self, " .iter()");
429437
write_str!(
@@ -665,6 +673,7 @@ impl Generator for StructBindGenerator {
665673

666674
let variable_type = match &variable_info.rust_type {
667675
RustType::Vec(InnerVecType::U8) => String::from("Py<PyBytes>"),
676+
RustType::Vec(InnerVecType::String) => String::from("Vec<String>"),
668677
RustType::Vec(InnerVecType::Custom(inner_type)) => {
669678
if self.is_frozen {
670679
format!("Vec<super::{inner_type}>")
@@ -717,7 +726,7 @@ impl Generator for StructBindGenerator {
717726

718727
let end = match &variable_info.rust_type {
719728
RustType::Vec(InnerVecType::U8) => Cow::Borrowed("PyBytes::new_bound(py, &[]).unbind()"),
720-
RustType::Vec(InnerVecType::Custom(_)) => Cow::Borrowed("Vec::new()"),
729+
RustType::Vec(InnerVecType::Custom(_) | InnerVecType::String) => Cow::Borrowed("Vec::new()"),
721730
RustType::Option(_, _) => Cow::Borrowed("None"),
722731
RustType::Union(inner_type) | RustType::Box(inner_type) | RustType::Custom(inner_type) => {
723732
Cow::Owned(format!("super::{inner_type}::py_default(py)"))
@@ -778,6 +787,9 @@ impl Generator for StructBindGenerator {
778787
" {variable_name}: PyBytes::new_bound(py, &flat_t.{variable_name}).unbind(),"
779788
)
780789
}
790+
RustType::Vec(InnerVecType::String) => {
791+
write_fmt!(self, " {variable_name}: flat_t.{variable_name},")
792+
}
781793
RustType::Vec(InnerVecType::Custom(_)) => {
782794
if self.is_frozen {
783795
let map_out = if variable_info.frozen_needs_py {
@@ -895,6 +907,9 @@ impl Generator for StructBindGenerator {
895907
" {variable_name}: py_type.{variable_name}.as_bytes(py).to_vec(),"
896908
)
897909
}
910+
RustType::Vec(InnerVecType::String) => {
911+
write_fmt!(self, " {variable_name}: py_type.{variable_name}.clone(),")
912+
}
898913
RustType::Vec(InnerVecType::Custom(_)) => {
899914
if self.is_frozen {
900915
let map_out = if variable_info.frozen_needs_py {

codegen/unions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ impl UnionBindGenerator {
8383
}
8484

8585
fn generate_str_method(&mut self) {
86-
write_str!(self, " pub fn __str__(&self) -> String {");
87-
write_str!(self, " format!(\"{self:?}\")");
86+
write_str!(self, " pub fn __str__(&self, py: Python) -> String {");
87+
write_str!(self, " self.__repr__(py)");
8888
write_str!(self, " }");
8989
}
9090

0 commit comments

Comments
 (0)