dockerfile_parser/instructions/
misc.rs

1// (C) Copyright 2019-2020 Hewlett Packard Enterprise Development LP
2
3use std::convert::TryFrom;
4
5use crate::Span;
6use crate::dockerfile_parser::Instruction;
7use crate::error::*;
8use crate::util::*;
9use crate::parser::*;
10
11/// A miscellaneous (unsupported) Dockerfile instruction.
12///
13/// These are instructions that aren't explicitly parsed. They may be invalid,
14/// deprecated, or otherwise unsupported by this library.
15///
16/// Unsupported but valid commands include: `MAINTAINER`, `EXPOSE`, `VOLUME`,
17/// `USER`, `WORKDIR`, `ONBUILD`, `STOPSIGNAL`, `HEALTHCHECK`, `SHELL`
18#[derive(Debug, PartialEq, Eq, Clone)]
19pub struct MiscInstruction {
20  pub span: Span,
21  pub instruction: SpannedString,
22  pub arguments: BreakableString
23}
24
25impl MiscInstruction {
26  pub(crate) fn from_record(record: Pair) -> Result<MiscInstruction> {
27    let span = Span::from_pair(&record);
28    let mut instruction = None;
29    let mut arguments = None;
30
31    for field in record.into_inner() {
32      match field.as_rule() {
33        Rule::misc_instruction => instruction = Some(parse_string(&field)?),
34        Rule::misc_arguments => arguments = Some(parse_any_breakable(field)?),
35        _ => return Err(unexpected_token(field))
36      }
37    }
38
39    let instruction = instruction.ok_or_else(|| Error::GenericParseError {
40      message: "generic instructions require a name".into()
41    })?;
42
43    let arguments = arguments.ok_or_else(|| Error::GenericParseError {
44      message: "generic instructions require arguments".into()
45    })?;
46
47    Ok(MiscInstruction {
48      span,
49      instruction, arguments
50    })
51  }
52}
53
54impl<'a> TryFrom<&'a Instruction> for &'a MiscInstruction {
55  type Error = Error;
56
57  fn try_from(instruction: &'a Instruction) -> std::result::Result<Self, Self::Error> {
58    if let Instruction::Misc(m) = instruction {
59      Ok(m)
60    } else {
61      Err(Error::ConversionError {
62        from: format!("{:?}", instruction),
63        to: "MiscInstruction".into()
64      })
65    }
66  }
67}