1use std::fmt::Display;
2
3use kdl::{KdlEntry, KdlNode};
4use serde::Serialize;
5
6use crate::error::Result;
7use crate::spec::context::ParsingContext;
8use crate::spec::helpers::NodeHelper;
9
10#[derive(Debug, Default, Clone, Serialize)]
11pub struct SpecMount {
12 pub run: String,
13}
14
15impl SpecMount {
16 pub(crate) fn parse(ctx: &ParsingContext, node: &NodeHelper) -> Result<Self> {
17 let mut mount = SpecMount::default();
18 for (k, v) in node.props() {
19 match k {
20 "run" => mount.run = v.ensure_string()?,
21 k => bail_parse!(ctx, v.entry.span(), "unsupported mount key {k}"),
22 }
23 }
24 for child in node.children() {
25 match child.name() {
26 "run" => mount.run = child.arg(0)?.ensure_string()?,
27 k => bail_parse!(
28 ctx,
29 child.node.name().span(),
30 "unsupported mount value key {k}"
31 ),
32 }
33 }
34 if mount.run.is_empty() {
35 bail_parse!(ctx, node.span(), "mount run is required")
36 }
37 Ok(mount)
38 }
39 pub fn usage(&self) -> String {
40 format!("mount:{}", &self.run)
41 }
42}
43
44impl From<&SpecMount> for KdlNode {
45 fn from(mount: &SpecMount) -> KdlNode {
46 let mut node = KdlNode::new("mount");
47 node.push(KdlEntry::new_prop("run", mount.run.clone()));
48 node
49 }
50}
51
52impl Display for SpecMount {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 write!(f, "{}", self.usage())
55 }
56}
57