Skip to main content

oak_dockerfile/parser/
element_type.rs

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