fidl_fuchsia_update_installer_ext/
options.rsuse fuchsia_inspect as inspect;
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Clone, Debug, Copy, PartialEq, Arbitrary, Serialize, Deserialize)]
pub enum Initiator {
User,
Service,
}
impl Initiator {
fn name(&self) -> &'static str {
match self {
Initiator::User => "User",
Initiator::Service => "Service",
}
}
}
#[derive(Clone, Debug, PartialEq, Arbitrary, Serialize, Deserialize)]
pub struct Options {
pub initiator: Initiator,
pub allow_attach_to_existing_attempt: bool,
pub should_write_recovery: bool,
}
impl Options {
pub fn write_to_inspect(&self, node: &inspect::Node) {
let Options { initiator, allow_attach_to_existing_attempt, should_write_recovery } = self;
node.record_string("initiator", initiator.name());
node.record_bool("allow_attach_to_existing_attempt", *allow_attach_to_existing_attempt);
node.record_bool("should_write_recovery", *should_write_recovery);
}
}
#[derive(Error, Debug, PartialEq)]
pub enum OptionsParseError {
#[error("missing initiator")]
MissingInitiator,
}
impl TryFrom<fidl_fuchsia_update_installer::Options> for Options {
type Error = OptionsParseError;
fn try_from(data: fidl_fuchsia_update_installer::Options) -> Result<Self, OptionsParseError> {
let initiator =
data.initiator.map(|o| o.into()).ok_or(OptionsParseError::MissingInitiator)?;
Ok(Self {
initiator,
allow_attach_to_existing_attempt: data
.allow_attach_to_existing_attempt
.unwrap_or(false),
should_write_recovery: data.should_write_recovery.unwrap_or(true),
})
}
}
impl From<&Options> for fidl_fuchsia_update_installer::Options {
fn from(options: &Options) -> Self {
Self {
initiator: Some(options.initiator.into()),
allow_attach_to_existing_attempt: Some(options.allow_attach_to_existing_attempt),
should_write_recovery: Some(options.should_write_recovery),
..Default::default()
}
}
}
impl From<Options> for fidl_fuchsia_update_installer::Options {
fn from(data: Options) -> Self {
(&data).into()
}
}
impl From<fidl_fuchsia_update_installer::Initiator> for Initiator {
fn from(fidl_initiator: fidl_fuchsia_update_installer::Initiator) -> Self {
match fidl_initiator {
fidl_fuchsia_update_installer::Initiator::User => Initiator::User,
fidl_fuchsia_update_installer::Initiator::Service => Initiator::Service,
}
}
}
impl From<Initiator> for fidl_fuchsia_update_installer::Initiator {
fn from(initiator: Initiator) -> Self {
match initiator {
Initiator::User => fidl_fuchsia_update_installer::Initiator::User,
Initiator::Service => fidl_fuchsia_update_installer::Initiator::Service,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
proptest! {
#[test]
fn options_roundtrips_through_fidl(options: Options) {
let as_fidl: fidl_fuchsia_update_installer::Options = options.clone().into();
prop_assert_eq!(as_fidl.try_into(), Ok(options));
}
#[test]
fn fidl_options_sans_initiator_error(options: Options) {
let mut as_fidl: fidl_fuchsia_update_installer::Options = options.into();
as_fidl.initiator = None;
prop_assert_eq!(Options::try_from(as_fidl), Err(OptionsParseError::MissingInitiator));
}
}
}