Skip to main content

rustify_licensing/core/
config.rs

1//! Config the licensing engine reads. these are typically constants in an app;
2//! here they're values an app constructs (and a scaffold later fills from `rustify.toml`).
3
4use std::path::PathBuf;
5
6/// Where the persisted license state lives, plus an optional env var that overrides the
7/// key at startup (handy in dev/CI).
8///
9/// A project-local dev file shadows the per-user
10/// app-data file when present.
11#[derive(Clone, Debug)]
12pub struct StorageConfig {
13    /// App-data file used by an installed app
14    /// (e.g. `~/Library/Application Support/MyApp/license.env`).
15    pub path: PathBuf,
16    /// A project-local file that, when present, shadows [`path`](Self::path) — the dev
17    /// convention (e.g. `.myapp_license.env` in a checkout). `None` to disable.
18    pub dev_shadow: Option<PathBuf>,
19    /// Optional env var whose value overrides the stored key (e.g. `MYAPP_LICENSE_KEY`).
20    pub env_key_var: Option<String>,
21}
22
23impl StorageConfig {
24    /// The file actually read/written: the dev-shadow if it exists, else the app-data path.
25    pub fn resolve(&self) -> PathBuf {
26        if let Some(shadow) = &self.dev_shadow {
27            if shadow.exists() {
28                return shadow.clone();
29            }
30        }
31        self.path.clone()
32    }
33
34    /// Convenience constructor: `<config_dir>/<app_name>/<filename>` for the app-data
35    /// path (created if missing), with an optional dev-shadow filename checked in the CWD.
36    pub fn app_data(
37        app_name: &str,
38        filename: &str,
39        dev_shadow: Option<&str>,
40        env_key_var: Option<&str>,
41    ) -> Self {
42        let dir = dirs::config_dir()
43            .map(|d| d.join(app_name))
44            .unwrap_or_else(|| PathBuf::from("."));
45        let _ = std::fs::create_dir_all(&dir);
46        StorageConfig {
47            path: dir.join(filename),
48            dev_shadow: dev_shadow.map(PathBuf::from),
49            env_key_var: env_key_var.map(String::from),
50        }
51    }
52}
53
54/// All the provider-agnostic knobs the licensing engine needs. `account_id`/`api_base`
55/// identify the backend (Keygen today) and are passed through to construct the provider.
56#[derive(Clone, Debug)]
57pub struct LicenseConfig {
58    /// Keygen account id — a public UUID that ships in the binary.
59    pub account_id: String,
60    /// Provider API base, e.g. `https://api.keygen.sh/v1`.
61    pub api_base: String,
62    /// Offline grace window, in seconds (e.g. 14 days).
63    pub grace_secs: u64,
64    /// Where the license state is persisted.
65    pub storage: StorageConfig,
66    /// Where "Buy" opens — the app's checkout/pricing page.
67    pub buy_url: String,
68}