Skip to main content

hyprstream_ts_codegen/py_codegen/
mod.rs

1//! Python code generation backend (skeleton).
2//!
3//! Provides `PythonBackend` which implements `CodegenBackend` with Python type
4//! mappings.  A complete Python implementation is out of scope; this skeleton
5//! validates that the `CodegenBackend` interface compiles correctly with a
6//! non-TypeScript implementation.
7
8#![allow(dead_code)]
9
10use hyprstream_rpc_build::backend::CodegenBackend;
11use hyprstream_rpc_build::schema::types::{FieldDef, ParsedSchema};
12
13/// Python code generation backend (skeleton).
14pub struct PythonBackend;
15
16impl CodegenBackend for PythonBackend {
17    fn map_type(&self, capnp_type: &str) -> String {
18        match capnp_type {
19            "Void" => "None".into(),
20            "Bool" => "bool".into(),
21            "UInt8" | "UInt16" | "UInt32" | "UInt64" | "Int8" | "Int16" | "Int32" | "Int64" => {
22                "int".into()
23            }
24            "Float32" | "Float64" => "float".into(),
25            "Text" => "str".into(),
26            "Data" => "bytes".into(),
27            _ if capnp_type.starts_with("List(") => {
28                let inner = &capnp_type[5..capnp_type.len() - 1];
29                format!("list[{}]", self.map_type(inner))
30            }
31            _ => capnp_type.to_owned(),
32        }
33    }
34
35    fn setter_method(&self, _type_name: &str) -> Option<String> {
36        // Not yet implemented
37        None
38    }
39
40    fn getter_expr(&self, reader_var: &str, field: &FieldDef) -> String {
41        // Not yet implemented
42        format!("{reader_var}.{}", field.name)
43    }
44
45    fn default_value(&self, capnp_type: &str) -> String {
46        match capnp_type {
47            "Void" => "None".into(),
48            "Bool" => "False".into(),
49            "Text" => "\"\"".into(),
50            "Data" => "b\"\"".into(),
51            _ if capnp_type.starts_with("List(") => "[]".into(),
52            _ if capnp_type.contains("Int") || capnp_type.contains("Float") => "0".into(),
53            _ => "None".into(),
54        }
55    }
56
57    fn generate_service_file(&self, service_name: &str, _schema: &ParsedSchema) -> String {
58        // Not yet implemented — placeholder comment
59        format!("# Generated for {service_name} — Python codegen not yet implemented\n")
60    }
61
62    fn runtime_file(&self) -> (String, String) {
63        // Not yet implemented
64        (
65            "capnp.py".to_owned(),
66            "# Cap'n Proto Python runtime — not yet implemented\n".to_owned(),
67        )
68    }
69
70    fn file_extension(&self) -> &str {
71        "py"
72    }
73}