rustify-licensing 0.1.0

Framework-agnostic in-app licensing (validate / machine-activate / offline-grace) for native Rust apps. Keygen provider + Bevy / egui / headless adapters.
Documentation
//! Config the licensing engine reads. these are typically constants in an app;
//! here they're values an app constructs (and a scaffold later fills from `rustify.toml`).

use std::path::PathBuf;

/// Where the persisted license state lives, plus an optional env var that overrides the
/// key at startup (handy in dev/CI).
///
/// A project-local dev file shadows the per-user
/// app-data file when present.
#[derive(Clone, Debug)]
pub struct StorageConfig {
    /// App-data file used by an installed app
    /// (e.g. `~/Library/Application Support/MyApp/license.env`).
    pub path: PathBuf,
    /// A project-local file that, when present, shadows [`path`](Self::path) — the dev
    /// convention (e.g. `.myapp_license.env` in a checkout). `None` to disable.
    pub dev_shadow: Option<PathBuf>,
    /// Optional env var whose value overrides the stored key (e.g. `MYAPP_LICENSE_KEY`).
    pub env_key_var: Option<String>,
}

impl StorageConfig {
    /// The file actually read/written: the dev-shadow if it exists, else the app-data path.
    pub fn resolve(&self) -> PathBuf {
        if let Some(shadow) = &self.dev_shadow {
            if shadow.exists() {
                return shadow.clone();
            }
        }
        self.path.clone()
    }

    /// Convenience constructor: `<config_dir>/<app_name>/<filename>` for the app-data
    /// path (created if missing), with an optional dev-shadow filename checked in the CWD.
    pub fn app_data(
        app_name: &str,
        filename: &str,
        dev_shadow: Option<&str>,
        env_key_var: Option<&str>,
    ) -> Self {
        let dir = dirs::config_dir()
            .map(|d| d.join(app_name))
            .unwrap_or_else(|| PathBuf::from("."));
        let _ = std::fs::create_dir_all(&dir);
        StorageConfig {
            path: dir.join(filename),
            dev_shadow: dev_shadow.map(PathBuf::from),
            env_key_var: env_key_var.map(String::from),
        }
    }
}

/// All the provider-agnostic knobs the licensing engine needs. `account_id`/`api_base`
/// identify the backend (Keygen today) and are passed through to construct the provider.
#[derive(Clone, Debug)]
pub struct LicenseConfig {
    /// Keygen account id — a public UUID that ships in the binary.
    pub account_id: String,
    /// Provider API base, e.g. `https://api.keygen.sh/v1`.
    pub api_base: String,
    /// Offline grace window, in seconds (e.g. 14 days).
    pub grace_secs: u64,
    /// Where the license state is persisted.
    pub storage: StorageConfig,
    /// Where "Buy" opens — the app's checkout/pricing page.
    pub buy_url: String,
}