use super::*;
use core::convert::Into;
pub trait OptionInsert {
fn insert_option_with_bytes(&mut self, key: OptionNumber, value: &[u8]) -> Result<(), Error>;
fn insert_option_empty(&mut self, key: OptionNumber) -> Result<(), Error> {
self.insert_option_with_bytes(key, &[])
}
fn insert_option_with_str(&mut self, key: OptionNumber, value: &str) -> Result<(), Error> {
self.insert_option_with_bytes(key, value.as_bytes())
}
fn insert_option_with_u32(&mut self, key: OptionNumber, value: u32) -> Result<(), Error> {
self.insert_option_with_bytes(key, encode_u32(value, &mut [0; 4]))
}
}
pub trait OptionInsertExt {
fn insert_option<'a, T>(&mut self, key: OptionKey<T>, value: T) -> Result<(), Error>
where
T: Into<OptionValue<'a>>;
}
impl<O> OptionInsertExt for O
where
O: OptionInsert + ?Sized,
{
fn insert_option<'a, T>(&mut self, key: OptionKey<T>, value: T) -> Result<(), Error>
where
T: Into<OptionValue<'a>>,
{
match value.into() {
OptionValue::Integer(x) => self.insert_option_with_u32(key.0, x),
OptionValue::Bytes(x) => self.insert_option_with_bytes(key.0, x),
OptionValue::ETag(x) => self.insert_option_with_bytes(key.0, x.as_bytes()),
}
}
}