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

use std::convert::TryFrom;

use crate::dockerfile_parser::Instruction;
use crate::error::*;
use crate::util::*;
use crate::parser::*;

/// A miscellaneous (unsupported) Dockerfile instruction.
///
/// These are instructions that aren't explicitly parsed. They may be invalid,
/// deprecated, or otherwise unsupported by this library.
///
/// Unsupported but valid commands include: `MAINTAINER`, `EXPOSE`, `VOLUME`,
/// `USER`, `WORKDIR`, `ONBUILD`, `STOPSIGNAL`, `HEALTHCHECK`, `SHELL`
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct MiscInstruction {
  instruction: String,
  arguments: BreakableString
}

impl MiscInstruction {
  pub(crate) fn from_record(record: Pair) -> Result<MiscInstruction> {
    let mut instruction = None;
    let mut arguments = None;

    for field in record.into_inner() {
      match field.as_rule() {
        Rule::misc_instruction => instruction = Some(field.as_str()),
        Rule::misc_arguments => arguments = Some(parse_any_breakable(field)?),
        _ => return Err(unexpected_token(field))
      }
    }

    let instruction = instruction.ok_or_else(|| Error::GenericParseError {
      message: "generic instructions require a name".into()
    })?.to_string();

    let arguments = arguments.ok_or_else(|| Error::GenericParseError {
      message: "generic instructions require arguments".into()
    })?;

    Ok(MiscInstruction {
      instruction, arguments
    })
  }
}

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

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