sl4f_lib/factory_reset/
facade.rs1use crate::common_utils::common::macros::{fx_err_and_bail, with_line};
6use crate::common_utils::common::LazyProxy;
7use anyhow::Error;
8use fidl_fuchsia_recovery::{FactoryResetMarker, FactoryResetProxy};
9use log::info;
10
11#[derive(Debug)]
16pub struct FactoryResetFacade {
17 factory_reset_manager: LazyProxy<FactoryResetMarker>,
18}
19
20impl FactoryResetFacade {
21 pub fn new() -> FactoryResetFacade {
22 FactoryResetFacade { factory_reset_manager: Default::default() }
23 }
24
25 fn factory_reset_manager(&self) -> Result<FactoryResetProxy, Error> {
27 self.factory_reset_manager.get_or_connect()
28 }
29
30 pub async fn factory_reset(&self) -> Result<(), Error> {
32 let tag = "FactoryResetFacade::factory_reset";
33 info!("Executing factory reset");
34 match self.factory_reset_manager()?.reset().await {
35 Ok(_) => Ok(()),
36 Err(e) => fx_err_and_bail!(
37 &with_line!(tag),
38 format_err!("FIDL call failed with error: {}", e)
39 ),
40 }
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use fidl::endpoints::create_proxy_and_stream;
48 use fidl_fuchsia_recovery::FactoryResetRequest;
49 use fuchsia_async as fasync;
50 use futures::prelude::*;
51 use lazy_static::lazy_static;
52
53 lazy_static! {
54 static ref RESULT: i32 = 0;
55 }
56
57 #[fasync::run_singlethreaded(test)]
58 async fn test_factory_reset() {
59 let (proxy, mut stream) = create_proxy_and_stream::<FactoryResetMarker>();
60 let facade = FactoryResetFacade::new();
61 facade.factory_reset_manager.set(proxy).unwrap();
62 let facade_fut = async move {
63 assert_eq!(facade.factory_reset().await.ok(), Some(()));
64 };
65 let stream_fut = async move {
66 match stream.try_next().await {
67 Ok(Some(FactoryResetRequest::Reset { responder })) => {
68 responder.send((*RESULT).clone()).unwrap();
69 }
70 err => panic!("Error in request handler: {:?}", err),
71 }
72 };
73 future::join(facade_fut, stream_fut).await;
74 }
75}