/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ //! Helpers for data returned by cargo_metadata. Note that this doesn't //! execute cargo_metadata, just parses its output. use anyhow::{bail, Context}; use camino::Utf8PathBuf; use cargo_metadata::Metadata; use std::{collections::HashMap, fs}; use crate::BindgenCrateConfigSupplier; #[derive(Debug, Clone, Default)] pub struct CrateConfigSupplier { paths: HashMap, } impl BindgenCrateConfigSupplier for CrateConfigSupplier { fn get_toml(&self, crate_name: &str) -> anyhow::Result> { crate::load_toml_file(self.get_toml_path(crate_name).as_deref()) } fn get_toml_path(&self, crate_name: &str) -> Option { self.paths.get(crate_name).map(|p| p.join("uniffi.toml")) } fn get_udl(&self, crate_name: &str, udl_name: &str) -> anyhow::Result { let path = self .paths .get(crate_name) .context(format!("No path known to UDL files for '{crate_name}'"))? .join("src") .join(format!("{udl_name}.udl")); if path.exists() { Ok(fs::read_to_string(path)?) } else { bail!(format!("No UDL file found at '{path}'")); } } } impl From for CrateConfigSupplier { fn from(metadata: Metadata) -> Self { let paths: HashMap = metadata .packages .iter() .flat_map(|p| { p.targets .iter() .filter(|t| { !t.is_bin() && !t.is_example() && !t.is_test() && !t.is_bench() && !t.is_custom_build() }) .filter_map(|t| { p.manifest_path .parent() .map(|p| (t.name.replace('-', "_"), p.to_owned())) }) }) .collect(); Self { paths } } }