use std::{io, path::Path};

use crate::sys::{SpiDevice as SysSpiDevice, spi::SpiDeviceImpl};

pub use crate::sys::{SpiClockPhase, SpiClockPolarity, SpiConfig};

pub struct SpiDevice(SpiDeviceImpl);

impl SpiDevice {
    pub fn open<P: AsRef<Path>>(device: P, config: &SpiConfig) -> io::Result<Self> {
        SpiDeviceImpl::open(device, config).map(Self)
    }

    pub fn transfer(&mut self, tx: &[u8], rx: &mut [u8]) -> io::Result<usize> {
        self.0.transfer(tx, rx)
    }

    pub fn set_config(&mut self, config: &SpiConfig) -> io::Result<()> {
        self.0.set_config(config)
    }
}
