sl4f_lib/location/
commands.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::location::emergency_provider_facade::EmergencyProviderFacade;
use crate::location::regulatory_region_facade::RegulatoryRegionFacade;
use crate::location::types::PositionSerializer;
use crate::server::Facade;
use anyhow::{format_err, Error};
use async_trait::async_trait;
use serde_json::{to_value, Value};

#[async_trait(?Send)]
impl Facade for RegulatoryRegionFacade {
    async fn handle_request(&self, method: String, args: Value) -> Result<Value, Error> {
        match method.as_ref() {
            "set_region" => {
                let region =
                    args.get("region").ok_or_else(|| format_err!("Must provide a `region`"))?;
                let region =
                    region.as_str().ok_or_else(|| format_err!("`region` must be a string"))?;
                Ok(to_value(self.set_region(region)?)?)
            }
            _ => {
                return Err(format_err!(
                    "unsupported command {} for regulatory-region-facade!",
                    method
                ))
            }
        }
    }
}

#[async_trait(?Send)]
impl Facade for EmergencyProviderFacade {
    async fn handle_request(&self, method: String, _args: Value) -> Result<Value, Error> {
        match method.as_ref() {
            "get_current" => Ok(to_value(PositionSerializer(self.get_current().await?))?),
            _ => {
                return Err(format_err!(
                    "unsupported command {} for emergency-provider-facade!",
                    method
                ))
            }
        }
    }
}