at_commands/
translate_util.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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::lowlevel::{Argument, Arguments};
use crate::serde::DeserializeErrorCause;

pub fn extract_vec_from_args(
    arguments: &Arguments,
) -> Result<&Vec<Argument>, DeserializeErrorCause> {
    if let Arguments::ArgumentList(arg_vec) = arguments {
        Ok(arg_vec)
    } else {
        Err(DeserializeErrorCause::UnknownArguments(arguments.clone()))
    }
}

pub fn extract_vec_vec_from_args(
    arguments: &Arguments,
) -> Result<&Vec<Vec<Argument>>, DeserializeErrorCause> {
    if let Arguments::ParenthesisDelimitedArgumentLists(arg_vec_vec) = arguments {
        Ok(arg_vec_vec)
    } else {
        Err(DeserializeErrorCause::UnknownArguments(arguments.clone()))
    }
}

pub fn extract_primitive_from_field<'a>(
    field: &'a Argument,
    args_for_error_reporting: &Arguments,
) -> Result<&'a String, DeserializeErrorCause> {
    if let Argument::PrimitiveArgument(arg) = field {
        Ok(arg)
    } else {
        Err(DeserializeErrorCause::UnknownArguments(args_for_error_reporting.clone()))
    }
}

pub fn extract_int_from_primitive(
    field: &str,
    args_for_error_reporting: &Arguments,
) -> Result<i64, DeserializeErrorCause> {
    field
        .parse()
        .map_err(|_| DeserializeErrorCause::UnknownArguments(args_for_error_reporting.clone()))
}

pub fn extract_key_from_field<'a>(
    field: &'a Argument,
    args_for_error_reporting: &'a Arguments,
) -> Result<&'a String, DeserializeErrorCause> {
    if let Argument::KeyValueArgument { key, .. } = field {
        Ok(key)
    } else {
        Err(DeserializeErrorCause::UnknownArguments(args_for_error_reporting.clone()))
    }
}

pub fn extract_value_from_field<'a>(
    field: &'a Argument,
    args_for_error_reporting: &'a Arguments,
) -> Result<&'a String, DeserializeErrorCause> {
    if let Argument::KeyValueArgument { value, .. } = field {
        Ok(value)
    } else {
        Err(DeserializeErrorCause::UnknownArguments(args_for_error_reporting.clone()))
    }
}