at_commands/
translate_util.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::lowlevel::{Argument, Arguments};
6use crate::serde::DeserializeErrorCause;
7
8pub fn extract_vec_from_args(
9    arguments: &Arguments,
10) -> Result<&Vec<Argument>, DeserializeErrorCause> {
11    if let Arguments::ArgumentList(arg_vec) = arguments {
12        Ok(arg_vec)
13    } else {
14        Err(DeserializeErrorCause::UnknownArguments(arguments.clone()))
15    }
16}
17
18pub fn extract_vec_vec_from_args(
19    arguments: &Arguments,
20) -> Result<&Vec<Vec<Argument>>, DeserializeErrorCause> {
21    if let Arguments::ParenthesisDelimitedArgumentLists(arg_vec_vec) = arguments {
22        Ok(arg_vec_vec)
23    } else {
24        Err(DeserializeErrorCause::UnknownArguments(arguments.clone()))
25    }
26}
27
28pub fn extract_primitive_from_field<'a>(
29    field: &'a Argument,
30    args_for_error_reporting: &Arguments,
31) -> Result<&'a String, DeserializeErrorCause> {
32    if let Argument::PrimitiveArgument(arg) = field {
33        Ok(arg)
34    } else {
35        Err(DeserializeErrorCause::UnknownArguments(args_for_error_reporting.clone()))
36    }
37}
38
39pub fn extract_int_from_primitive(
40    field: &str,
41    args_for_error_reporting: &Arguments,
42) -> Result<i64, DeserializeErrorCause> {
43    field
44        .parse()
45        .map_err(|_| DeserializeErrorCause::UnknownArguments(args_for_error_reporting.clone()))
46}
47
48pub fn extract_key_from_field<'a>(
49    field: &'a Argument,
50    args_for_error_reporting: &'a Arguments,
51) -> Result<&'a String, DeserializeErrorCause> {
52    if let Argument::KeyValueArgument { key, .. } = field {
53        Ok(key)
54    } else {
55        Err(DeserializeErrorCause::UnknownArguments(args_for_error_reporting.clone()))
56    }
57}
58
59pub fn extract_value_from_field<'a>(
60    field: &'a Argument,
61    args_for_error_reporting: &'a Arguments,
62) -> Result<&'a String, DeserializeErrorCause> {
63    if let Argument::KeyValueArgument { value, .. } = field {
64        Ok(value)
65    } else {
66        Err(DeserializeErrorCause::UnknownArguments(args_for_error_reporting.clone()))
67    }
68}