Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/python/mod.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use std::process::Command;
6
7 use anyhow::Result;
8 use fs_err as fs;
9
10 mod pipeline;
11 pub use pipeline::pipeline;
12
13 mod gen_python;
14 #[cfg(feature = "bindgen-tests")]
15 pub mod test;
16 use crate::{BindingGenerator, Component, GenerationSettings};
17
18 use gen_python::{generate_python_bindings, Config};
19
20 pub struct PythonBindingGenerator;
21
22 impl BindingGenerator for PythonBindingGenerator {
23 type Config = Config;
24
25 fn new_config(&self, root_toml: &toml::Value) -> Result<Self::Config> {
26 Ok(
27 match root_toml.get("bindings").and_then(|b| b.get("python")) {
28 Some(v) => v.clone().try_into()?,
29 None => Default::default(),
30 },
31 )
32 }
33
34 fn update_component_configs(
35 &self,
36 settings: &GenerationSettings,
37 components: &mut Vec<Component<Self::Config>>,
38 ) -> Result<()> {
39 for c in &mut *components {
40 c.config.cdylib_name.get_or_insert_with(|| {
41 settings
42 .cdylib
43 .clone()
44 .unwrap_or_else(|| format!("uniffi_{}", c.ci.namespace()))
45 });
46 }
47 Ok(())
48 }
49
50 fn write_bindings(
51 &self,
52 settings: &GenerationSettings,
53 components: &[Component<Self::Config>],
54 ) -> Result<()> {
55 for Component { ci, config, .. } in components {
56 let py_file = settings.out_dir.join(format!("{}.py", ci.namespace()));
57 fs::write(&py_file, generate_python_bindings(config, &mut ci.clone())?)?;
58
59 if settings.try_format_code {
60 if let Err(e) = Command::new("yapf").arg(&py_file).output() {
61 println!(
62 "Warning: Unable to auto-format {} using yapf: {e:?}",
63 py_file.file_name().unwrap(),
64 )
65 }
66 }
67 }
68
69 Ok(())
70 }
71 }