pgrx_pg_config/cargo.rs
1//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
2//LICENSE
3//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
4//LICENSE
5//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
6//LICENSE
7//LICENSE All rights reserved.
8//LICENSE
9//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
10use std::path::Path;
11
12use cargo_toml::Manifest;
13use eyre::eyre;
14
15/// Extension to `cargo_toml::Manifest`.
16/// Import by adding `use pgrx_pg_config::cargo::PgrxManifestExt;`
17/// and extended functions will be available on `Manifest` values.
18pub trait PgrxManifestExt {
19 /// Package name
20 fn package_name(&self) -> eyre::Result<String>;
21
22 /// Package version
23 fn package_version(&self) -> eyre::Result<String>;
24
25 /// Resolved string for target library name, either its lib.name,
26 /// or package name with hyphens replaced with underscore.
27 /// <https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-name-field>
28 fn lib_name(&self) -> eyre::Result<String>;
29
30 /// Resolved string for target library name extension filename
31 fn lib_filename(&self) -> eyre::Result<String>;
32}
33
34impl PgrxManifestExt for Manifest {
35 fn package_name(&self) -> eyre::Result<String> {
36 match &self.package {
37 Some(package) => Ok(package.name.to_owned()),
38 None => Err(eyre!("Could not get [package] from manifest.")),
39 }
40 }
41
42 fn package_version(&self) -> eyre::Result<String> {
43 match &self.package {
44 Some(package) => match &package.version {
45 cargo_toml::Inheritable::Set(version) => Ok(version.to_owned()),
46 // This should be impossible to hit, since we use
47 // `Manifest::from_path`, which calls `complete_from_path`,
48 // which is documented as resolving these. That said, I
49 // haven't tested it, and it's not clear how much it
50 // actually matters either way, so we just emit an error
51 // rather than doing something like `unreachable!()`.
52 cargo_toml::Inheritable::Inherited => {
53 Err(eyre!("Workspace-inherited package version are not currently supported."))
54 }
55 },
56 None => Err(eyre!("Could not get [package] from manifest.")),
57 }
58 }
59
60 fn lib_name(&self) -> eyre::Result<String> {
61 // `cargo_manifest` auto fills lib.name with package.name;
62 // hyphen replaced with underscore if crate type is lib.
63 // So we will always have a lib.name for lib crates.
64 Ok(self
65 .package
66 .as_ref()
67 .and_then(|_| self.lib.as_ref())
68 .map(|lib| lib.name.to_owned())
69 .flatten()
70 .ok_or_else(|| eyre!("Could not get [lib] name from manifest."))?)
71 }
72
73 fn lib_filename(&self) -> eyre::Result<String> {
74 use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
75 let lib_name = &self.lib_name()?;
76 Ok(format!("{DLL_PREFIX}{}{DLL_SUFFIX}", lib_name))
77 }
78}
79
80/// Helper functions to read `Cargo.toml` and remap error to `eyre::Result`.
81pub fn read_manifest<T: AsRef<Path>>(path: T) -> eyre::Result<Manifest> {
82 Manifest::from_path(path).map_err(|err| eyre!("Couldn't parse manifest: {err}"))
83}