oak_dockerfile/parser/
element_type.rs1use oak_core::{ElementType, Parser, UniversalElementRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7pub enum DockerfileElementType {
8 Identifier,
9 String,
10 Number,
11 Whitespace,
12 Newline,
13 From,
14 Run,
15 Cmd,
16 Label,
17 Maintainer,
18 Expose,
19 Env,
20 Add,
21 Copy,
22 Entrypoint,
23 Volume,
24 User,
25 Workdir,
26 Arg,
27 Onbuild,
28 Stopsignal,
29 Healthcheck,
30 Shell,
31 As,
32 None,
33 Interval,
34 Timeout,
35 StartPeriod,
36 Retries,
37 Equal,
38 Equals,
39 Colon,
40 Comma,
41 Semicolon,
42 Dollar,
43 LeftBracket,
44 RightBracket,
45 LeftBrace,
46 RightBrace,
47 LeftParen,
48 RightParen,
49 Comment,
50 Path,
51 Root,
52 Statement,
53 Error,
54 Eof,
55}
56
57impl DockerfileElementType {
58 pub fn is_instruction(&self) -> bool {
59 matches!(
60 self,
61 Self::From
62 | Self::Run
63 | Self::Cmd
64 | Self::Label
65 | Self::Maintainer
66 | Self::Expose
67 | Self::Env
68 | Self::Add
69 | Self::Copy
70 | Self::Entrypoint
71 | Self::Volume
72 | Self::User
73 | Self::Workdir
74 | Self::Arg
75 | Self::Onbuild
76 | Self::Stopsignal
77 | Self::Healthcheck
78 | Self::Shell
79 )
80 }
81
82 pub fn is_trivia(&self) -> bool {
83 matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
84 }
85}
86
87impl ElementType for DockerfileElementType {
88 type Role = UniversalElementRole;
89
90 fn role(&self) -> Self::Role {
91 match self {
92 Self::Root => UniversalElementRole::Root,
93
94 Self::Error => UniversalElementRole::Error,
95 _ => UniversalElementRole::None,
96 }
97 }
98}
99
100impl From<crate::lexer::token_type::DockerfileTokenType> for DockerfileElementType {
101 fn from(token: crate::lexer::token_type::DockerfileTokenType) -> Self {
102 unsafe { std::mem::transmute(token) }
103 }
104}