use std::ffi::CString;
use std::fmt;
use sane_sys as sys;
use crate::Device;
use crate::error::{Error, Result};
use crate::raw::Context;
pub type AuthCallback = dyn Fn(&str) -> Option<Credentials> + Send + Sync;
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Credentials {
pub username: String,
pub password: String,
}
impl Credentials {
pub fn new(username: impl Into<String>, password: impl Into<String>) -> Self {
Self {
username: username.into(),
password: password.into(),
}
}
}
impl fmt::Debug for Credentials {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Credentials")
.field("username", &self.username)
.field("password", &"<redacted>")
.finish()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Version {
pub major: u8,
pub minor: u8,
pub build: u16,
}
impl Version {
fn from_code(code: sys::SANE_Int) -> Self {
Self {
major: sys::SANE_VERSION_MAJOR(code) as u8,
minor: sys::SANE_VERSION_MINOR(code) as u8,
build: sys::SANE_VERSION_BUILD(code) as u16,
}
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.build)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct DeviceInfo {
pub name: String,
pub vendor: String,
pub model: String,
pub kind: String,
pub(crate) raw_name: CString,
}
pub struct Sane {
context: Context,
version: Version,
}
impl Sane {
pub fn init() -> Result<Self> {
Self::init_inner(None)
}
pub fn init_with_auth(
callback: impl Fn(&str) -> Option<Credentials> + Send + Sync + 'static,
) -> Result<Self> {
Self::init_inner(Some(Box::new(callback)))
}
fn init_inner(callback: Option<Box<AuthCallback>>) -> Result<Self> {
let (context, code) = Context::init(callback)?;
Ok(Self {
context,
version: Version::from_code(code),
})
}
pub fn version(&self) -> Version {
self.version
}
pub fn devices(&self, local_only: bool) -> Result<Vec<DeviceInfo>> {
self.context.devices(local_only)
}
pub fn open(&self, name: &str) -> Result<Device<'_>> {
let raw_name = CString::new(name).map_err(|_| Error::NulByte)?;
self.open_raw(name.to_owned(), &raw_name)
}
pub fn open_device(&self, device: &DeviceInfo) -> Result<Device<'_>> {
self.open_raw(device.name.clone(), &device.raw_name)
}
fn open_raw(&self, name: String, raw_name: &std::ffi::CStr) -> Result<Device<'_>> {
Device::new(name, self.context.open(raw_name)?)
}
}
impl fmt::Debug for Sane {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Sane")
.field("version", &self.version)
.finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_from_code() {
let version = Version::from_code(sys::SANE_VERSION_CODE(1, 3, 1));
assert_eq!(
version,
Version {
major: 1,
minor: 3,
build: 1
}
);
assert_eq!(version.to_string(), "1.3.1");
}
#[test]
fn credentials_hide_the_password() {
let credentials = Credentials::new("alice", "secret");
assert!(!format!("{credentials:?}").contains("secret"));
}
}