Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/ruby/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 crate::{BindingGenerator, Component, ComponentInterface, GenerationSettings};
8 use anyhow::{Context, Result};
9 use fs_err as fs;
10
11 mod gen_ruby;
12 #[cfg(feature = "bindgen-tests")]
13 pub mod test;
14 use gen_ruby::{Config, RubyWrapper};
15
16 pub struct RubyBindingGenerator;
17 impl BindingGenerator for RubyBindingGenerator {
18 type Config = Config;
19
20 fn new_config(&self, root_toml: &toml::Value) -> Result<Self::Config> {
21 Ok(
22 match root_toml.get("bindings").and_then(|b| b.get("ruby")) {
23 Some(v) => v.clone().try_into()?,
24 None => Default::default(),
25 },
26 )
27 }
28
29 fn update_component_configs(
30 &self,
31 settings: &GenerationSettings,
32 components: &mut Vec<Component<Self::Config>>,
33 ) -> Result<()> {
34 for c in &mut *components {
35 c.config.cdylib_name.get_or_insert_with(|| {
36 settings
37 .cdylib
38 .clone()
39 .unwrap_or_else(|| format!("uniffi_{}", c.ci.namespace()))
40 });
41 }
42 Ok(())
43 }
44
45 fn write_bindings(
46 &self,
47 settings: &GenerationSettings,
48 components: &[Component<Self::Config>],
49 ) -> Result<()> {
50 for Component { ci, config, .. } in components {
51 let rb_file = settings.out_dir.join(format!("{}.rb", ci.namespace()));
52 fs::write(&rb_file, generate_ruby_bindings(config, ci)?)?;
53
54 if settings.try_format_code {
55 if let Err(e) = Command::new("rubocop").arg("-A").arg(&rb_file).output() {
56 println!(
57 "Warning: Unable to auto-format {} using rubocop: {e:?}",
58 rb_file.file_name().unwrap(),
59 )
60 }
61 }
62 }
63 Ok(())
64 }
65 }
66
67 // Generate ruby bindings for the given ComponentInterface, as a string.
68 pub fn generate_ruby_bindings(config: &Config, ci: &ComponentInterface) -> Result<String> {
69 use askama::Template;
70 RubyWrapper::new(config.clone(), ci)
71 .render()
72 .context("failed to render ruby bindings")
73 }