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
69
70
71
72
// (C) Copyright 2019-2020 Hewlett Packard Enterprise Development LP

use std::convert::TryFrom;

use crate::dockerfile_parser::Instruction;
use crate::parser::{Pair, Rule};
use crate::error::*;

use enquote::unquote;
use snafu::ResultExt;

/// A Dockerfile [`ARG` instruction][arg].
///
/// [arg]: https://docs.docker.com/engine/reference/builder/#arg
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArgInstruction {
  /// The argument key
  pub name: String,

  /// An optional argument value.
  ///
  /// This may be unset when passing arguments through to later stages in a
  /// [multi-stage build][build].
  ///
  /// [build]: https://docs.docker.com/develop/develop-images/multistage-build/
  pub value: Option<String>
}

impl ArgInstruction {
  pub(crate) fn from_record(record: Pair) -> Result<ArgInstruction> {
    let mut name = None;
    let mut value = None;

    for field in record.into_inner() {
      match field.as_rule() {
        Rule::arg_name => name = Some(field.as_str()),
        Rule::arg_quoted_value => {
          let v = unquote(field.as_str()).context(UnescapeError)?;

          value = Some(v);
        }
        Rule::arg_value => value = Some(field.as_str().to_string()),
        _ => return Err(unexpected_token(field))
      }
    }

    let name = name.ok_or_else(|| Error::GenericParseError {
      message: "arg name is required".into()
    })?.to_string();

    let value = value.map(String::from);

    Ok(ArgInstruction {
      name, value
    })
  }
}

impl<'a> TryFrom<&'a Instruction> for &'a ArgInstruction {
 type Error = Error;

 fn try_from(instruction: &'a Instruction) -> std::result::Result<Self, Self::Error> {
   if let Instruction::Arg(a) = instruction {
     Ok(a)
   } else {
     Err(Error::ConversionError {
       from: format!("{:?}", instruction),
       to: "ArgInstruction".into()
     })
   }
 }
}