From c9b233e78fad424a53621dfc7215297430cca0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Djk=C3=A1=C5=A5o?= Date: Thu, 23 May 2024 00:21:13 +0200 Subject: [PATCH] add features to feature gate big deps --- Cargo.lock | 3 +- sdk/Cargo.toml | 61 +++++++++++++++++++++++++++-------------- sdk/src/apl/mod.rs | 2 +- sdk/src/config.rs | 4 +++ sdk/src/lib.rs | 27 ++++++++++++++---- sdk/src/webhooks/mod.rs | 1 + 6 files changed, 70 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 284ad76..88c4368 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2677,7 +2677,7 @@ checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" [[package]] name = "saleor-app-sdk" -version = "0.2.1" +version = "0.2.2" dependencies = [ "anyhow", "async-trait", @@ -2694,7 +2694,6 @@ dependencies = [ "serde_json", "strum 0.26.2", "strum_macros 0.26.2", - "tower", "tracing", "tracing-subscriber", "url", diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 99e61ab..f5d5e08 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "saleor-app-sdk" authors = ["Djkáťo "] -version = "0.2.1" +version = "0.2.2" edition = "2021" description = "Unofficial Saleor App SDK like library, made to for Rust." keywords = ["saleor", "sdk", "plugin"] @@ -12,26 +12,47 @@ documentation = "https://github.com/djkato/saleor-apps-rs" license = "MIT OR Apache-2.0" [dependencies] -anyhow.workspace = true +anyhow = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +envy = { workspace = true } +dotenvy = { workspace = true } +rust_decimal = { workspace = true, features = ["serde-float"] } +iso_currency = { workspace = true, features = ["with-serde", "iterator"] } +strum = { version = "0.26.2" } +strum_macros = { version = "0.26.2" } +async-trait = { version = "0.1.80" } + +## Needed for middleware +axum = { workspace = true, optional = true } +jsonwebtoken = { version = "9.3.0", optional = true } +url = { version = "2.5.0", optional = true } +reqwest = { version = "0.12.3", features = ["json"], optional = true } +http = { version = "1.1.0", optional = true } +# tower = { workspace = true, optional = true } + +## Needed for APLs redis = { workspace = true, features = [ "aio", "tokio-comp", "connection-manager", -] } -serde.workspace = true -axum.workspace = true -tracing.workspace = true -tracing-subscriber.workspace = true -serde_json.workspace = true -envy.workspace = true -dotenvy.workspace = true -tower = { workspace = true } -rust_decimal = { workspace = true, features = ["serde-float"] } -iso_currency = { workspace = true, features = ["with-serde", "iterator"] } -reqwest = { version = "0.12.3", features = ["json"] } -jsonwebtoken = "9.3.0" -async-trait = "0.1.80" -http = "1.1.0" -url = "2.5.0" -strum = "0.26.2" -strum_macros = "0.26.2" +], optional = true } + +## Tracing +tracing = { workspace = true, optional = true } +tracing-subscriber = { workspace = true, optional = true } + +## Needed for webhooks + +[features] +default = ["middleware", "redis_apl", "webhook_utils", "tracing"] +middleware = [ + "dep:axum", + "dep:jsonwebtoken", + "dep:url", + "dep:reqwest", + "dep:http", +] +redis_apl = ["dep:redis"] +webhook_utils = ["dep:http"] +tracing = ["dep:tracing", "dep:tracing-subscriber"] diff --git a/sdk/src/apl/mod.rs b/sdk/src/apl/mod.rs index 54242e4..b684a9e 100644 --- a/sdk/src/apl/mod.rs +++ b/sdk/src/apl/mod.rs @@ -1,5 +1,6 @@ pub mod env_apl; pub mod file_apl; +#[cfg(feature = "redis_apl")] pub mod redis_apl; use crate::AuthData; @@ -7,7 +8,6 @@ use anyhow::Result; use async_trait::async_trait; use serde::{Deserialize, Serialize}; - #[derive(Debug, Clone, Serialize, Deserialize)] pub enum AplType { Redis, diff --git a/sdk/src/config.rs b/sdk/src/config.rs index d5b6b63..960e609 100644 --- a/sdk/src/config.rs +++ b/sdk/src/config.rs @@ -1,9 +1,11 @@ use serde::Deserialize; +#[cfg(feature = "tracing")] use tracing::{debug, Level}; use crate::apl::AplType; +#[cfg(feature = "tracing")] #[derive(Debug, Deserialize)] #[serde(remote = "Level")] pub enum LocalTracingLevel { @@ -27,6 +29,7 @@ pub struct Config { pub app_iframe_base_url: String, pub apl: AplType, pub apl_url: String, + #[cfg(feature = "tracing")] #[serde(with = "LocalTracingLevel")] pub log_level: tracing::Level, } @@ -41,6 +44,7 @@ impl Config { pub fn load() -> Result { _ = dotenvy::dotenv(); let env = envy::from_env::(); + #[cfg(feature = "tracing")] debug!("{:?}", &env); env } diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index 5166046..92570c0 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -3,6 +3,7 @@ pub mod config; pub mod headers; pub mod locales; pub mod manifest; +#[cfg(feature = "middleware")] pub mod middleware; pub mod webhooks; @@ -10,7 +11,9 @@ use apl::{AplType, APL}; use config::Config; use serde::{Deserialize, Serialize}; -use crate::apl::{env_apl::EnvApl, file_apl::FileApl, redis_apl::RedisApl}; +use crate::apl::env_apl::EnvApl; +#[cfg(feature = "redis_apl")] +use crate::apl::{file_apl::FileApl, redis_apl::RedisApl}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AuthToken { @@ -51,11 +54,25 @@ impl SaleorApp { use AplType::{Env, File, Redis}; Ok(SaleorApp { apl: match config.apl { - Redis => Box::new(RedisApl::new(&config.apl_url, &config.app_api_base_url)?), + Redis => { + if cfg!(not(feature = "redis_apl")) { + dbg!("Tried starting app with apl that wasn't present at compile time (cargo feature missing). Falling back to env_apl"); + Box::new(EnvApl {}) + } else { + Box::new(RedisApl::new(&config.apl_url, &config.app_api_base_url)?) + } + } Env => Box::new(EnvApl {}), - File => Box::new(FileApl { - path: "apl.txt".to_owned(), - }), + File => { + if cfg!(not(feature = "file_apl")) { + dbg!("Tried starting app with apl that wasn't present at compile time (cargo feature missing). Falling back to env_apl"); + Box::new(EnvApl {}) + } else { + Box::new(FileApl { + path: "apl.txt".to_owned(), + }) + } + } }, }) } diff --git a/sdk/src/webhooks/mod.rs b/sdk/src/webhooks/mod.rs index 725c7b0..0385de4 100644 --- a/sdk/src/webhooks/mod.rs +++ b/sdk/src/webhooks/mod.rs @@ -1,4 +1,5 @@ pub mod sync_response; +#[cfg(feature = "webhook_utils")] pub mod utils; use serde::{Deserialize, Serialize};