Repository navigation
Expand file tree
/
Copy pathpython_type.rs
More file actions
158 lines (142 loc) · 5.53 KB
/
Copy pathpython_type.rs
File metadata and controls
158 lines (142 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#[cfg(test)]
use crate::mock;
use crate::options::TypeOverrideOptions;
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, PartialOrd, Ord)]
pub struct PythonType {
pub declaration: Option<String>,
pub constructor: String,
#[serde(default)]
pub annotation: String,
#[serde(default)]
pub import: Option<String>,
#[serde(default)]
pub encode: Option<String>,
#[serde(default)]
pub decode: Option<String>,
}
macro_rules! new_type {
($name:literal) => {
new_type!($name, None)
};
($name:literal,$import:tt) => {{
let import: Option<&'static str> = Option::from($import);
crate::python_type::PythonType {
declaration: None,
constructor: $name.into(),
annotation: $name.into(),
import: import.map(String::from),
encode: None,
decode: None,
}
}};
($name:literal,$annotation:literal, $import:tt) => {{
let import: Option<&'static str> = Option::from($import);
crate::python_type::PythonType {
declaration: None,
constructor: $name.into(),
annotation: $annotation.into(),
import: import.map(String::from),
encode: None,
decode: None,
}
}};
}
impl PythonType {
pub fn from_pg_type(name: &str) -> Self {
match name {
"anycompatiblearray" | "anyarray" => {
new_type!("list[typing.Any]", "import typing")
}
"anyrange" => new_type!("asyncpg.Range", "import asyncpg"),
"anymultirange" => new_type!("list[asyncpg.Range]", "import asyncpg"),
"record" => new_type!("asyncpg.Record", "import asyncpg"),
"bit" | "varbit" => new_type!("asyncpg.BitString", "import asyncpg"),
"bool" | "boolean" => new_type!("bool"),
"box" => new_type!("asyncpg.Box", "import asyncpg"),
"bytea" => new_type!("bytes"),
"macaddr" | "char" | "name" | "varchar" | "text" | "xml" => new_type!("str"),
"cidr" => new_type!(
"ipaddress.IPv4Network | ipaddress.IPv6Network",
"import ipaddress"
),
"circle" => new_type!("asyncpg.types.Circle", "import asyncpg.types"),
"date" => new_type!("datetime.date", "import datetime"),
"time" | "time with time zone" | "timetz" => {
new_type!("datetime.time", "import datetime")
}
"timestamp" | "timestamp with time zone" | "timestamptz" => {
new_type!("datetime.datetime", "import datetime")
}
"interval" => new_type!("datetime.timedelta", "import datetime"),
"float" | "double" | "precision" => new_type!("float"),
"pg_catalog.int8" | "pg_catalog.int4" | "int4" | "int8" | "smallint" | "integer"
| "bigint" => {
new_type!("int")
}
"pg_catalog.numeric" | "numeric" => new_type!("decimal.Decimal", "import decimal"),
"anyenum" | "json" | "jsonb" | "money" => new_type!("str"),
"line" => new_type!("asyncpg.Line", "import asyncpg"),
"lseg" => new_type!("asyncpg.LineSegment", "import asyncpg"),
"path" => new_type!("asyncpg.types.Path", "import asyncpg.types"),
"point" => new_type!("asyncpg.types.Point", "import asyncpg.types"),
"polygon" => new_type!("asyncpg.types.Polygon", "import asyncpg.types"),
"uuid" => new_type!("uuid.UUID", "import uuid"),
"tid" => new_type!("tuple"),
"any" => new_type!("typing.Any", "import typing"),
"tstzrange" | "tsrange" => new_type!(
"asyncpg.types.Range",
"asyncpg.types.Range[datetime.datetime]",
"import asyncpg\nimport datetime"
),
"daterange" => new_type!(
"asyncpg.types.Range",
"asyncpg.types.Range[datetime.date]",
"import asyncpg\nimport datetime"
),
"int4range" | "int8range" => new_type!(
"asyncpg.types.Range",
"asyncpg.types.Range[int]",
"import asyncpg"
),
"numrange" => new_type!(
"asyncpg.types.Range",
"asyncpg.types.Range[float]",
"import asyncpg"
),
_ => new_type!("typing.Any", "import typing"),
}
}
pub fn is_any(&self) -> bool {
self.annotation == "typing.Any"
}
}
impl From<TypeOverrideOptions> for PythonType {
fn from(value: TypeOverrideOptions) -> Self {
PythonType {
constructor: value.python_type.clone(),
declaration: None,
annotation: value.python_type,
encode: value.encode.clone(),
decode: value.decode.clone(),
import: value.import,
}
}
}
#[test]
fn parse_type_from_json() {
let json = r#"{ "name": "dict" }"#;
let type_option = serde_json::from_str::<TypeOverrideOptions>(json).unwrap();
let _ = PythonType::from(type_option);
}
#[test]
fn test_type_imports() {
let json = r#"{ "name": "uuid.UUID", "import": "import uuid" }"#;
let type_option = serde_json::from_str::<TypeOverrideOptions>(json).unwrap();
let type_ = PythonType::from(type_option);
assert_eq!(type_.import.as_deref(), Some("import uuid"));
}
#[test]
fn test_type_new() {
let r#type = new_type!("uuid.UUID", "import, uuid");
assert_eq!(r#type.constructor, "uuid.UUID")
}