dockerfile_parser/instructions/
misc.rs1use std::convert::TryFrom;
4
5use crate::Span;
6use crate::dockerfile_parser::Instruction;
7use crate::error::*;
8use crate::util::*;
9use crate::parser::*;
10
11#[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}