use std::fmt;
use std::path::{Path, PathBuf};
use crate::error::{Error, Result};
pub struct Library {
inner: edgefirst_tflite_sys::tensorflowlite_c,
path: Option<PathBuf>,
}
impl Library {
pub fn new() -> Result<Self> {
let (inner, path) =
edgefirst_tflite_sys::discovery::discover_with_path().map_err(Error::from)?;
Ok(Self {
inner,
path: Some(path),
})
}
pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {
let raw = path.as_ref();
let inner = edgefirst_tflite_sys::discovery::load(raw).map_err(Error::from)?;
let resolved = if raw.is_file() {
std::fs::canonicalize(raw).unwrap_or_else(|_| raw.to_path_buf())
} else {
raw.to_path_buf()
};
Ok(Self {
inner,
path: Some(resolved),
})
}
#[must_use]
pub fn as_sys(&self) -> &edgefirst_tflite_sys::tensorflowlite_c {
&self.inner
}
pub(crate) fn reopen(&self) -> Result<libloading::Library> {
let path = self
.path
.as_ref()
.ok_or_else(|| Error::invalid_argument("library path not available for reopen"))?;
unsafe { libloading::Library::new(path.as_os_str()) }.map_err(Error::from)
}
}
impl fmt::Debug for Library {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Library")
.field("inner", &"tensorflowlite_c { .. }")
.finish()
}
}