Mercurial > python-compiler.rs
comparison src/python_dump.rs @ 1:b90e49ab734b
Factorise conversion of Python str into Rust String.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 May 2016 00:52:00 +0100 |
parents | 211b0df72e64 |
children | ddf372373a77 |
comparison
equal
deleted
inserted
replaced
0:211b0df72e64 | 1:b90e49ab734b |
---|---|
50 } | 50 } |
51 map | 51 map |
52 }; | 52 }; |
53 */ | 53 */ |
54 | 54 |
55 fn get_str(py: Python, object: PyObject) -> String { | |
56 let pystring = object.str(py).unwrap(); | |
57 let mut string = pystring.to_string(py).unwrap(); | |
58 string.to_mut().to_string() | |
59 } | |
60 | |
55 if is_instance(&ast, &function_def_type) { | 61 if is_instance(&ast, &function_def_type) { |
56 let name = ast.getattr(py, "name").unwrap(); | 62 let name = ast.getattr(py, "name").unwrap(); |
57 let args = ast.getattr(py, "args").unwrap(); | 63 let args = ast.getattr(py, "args").unwrap(); |
58 let body = ast.getattr(py, "body").unwrap(); | 64 let body = ast.getattr(py, "body").unwrap(); |
59 let args = dump(py, indent, args); | 65 let args = dump(py, indent, args); |
179 format!("from {} import {}", module, arguments.join(", ")) | 185 format!("from {} import {}", module, arguments.join(", ")) |
180 } else if is_instance(&ast, &alias_type) { | 186 } else if is_instance(&ast, &alias_type) { |
181 let name = ast.getattr(py, "name").unwrap(); | 187 let name = ast.getattr(py, "name").unwrap(); |
182 let asname = ast.getattr(py, "asname").unwrap(); | 188 let asname = ast.getattr(py, "asname").unwrap(); |
183 | 189 |
184 let name = { | 190 let name = get_str(py, name); |
185 let name = name.str(py).unwrap(); | 191 let asname = get_str(py, asname); |
186 let mut name = name.to_string(py).unwrap(); | |
187 name.to_mut().to_string() | |
188 }; | |
189 | |
190 let asname = { | |
191 let asname = asname.str(py).unwrap(); | |
192 let mut asname = asname.to_string(py).unwrap(); | |
193 asname.to_mut().to_string() | |
194 }; | |
195 | 192 |
196 if asname == "None" { | 193 if asname == "None" { |
197 format!("{}", name) | 194 format!("{}", name) |
198 } else { | 195 } else { |
199 format!("{} as {}", name, asname) | 196 format!("{} as {}", name, asname) |