Skip to main content

nautilus_codegen/python/
backend.rs

1//! Python-specific implementation of the shared language backend.
2
3use nautilus_schema::ir::ScalarType;
4
5use crate::backend::LanguageBackend;
6
7/// Python language backend.
8pub struct PythonBackend;
9
10impl LanguageBackend for PythonBackend {
11    fn scalar_to_type(&self, scalar: &ScalarType) -> &'static str {
12        match scalar {
13            ScalarType::String => "str",
14            ScalarType::Int => "int",
15            ScalarType::BigInt => "int",
16            ScalarType::Float => "float",
17            ScalarType::Decimal { .. } => "Decimal",
18            ScalarType::Boolean => "bool",
19            ScalarType::DateTime => "datetime",
20            ScalarType::Bytes => "bytes",
21            ScalarType::Json => "Dict[str, Any]",
22            ScalarType::Uuid => "UUID",
23            ScalarType::Jsonb => "Dict[str, Any]",
24            ScalarType::Xml | ScalarType::Char { .. } | ScalarType::VarChar { .. } => "str",
25        }
26    }
27
28    fn array_type(&self, inner: &str) -> String {
29        format!("List[{}]", inner)
30    }
31
32    fn not_in_suffix(&self) -> &'static str {
33        "not_in"
34    }
35
36    fn startswith_suffix(&self) -> &'static str {
37        "startswith"
38    }
39
40    fn endswith_suffix(&self) -> &'static str {
41        "endswith"
42    }
43
44    fn null_suffix(&self) -> &'static str {
45        "is_null"
46    }
47
48    fn null_literal(&self) -> &'static str {
49        "None"
50    }
51
52    fn true_literal(&self) -> &'static str {
53        "True"
54    }
55
56    fn false_literal(&self) -> &'static str {
57        "False"
58    }
59
60    fn string_literal(&self, s: &str) -> String {
61        format!("\"{}\"", s)
62    }
63
64    fn empty_array_literal(&self) -> &'static str {
65        "Field(default_factory=list)"
66    }
67}