use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VaultLicense {
pub creator: String, pub allowed_use: String, pub royalty_percent: u8, pub expires_on: Option<String>, pub notes: Option<String>, }
impl VaultLicense {
pub fn new(
creator: &str,
allowed_use: &str,
royalty_percent: u8,
expires_on: Option<&str>,
notes: Option<&str>,
) -> Self {
Self {
creator: creator.to_string(),
allowed_use: allowed_use.to_string(),
royalty_percent,
expires_on: expires_on.map(|s| s.to_string()),
notes: notes.map(|s| s.to_string()),
}
}
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(self)
}
pub fn from_json(json_str: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json_str)
}
pub fn summary(&self) -> String {
format!(
"๐ License: Use = {}, Royalty = {}%, Creator = {}{}{}",
self.allowed_use,
self.royalty_percent,
self.creator,
match &self.expires_on {
Some(date) => format!(", Expires = {}", date),
None => "".to_string(),
},
match &self.notes {
Some(n) => format!(", Notes: {}", n),
None => "".to_string(),
}
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_license_serialization_roundtrip() {
let license = VaultLicense::new("rio@vios.dev", "commercial", 15, Some("2026-12-31"), Some("Test license"));
let json = license.to_json().unwrap();
let parsed = VaultLicense::from_json(&json).unwrap();
assert_eq!(license.creator, parsed.creator);
assert_eq!(license.allowed_use, parsed.allowed_use);
}
}