use crate::{AppAttr, Result, SkfApp, SkfContainer, SkfDevice};
use tracing::warn;
pub fn open_or_create_app(
device: &dyn SkfDevice,
name: &str,
attr_fn: fn() -> AppAttr,
) -> Result<Box<dyn SkfApp>> {
let list = device.enumerate_app_name()?;
if list.contains(&name.to_string()) {
return device.open_app(name);
}
device.create_app(name, &attr_fn())
}
pub fn recreate_app(
device: &dyn SkfDevice,
name: &str,
attr_fn: fn() -> AppAttr,
) -> Result<Box<dyn SkfApp>> {
let list = device.enumerate_app_name()?;
if list.contains(&name.to_string()) {
device.delete_app(name)?;
}
device.create_app(name, &attr_fn())
}
pub fn open_or_create_container(app: &dyn SkfApp, name: &str) -> Result<Box<dyn SkfContainer>> {
let list = app.enumerate_container_name()?;
if list.contains(&name.to_string()) {
return app.open_container(name);
}
app.create_container(name)
}
pub fn recreate_container(app: &dyn SkfApp, name: &str) -> Result<Box<dyn SkfContainer>> {
let list = app.enumerate_container_name()?;
if list.contains(&name.to_string()) {
app.delete_container(name)?;
}
app.create_container(name)
}
pub struct SecureApp {
app: Box<dyn SkfApp>,
}
impl SecureApp {
pub fn app(&self) -> &Box<dyn SkfApp> {
&self.app
}
}
impl SecureApp {
pub fn new(app: Box<dyn SkfApp>) -> Self {
Self::from(app)
}
}
impl Drop for SecureApp {
fn drop(&mut self) {
if let Err(err) = self.app.clear_secure_state() {
warn!("Clear secure state failed: err = {}", err);
}
}
}
impl From<Box<dyn SkfApp>> for SecureApp {
fn from(app: Box<dyn SkfApp>) -> Self {
Self { app }
}
}