Skip to main content

rustify_licensing/
lib.rs

1//! # rustify-licensing
2//!
3//! Framework-agnostic in-app licensing for native Rust desktop apps: validate a key,
4//! node-lock the machine, cache the result, and stay unlocked through a bounded
5//! **offline grace** window — failing **closed** on an authoritative denial.
6//!
7//! A framework-agnostic core plus thin adapters, so the same logic drives a Bevy app,
8//! a headless CLI, an egui/immediate-mode GUI, or tests.
9//!
10//! ## Shape
11//! - [`core`] — pure, runtime-agnostic: [`LicenseState`] persistence + fingerprint,
12//!   [`LicenseStatus`], [`CheckErr`] (the load-bearing `Denied` vs `Offline` split),
13//!   and the [`core::engine`] that folds a check result into a status.
14//! - [`provider`] — the [`LicenseProvider`] trait and the [`KeygenProvider`] impl
15//!   (the exact Keygen validate/activate/node-lock dance). Swap in Polar later.
16//! - [`adapters`] — `headless` (a blocking driver) and, behind the `bevy` feature,
17//!   a [`adapters::bevy`] plugin with resources, systems, and an event API.
18//!
19//! ## Quick start (headless)
20//! ```no_run
21//! use rustify_licensing::{KeygenProvider, LicenseConfig, LicenseState, StorageConfig};
22//! use rustify_licensing::adapters::headless::check_blocking;
23//!
24//! let config = LicenseConfig {
25//!     account_id: "your-keygen-account-uuid".into(),
26//!     api_base: "https://api.keygen.sh/v1".into(),
27//!     grace_secs: 14 * 24 * 60 * 60,
28//!     storage: StorageConfig::app_data("MyApp", "license.env", None, Some("MYAPP_LICENSE_KEY")),
29//!     buy_url: "https://myapp.example/#pricing".into(),
30//! };
31//! let provider = KeygenProvider::from_config(&config);
32//! let mut state = LicenseState::load(&config.storage);
33//! let status = check_blocking(&config, &provider, &mut state, |line| println!("… {line}"));
34//! println!("status: {status:?}");
35//! ```
36
37pub mod adapters;
38pub mod core;
39pub mod provider;
40
41pub use crate::core::config::{LicenseConfig, StorageConfig};
42pub use crate::core::error::CheckErr;
43pub use crate::core::state::{now_secs, LicenseState};
44pub use crate::core::status::LicenseStatus;
45pub use crate::provider::{KeygenProvider, LicenseProvider};
46
47/// Framework-agnostic threaded driver for immediate-mode GUIs / CLIs.
48pub use crate::adapters::worker::LicenseWorker;
49
50#[cfg(feature = "bevy")]
51pub use crate::adapters::bevy;
52
53/// Open a URL in the user's default browser (for a "Buy" button → `config.buy_url`).
54pub fn open_url(url: &str) {
55    #[cfg(target_os = "macos")]
56    let _ = std::process::Command::new("open").arg(url).spawn();
57    #[cfg(target_os = "windows")]
58    let _ = std::process::Command::new("cmd")
59        .args(["/C", "start", "", url])
60        .spawn();
61    #[cfg(all(unix, not(target_os = "macos")))]
62    let _ = std::process::Command::new("xdg-open").arg(url).spawn();
63}