oak_dockerfile/parser/element_type.rs
1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for the Dockerfile parser.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum DockerfileElementType {
7 /// An identifier.
8 Identifier,
9 /// A string literal.
10 String,
11 /// A numeric literal.
12 Number,
13 /// Whitespace.
14 Whitespace,
15 /// A newline character.
16 Newline,
17 /// `FROM` instruction.
18 From,
19 /// `RUN` instruction.
20 Run,
21 /// `CMD` instruction.
22 Cmd,
23 /// `LABEL` instruction.
24 Label,
25 /// `MAINTAINER` instruction.
26 Maintainer,
27 /// `EXPOSE` instruction.
28 Expose,
29 /// `ENV` instruction.
30 Env,
31 /// `ADD` instruction.
32 Add,
33 /// `COPY` instruction.
34 Copy,
35 /// `ENTRYPOINT` instruction.
36 Entrypoint,
37 /// `VOLUME` instruction.
38 Volume,
39 /// `USER` instruction.
40 User,
41 /// `WORKDIR` instruction.
42 Workdir,
43 /// `ARG` instruction.
44 Arg,
45 /// `ONBUILD` instruction.
46 Onbuild,
47 /// `STOPSIGNAL` instruction.
48 Stopsignal,
49 /// `HEALTHCHECK` instruction.
50 Healthcheck,
51 /// `SHELL` instruction.
52 Shell,
53 /// `AS` keyword.
54 As,
55 /// No-op or placeholder.
56 None,
57 /// `interval` keyword.
58 Interval,
59 /// `timeout` keyword.
60 Timeout,
61 /// `start-period` keyword.
62 StartPeriod,
63 /// `retries` keyword.
64 Retries,
65 /// Single equals sign `=`.
66 Equal,
67 /// Double equals sign `==`.
68 Equals,
69 /// Colon `:`.
70 Colon,
71 /// Comma `,`.
72 Comma,
73 /// Semicolon `;`.
74 Semicolon,
75 /// Dollar sign `$`.
76 Dollar,
77 /// Left bracket `[`.
78 LeftBracket,
79 /// Right bracket `]`.
80 RightBracket,
81 /// Left brace `{`.
82 LeftBrace,
83 /// Right brace `}`.
84 RightBrace,
85 /// Left parenthesis `(`.
86 LeftParen,
87 /// Right parenthesis `)`.
88 RightParen,
89 /// A comment.
90 Comment,
91 /// A file path.
92 Path,
93 /// The root of the Dockerfile.
94 Root,
95 /// A statement or instruction.
96 Statement,
97 /// An error element.
98 Error,
99 /// End of stream.
100 Eof,
101}
102
103impl DockerfileElementType {
104 /// Returns `true` if this element type is a Dockerfile instruction.
105 pub fn is_instruction(&self) -> bool {
106 matches!(
107 self,
108 Self::From
109 | Self::Run
110 | Self::Cmd
111 | Self::Label
112 | Self::Maintainer
113 | Self::Expose
114 | Self::Env
115 | Self::Add
116 | Self::Copy
117 | Self::Entrypoint
118 | Self::Volume
119 | Self::User
120 | Self::Workdir
121 | Self::Arg
122 | Self::Onbuild
123 | Self::Stopsignal
124 | Self::Healthcheck
125 | Self::Shell
126 )
127 }
128
129 /// Returns `true` if this element type is a trivia element (whitespace, newline, or comment).
130 pub fn is_trivia(&self) -> bool {
131 matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
132 }
133}
134
135impl ElementType for DockerfileElementType {
136 type Role = UniversalElementRole;
137
138 fn role(&self) -> Self::Role {
139 match self {
140 Self::Root => UniversalElementRole::Root,
141
142 Self::Error => UniversalElementRole::Error,
143 _ => UniversalElementRole::None,
144 }
145 }
146}
147
148impl From<crate::lexer::token_type::DockerfileTokenType> for DockerfileElementType {
149 fn from(token: crate::lexer::token_type::DockerfileTokenType) -> Self {
150 unsafe { std::mem::transmute(token) }
151 }
152}