pub mod bytes;
pub use bytes::BytesInput;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::{clone::Clone, fmt::Debug};
#[cfg(feature = "std")]
use std::{
fs::File,
io::{Read, Write},
path::Path,
};
use serde::{Deserialize, Serialize};
use crate::{bolts::ownedref::OwnedSlice, Error};
pub trait Input: Clone + serde::Serialize + serde::de::DeserializeOwned + Debug {
#[cfg(feature = "std")]
fn to_file<P>(&self, path: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
let mut file = File::create(path)?;
let serialized = postcard::to_allocvec(self)?;
file.write_all(&serialized)?;
Ok(())
}
#[cfg(not(feature = "std"))]
fn to_file<P>(&self, _path: P) -> Result<(), Error> {
Err(Error::NotImplemented("Not supported in no_std".into()))
}
#[cfg(feature = "std")]
fn from_file<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
let mut file = File::open(path)?;
let mut bytes: Vec<u8> = vec![];
file.read_to_end(&mut bytes)?;
Ok(postcard::from_bytes(&bytes)?)
}
#[cfg(not(feature = "std"))]
fn from_file<P>(_path: P) -> Result<Self, Error> {
Err(Error::NotImplemented("Not supprted in no_std".into()))
}
fn generate_name(&self, idx: usize) -> String;
}
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
pub struct NopInput {}
impl Input for NopInput {
fn generate_name(&self, _idx: usize) -> String {
"nop-input".to_string()
}
}
impl HasTargetBytes for NopInput {
fn target_bytes(&self) -> OwnedSlice<u8> {
OwnedSlice::Owned(vec![0])
}
}
pub trait HasTargetBytes {
fn target_bytes(&self) -> OwnedSlice<u8>;
}
pub trait HasBytesVec {
fn bytes(&self) -> &[u8];
fn bytes_mut(&mut self) -> &mut Vec<u8>;
}
pub trait HasLen {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}