add cargo manifest reflection

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-24 09:10:01 +00:00
parent f014231644
commit 936d2915e2
8 changed files with 142 additions and 0 deletions

View file

@ -53,8 +53,10 @@ sha256_media = []
argon2.workspace = true
axum.workspace = true
bytes.workspace = true
cargo_toml.workspace = true
checked_ops.workspace = true
chrono.workspace = true
conduit-macros.workspace = true
const-str.workspace = true
either.workspace = true
figment.workspace = true

View file

@ -55,6 +55,8 @@ pub enum Error {
Http(#[from] http::Error),
#[error("{0}")]
HttpHeader(#[from] http::header::InvalidHeaderValue),
#[error("{0}")]
CargoToml(#[from] cargo_toml::Error),
// ruma
#[error("{0}")]

66
src/core/info/cargo.rs Normal file
View file

@ -0,0 +1,66 @@
//! Information about the build related to Cargo. This is a frontend interface
//! informed by proc-macros that capture raw information at build time which is
//! further processed at runtime either during static initialization or as
//! necessary.
use std::sync::OnceLock;
use cargo_toml::Manifest;
use conduit_macros::cargo_manifest;
use crate::Result;
// Raw captures of the cargo manifest for each crate. This is provided by a
// proc-macro at build time since the source directory and the cargo toml's may
// not be present during execution.
#[cargo_manifest]
const WORKSPACE_MANIFEST: &'static str = ();
#[cargo_manifest("macros")]
const MACROS_MANIFEST: &'static str = ();
#[cargo_manifest("core")]
const CORE_MANIFEST: &'static str = ();
#[cargo_manifest("database")]
const DATABASE_MANIFEST: &'static str = ();
#[cargo_manifest("service")]
const SERVICE_MANIFEST: &'static str = ();
#[cargo_manifest("admin")]
const ADMIN_MANIFEST: &'static str = ();
#[cargo_manifest("router")]
const ROUTER_MANIFEST: &'static str = ();
#[cargo_manifest("main")]
const MAIN_MANIFEST: &'static str = ();
/// Processed list of features access all project crates. This is generated from
/// the data in the MANIFEST strings and contains all possible project features.
/// For *enabled* features see the info::rustc module instead.
static FEATURES: OnceLock<Vec<String>> = OnceLock::new();
/// List of all possible features for the project. For *enabled* features in
/// this build see the companion function in info::rustc.
pub fn features() -> &'static Vec<String> {
FEATURES.get_or_init(|| init_features().unwrap_or_else(|e| panic!("Failed initialize features: {e}")))
}
fn init_features() -> Result<Vec<String>> {
let mut features = Vec::new();
append_features(&mut features, WORKSPACE_MANIFEST)?;
append_features(&mut features, MACROS_MANIFEST)?;
append_features(&mut features, CORE_MANIFEST)?;
append_features(&mut features, DATABASE_MANIFEST)?;
append_features(&mut features, SERVICE_MANIFEST)?;
append_features(&mut features, ADMIN_MANIFEST)?;
append_features(&mut features, ROUTER_MANIFEST)?;
append_features(&mut features, MAIN_MANIFEST)?;
features.sort();
features.dedup();
Ok(features)
}
fn append_features(features: &mut Vec<String>, manifest: &str) -> Result<()> {
let manifest = Manifest::from_str(manifest)?;
features.extend(manifest.features.keys().cloned());
Ok(())
}

View file

@ -1,4 +1,5 @@
//! Information about the project. This module contains version, build, system,
//! etc information which can be queried by admins or used by developers.
pub mod cargo;
pub mod version;