objects.rs0.00%
1
// Copyright 2025 Google LLC2
//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 at6
//7
// https://www.apache.org/licenses/LICENSE-2.08
//9
// Unless required by applicable law or agreed to in writing, software10
// 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 and13
// limitations under the License.14
15
use std::collections::HashMap;16
17
use alioth::errors::{DebugTrace, trace_error};18
use snafu::Snafu;19
20
pub const DOC_OBJECTS: &str = r#"Supply additional data to other command line flags.21
* <id>,<value>22
23
Any value that comes after an equal sign(=) and contains a comma(,)24
or equal sign can be supplied using this flag. `<id>` must start25
with `id_` and `<id>` cannot contain any comma or equal sign.26
27
Example: assuming we are going a add a virtio-blk device backed by28
`/path/to/disk,2024.img` and a virtio-fs device backed by a29
vhost-user process listening on socket `/path/to/socket=1`, these30
2 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)))]38
pub 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
45
pub 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