Alioth Code Coverage

objects.rs0.00%

1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::HashMap;
16
17use alioth::errors::{DebugTrace, trace_error};
18use snafu::Snafu;
19
20pub const DOC_OBJECTS: &str = r#"Supply additional data to other command line flags.
21* <id>,<value>
22
23Any value that comes after an equal sign(=) and contains a comma(,)
24or equal sign can be supplied using this flag. `<id>` must start
25with `id_` and `<id>` cannot contain any comma or equal sign.
26
27Example: assuming we are going a add a virtio-blk device backed by
28`/path/to/disk,2024.img` and a virtio-fs device backed by a
29vhost-user process listening on socket `/path/to/socket=1`, these
302 devices can be expressed in the command line as follows:
31 --blk path=id_blk --fs vu,socket=id_fs,tag=shared-dir \
32 -o id_blk,/path/to/disk,2024.img \
33 -o id_fs,/path/to/socket=1"#;
34
35#[trace_error]
36#[derive(Snafu, DebugTrace)]
37#[snafu(module, context(suffix(false)))]
38pub enum Error {
39 #[snafu(display("Invalid object key {key:?}, must start with `id_`"))]
40 InvalidKey { key: String },
41 #[snafu(display("Key {key:?} showed up more than once"))]
42 DuplicateKey { key: String },
43}
44
45pub fn parse_objects(objects: &[String]) -> Result<HashMap<&str, &str>, Error> {
46 let mut map = HashMap::new();
47 for obj_s in objects {
48 let (key, val) = obj_s.split_once(',').unwrap_or((obj_s, ""));
49 if !key.starts_with("id_") {
50 return error::InvalidKey {
51 key: key.to_owned(),
52 }
53 .fail();
54 }
55 if map.insert(key, val).is_some() {
56 return error::DuplicateKey {
57 key: key.to_owned(),
58 }
59 .fail();
60 }
61 }
62 Ok(map)
63}
64