From 9760483cc2608ae5f554789464391e95443904a4 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 6 Dec 2026 04:51:18 +0100 Subject: [PATCH] common: make taler-common's database, runtime and crypto parts optional features A program that only reads Taler configuration files and logs (a helper, a converter) then links 85 crates instead of 148 and compiles no C; the default features keep every current user unchanged. Assisted by LLM --- Cargo.toml | 4 +++- adapters/taler-cyclos/Cargo.toml | 2 +- adapters/taler-magnet-bank/Cargo.toml | 2 +- adapters/taler-wise/Cargo.toml | 2 +- common/taler-api/Cargo.toml | 2 +- common/taler-common/Cargo.toml | 17 ++++++++++++++--- common/taler-common/src/api.rs | 3 +++ common/taler-common/src/api/wire.rs | 5 +++-- common/taler-common/src/config.rs | 1 + common/taler-common/src/db.rs | 13 +++++++++++-- common/taler-common/src/lib.rs | 8 ++++++++ common/taler-common/src/log.rs | 3 +++ common/taler-common/src/types/amount.rs | 8 ++++++++ common/taler-common/src/types/base32.rs | 3 +++ common/taler-common/src/types/time.rs | 3 +++ taler-apns-relay/Cargo.toml | 2 +- 16 files changed, 65 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dde1cfe..aba7996 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,7 +57,9 @@ anyhow = "1" http-body-util = "0.1.2" owo-colors = "4.2.3" aws-lc-rs = { version = "1.15", default-features = false, features = ["alloc", "aws-lc-sys"] } -compact_str = { version = "0.10", features = ["serde", "sqlx-postgres"] } +# The members that bind it in queries add "sqlx-postgres" (taler-common with +# its feature "db"). +compact_str = { version = "0.10", features = ["serde"] } hyper-util = "0.1" hyper-rustls = { version = "0.27", features = [ "aws-lc-rs", diff --git a/adapters/taler-cyclos/Cargo.toml b/adapters/taler-cyclos/Cargo.toml index 559e985..5546d8a 100644 --- a/adapters/taler-cyclos/Cargo.toml +++ b/adapters/taler-cyclos/Cargo.toml @@ -29,7 +29,7 @@ serde_path_to_error.workspace = true thiserror.workspace = true tracing.workspace = true tokio.workspace = true -compact_str.workspace = true +compact_str = { workspace = true, features = ["sqlx-postgres"] } anyhow.workspace = true owo-colors.workspace = true failure-injection.workspace = true diff --git a/adapters/taler-magnet-bank/Cargo.toml b/adapters/taler-magnet-bank/Cargo.toml index 5ba6234..85e9ef3 100644 --- a/adapters/taler-magnet-bank/Cargo.toml +++ b/adapters/taler-magnet-bank/Cargo.toml @@ -39,7 +39,7 @@ failure-injection.workspace = true http.workspace = true url.workspace = true aws-lc-rs.workspace = true -compact_str.workspace = true +compact_str = { workspace = true, features = ["sqlx-postgres"] } prometheus-client.workspace = true [dev-dependencies] diff --git a/adapters/taler-wise/Cargo.toml b/adapters/taler-wise/Cargo.toml index f22bb87..6711413 100644 --- a/adapters/taler-wise/Cargo.toml +++ b/adapters/taler-wise/Cargo.toml @@ -14,7 +14,7 @@ doctest = false [dependencies] pem = "4" clap.workspace = true -compact_str.workspace = true +compact_str = { workspace = true, features = ["sqlx-postgres"] } jiff.workspace = true sqlx.workspace = true taler-api.workspace = true diff --git a/common/taler-api/Cargo.toml b/common/taler-api/Cargo.toml index 21b411b..a5bde16 100644 --- a/common/taler-api/Cargo.toml +++ b/common/taler-api/Cargo.toml @@ -39,7 +39,7 @@ taler-common.workspace = true sqlx.workspace = true jiff.workspace = true aws-lc-rs.workspace = true -compact_str.workspace = true +compact_str = { workspace = true, features = ["sqlx-postgres"] } regex.workspace = true prometheus-client.workspace = true diff --git a/common/taler-common/Cargo.toml b/common/taler-common/Cargo.toml index f64ac11..04c5faf 100644 --- a/common/taler-common/Cargo.toml +++ b/common/taler-common/Cargo.toml @@ -30,13 +30,24 @@ tracing.workspace = true clap.workspace = true anyhow.workspace = true tracing-subscriber.workspace = true -tokio = { workspace = true, features = ["rt-multi-thread"] } -sqlx = { workspace = true, features = ["derive"] } +tokio = { workspace = true, features = ["rt-multi-thread"], optional = true } +sqlx = { workspace = true, features = ["derive"], optional = true } compact_str.workspace = true -aws-lc-rs.workspace = true +aws-lc-rs = { workspace = true, optional = true } regex.workspace = true taler-macros.workspace = true +[features] +default = ["db", "runtime", "crypto"] +# The PostgreSQL side: the `db` module's pool and migrations, the sqlx +# encoding of the types, and `Section::postgres`. +db = ["dep:sqlx", "compact_str/sqlx-postgres"] +# `taler_main` and the task ids of the log, on a tokio runtime. +runtime = ["dep:tokio"] +# Ed25519 and SHA-512 with aws-lc-rs: the `api` and `signature` modules and +# `Amount::signature_bytes`. +crypto = ["dep:aws-lc-rs"] + [dev-dependencies] criterion.workspace = true diff --git a/common/taler-common/src/api.rs b/common/taler-common/src/api.rs index 74dea70..9ac3507 100644 --- a/common/taler-common/src/api.rs +++ b/common/taler-common/src/api.rs @@ -230,12 +230,14 @@ impl EddsaPublicKey { } } +#[cfg(feature = "db")] impl sqlx::Type for EddsaPublicKey { fn type_info() -> sqlx::postgres::PgTypeInfo { >::type_info() } } +#[cfg(feature = "db")] impl<'q> sqlx::Encode<'q, sqlx::Postgres> for EddsaPublicKey { fn encode_by_ref( &self, @@ -245,6 +247,7 @@ impl<'q> sqlx::Encode<'q, sqlx::Postgres> for EddsaPublicKey { } } +#[cfg(feature = "db")] impl<'r> sqlx::Decode<'r, sqlx::Postgres> for EddsaPublicKey { fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result { let raw = >::decode(value)?; diff --git a/common/taler-common/src/api/wire.rs b/common/taler-common/src/api/wire.rs index ee541c6..0b8f39c 100644 --- a/common/taler-common/src/api/wire.rs +++ b/common/taler-common/src/api/wire.rs @@ -186,9 +186,10 @@ pub struct AddMappedRequest { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AccountInfo {} -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, sqlx::Type)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] +#[cfg_attr(feature = "db", derive(sqlx::Type))] #[allow(non_camel_case_types)] -#[sqlx(type_name = "transfer_status")] +#[cfg_attr(feature = "db", sqlx(type_name = "transfer_status"))] pub enum TransferState { pending, transient_failure, diff --git a/common/taler-common/src/config.rs b/common/taler-common/src/config.rs index 1e73f6d..27be893 100644 --- a/common/taler-common/src/config.rs +++ b/common/taler-common/src/config.rs @@ -994,6 +994,7 @@ impl<'cfg, 'arg> Section<'cfg, 'arg> { } /** Access [option] as Postgres URI */ + #[cfg(feature = "db")] pub fn postgres(&self, option: &'arg str) -> Value<'arg, sqlx::postgres::PgConnectOptions> { self.parse("Postgres URI", option) } diff --git a/common/taler-common/src/db.rs b/common/taler-common/src/db.rs index e57a674..02ab9c9 100644 --- a/common/taler-common/src/db.rs +++ b/common/taler-common/src/db.rs @@ -14,22 +14,26 @@ TALER; see the file COPYING. If not, see */ +#[cfg(feature = "db")] use std::{ io::ErrorKind, path::{Path, PathBuf}, sync::Arc, }; +#[cfg(feature = "db")] use sqlx::{ AssertSqlSafe, Connection, PgConnection, PgPool, Postgres, Row, Transaction, postgres::{PgConnectOptions, PgPoolOptions, PgRow}, }; use taler_macros::EnumMeta; +#[cfg(feature = "db")] use tracing::{debug, info}; -#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type, EnumMeta)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumMeta)] +#[cfg_attr(feature = "db", derive(sqlx::Type))] #[allow(non_camel_case_types)] -#[sqlx(type_name = "incoming_type")] +#[cfg_attr(feature = "db", sqlx(type_name = "incoming_type"))] #[enum_meta(Str)] pub enum IncomingType { reserve, @@ -39,10 +43,12 @@ pub enum IncomingType { /* ----- Pool ----- */ +#[cfg(feature = "db")] pub async fn pool(cfg: PgConnectOptions, schema: &str) -> sqlx::Result { pool_with_options(cfg, schema, PgPoolOptions::new()).await } +#[cfg(feature = "db")] pub async fn pool_with_options( cfg: PgConnectOptions, schema: &str, @@ -70,6 +76,7 @@ pub async fn pool_with_options( /* ----- Migration----- */ +#[cfg(feature = "db")] #[derive(Debug, thiserror::Error)] pub enum MigrationErr { #[error("could not read patch at '{}': {}", .0.to_string_lossy(), .1.kind())] @@ -78,6 +85,7 @@ pub enum MigrationErr { Sql(#[from] sqlx::Error), } +#[cfg(feature = "db")] pub async fn dbinit( conn: &mut PgConnection, sql_dir: &Path, @@ -87,6 +95,7 @@ pub async fn dbinit( dbinit_setup(conn, sql_dir, prefix, reset, async |_| Ok(())).await } +#[cfg(feature = "db")] pub async fn dbinit_setup( conn: &mut PgConnection, sql_dir: &Path, diff --git a/common/taler-common/src/lib.rs b/common/taler-common/src/lib.rs index 5481072..1adf04b 100644 --- a/common/taler-common/src/lib.rs +++ b/common/taler-common/src/lib.rs @@ -16,13 +16,19 @@ use std::{path::PathBuf, time::Duration}; +#[cfg(feature = "runtime")] use config::{Config, parser::ConfigSource}; +#[cfg(feature = "runtime")] use tracing::error; +#[cfg(feature = "runtime")] use tracing_subscriber::util::SubscriberInitExt; +#[cfg(feature = "runtime")] use crate::log::taler_logger; +#[cfg(feature = "crypto")] pub mod api; +#[cfg(feature = "db")] pub mod bench; pub mod cli; pub mod config; @@ -32,6 +38,7 @@ pub mod error; pub mod error_code; pub mod json_file; pub mod log; +#[cfg(feature = "crypto")] pub mod signature; pub mod types; @@ -55,6 +62,7 @@ pub struct CommonArgs { #[error("{0}")] pub struct PermanentErr(String); +#[cfg(feature = "runtime")] pub fn taler_main( src: ConfigSource, args: CommonArgs, diff --git a/common/taler-common/src/log.rs b/common/taler-common/src/log.rs index 2f8ac06..4f0dd4b 100644 --- a/common/taler-common/src/log.rs +++ b/common/taler-common/src/log.rs @@ -16,6 +16,7 @@ use std::io::IsTerminal; +#[cfg(feature = "runtime")] use compact_str::CompactString; use jiff::{Timestamp, fmt::StdFmtWrite, tz::TimeZone}; use tracing::{Event, Level, Subscriber}; @@ -26,6 +27,7 @@ use tracing_subscriber::{ util::SubscriberInitExt, }; +#[cfg(feature = "runtime")] tokio::task_local! { pub static LOG_TASK_ID: CompactString; } @@ -101,6 +103,7 @@ where if ansi { write!(&mut w, "\x1b[2;37m")?; // Dim fg } + #[cfg(feature = "runtime")] LOG_TASK_ID .try_with(|id| write!(w, "({id})")) .ok() diff --git a/common/taler-common/src/types/amount.rs b/common/taler-common/src/types/amount.rs index 7c95086..27b06a7 100644 --- a/common/taler-common/src/types/amount.rs +++ b/common/taler-common/src/types/amount.rs @@ -25,6 +25,7 @@ use std::{ use compact_str::format_compact; use super::utils::InlineStr; +#[cfg(feature = "crypto")] use crate::signature::Buf; /** Number of characters we use to represent currency names */ @@ -135,6 +136,7 @@ impl Display for Currency { } } +#[cfg(feature = "db")] #[derive(sqlx::Type)] #[sqlx(type_name = "taler_amount")] struct PgTalerAmount { @@ -282,12 +284,14 @@ impl Debug for Decimal { } } +#[cfg(feature = "db")] impl sqlx::Type for Decimal { fn type_info() -> sqlx::postgres::PgTypeInfo { PgTalerAmount::type_info() } } +#[cfg(feature = "db")] impl<'q> sqlx::Encode<'q, sqlx::Postgres> for Decimal { fn encode_by_ref( &self, @@ -301,6 +305,7 @@ impl<'q> sqlx::Encode<'q, sqlx::Postgres> for Decimal { } } +#[cfg(feature = "db")] impl<'r> sqlx::Decode<'r, sqlx::Postgres> for Decimal { fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result { let pg = PgTalerAmount::decode(value)?; @@ -385,6 +390,7 @@ impl Amount { } /** Encode amount for signature */ + #[cfg(feature = "crypto")] pub fn signature_bytes(self) -> [u8; 24] { Buf::new() .put_u64(self.val) @@ -452,12 +458,14 @@ impl Debug for Amount { } } +#[cfg(feature = "db")] impl sqlx::Type for Amount { fn type_info() -> sqlx::postgres::PgTypeInfo { PgTalerAmount::type_info() } } +#[cfg(feature = "db")] impl<'q> sqlx::Encode<'q, sqlx::Postgres> for Amount { fn encode_by_ref( &self, diff --git a/common/taler-common/src/types/base32.rs b/common/taler-common/src/types/base32.rs index 3311a9b..de77e11 100644 --- a/common/taler-common/src/types/base32.rs +++ b/common/taler-common/src/types/base32.rs @@ -104,12 +104,14 @@ impl<'a, const L: usize> TryFrom<&'a [u8]> for Base32 { } } +#[cfg(feature = "db")] impl sqlx::Type for Base32 { fn type_info() -> sqlx::postgres::PgTypeInfo { <&[u8]>::type_info() } } +#[cfg(feature = "db")] impl<'q, const L: usize> sqlx::Encode<'q, sqlx::Postgres> for Base32 { fn encode_by_ref( &self, @@ -119,6 +121,7 @@ impl<'q, const L: usize> sqlx::Encode<'q, sqlx::Postgres> for Base32 { } } +#[cfg(feature = "db")] impl<'r, const L: usize> sqlx::Decode<'r, sqlx::Postgres> for Base32 { fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result { let array = <[u8; L]>::decode(value)?; diff --git a/common/taler-common/src/types/time.rs b/common/taler-common/src/types/time.rs index 5d98c04..66676ea 100644 --- a/common/taler-common/src/types/time.rs +++ b/common/taler-common/src/types/time.rs @@ -140,12 +140,14 @@ impl Add for TalerTimestamp { } } +#[cfg(feature = "db")] impl sqlx::Type for TalerTimestamp { fn type_info() -> ::TypeInfo { Option::::type_info() } } +#[cfg(feature = "db")] impl<'q> sqlx::Encode<'q, sqlx::Postgres> for TalerTimestamp { fn encode_by_ref( &self, @@ -159,6 +161,7 @@ impl<'q> sqlx::Encode<'q, sqlx::Postgres> for TalerTimestamp { } } +#[cfg(feature = "db")] impl<'r> sqlx::Decode<'r, sqlx::Postgres> for TalerTimestamp { fn decode( value: ::ValueRef<'r>, diff --git a/taler-apns-relay/Cargo.toml b/taler-apns-relay/Cargo.toml index a83fbbb..568bba9 100644 --- a/taler-apns-relay/Cargo.toml +++ b/taler-apns-relay/Cargo.toml @@ -31,7 +31,7 @@ rustls.workspace = true jiff.workspace = true tokio.workspace = true aws-lc-rs.workspace = true -compact_str.workspace = true +compact_str = { workspace = true, features = ["sqlx-postgres"] } tracing.workspace = true thiserror.workspace = true -- 2.47.3