| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 2 | borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#, |
| 3 | fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/, |
| 4 | }; |
| 5 | |
| 6 | use craterust-analyzer cargo dockerfile 0.2.1 crate/::{ |
| 7 | instructionsrust-analyzer cargo dockerfile 0.2.1 instructions/::{ |
| 8 | Argrust-analyzer cargo dockerfile 0.2.1 instructions/Arg#, |
| 9 | Directiverust-analyzer cargo dockerfile 0.2.1 instructions/Directive#, |
| 10 | Fromrust-analyzer cargo dockerfile 0.2.1 instructions/From#, |
| 11 | Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#, |
| 12 | }, |
| 13 | }; |
| 14 | |
| 15 | /// A Dockerfile represented in code. |
| 16 | /// |
| 17 | /// A Dockerfile, conceptually, is a series of instructions. In code, that is exactly how they are |
| 18 | /// represented here. A wrapper around a `Vec<Instruction>` with a few convenience methods and |
| 19 | /// such. |
| 20 | pub struct Dockerfilerust-analyzer cargo dockerfile 0.2.1 builder/Dockerfile#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#>); |
| 21 | |
| 22 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Dockerfilerust-analyzer cargo dockerfile 0.2.1 builder/Dockerfile# { |
| 23 | fn fmtrust-analyzer cargo dockerfile 0.2.1 builder/impl#[Dockerfile][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 24 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", self.0rust-analyzer cargo dockerfile 0.2.1 builder/Dockerfile#0..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]fold().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(), |acc, elem| acc +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). &elem.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl Dockerfilerust-analyzer cargo dockerfile 0.2.1 builder/Dockerfile# { |
| 29 | /// Start building a new Dockerfile from the specified base image. |
| 30 | /// |
| 31 | /// Only requirement is the initial `FROM` instruction. Call `.finish()` when complete. |
| 32 | pub fn baserust-analyzer cargo dockerfile 0.2.1 builder/impl#[Dockerfile]base().<T: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str>>>(from: T) -> DockerfileBuilderrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder# { |
| 33 | DockerfileBuilderrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#{ |
| 34 | initial_directivesrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_directives.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 35 | initial_argsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_args.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 36 | fromrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#from.: Fromrust-analyzer cargo dockerfile 0.2.1 instructions/From#::newrust-analyzer cargo dockerfile 0.2.1 instructions/impl#[From]new().(from), |
| 37 | instructionsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#instructions.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// A Dockerfile builder. |
| 43 | pub struct DockerfileBuilderrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder# { |
| 44 | /// Any parser directives. |
| 45 | initial_directivesrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_directives.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Directiverust-analyzer cargo dockerfile 0.2.1 instructions/Directive#>>, |
| 46 | |
| 47 | /// Any initial `ARG`s which are to appear before the initial `FROM` instruction. |
| 48 | initial_argsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_args.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Argrust-analyzer cargo dockerfile 0.2.1 instructions/Arg#>>, |
| 49 | |
| 50 | /// The new Dockerfile's initial `FROM` instruction. |
| 51 | fromrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#from.: Fromrust-analyzer cargo dockerfile 0.2.1 instructions/From#, |
| 52 | |
| 53 | /// Any additional instructions part of the Dockerfile. |
| 54 | instructionsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#instructions.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#>>, |
| 55 | } |
| 56 | |
| 57 | impl DockerfileBuilderrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder# { |
| 58 | /// Push a new initial directive to this Dockerfile instance. |
| 59 | pub fn push_initial_directiverust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder]push_initial_directive().(mut self, directive: Directiverust-analyzer cargo dockerfile 0.2.1 instructions/Directive#) -> Selfrust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder] { |
| 60 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref mut directives) = self.initial_directivesrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_directives. { |
| 61 | directives.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(directive); |
| 62 | } else { |
| 63 | self.initial_directivesrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_directives. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![directive]); |
| 64 | } |
| 65 | self |
| 66 | } |
| 67 | |
| 68 | /// Push a new initial arg to this Dockerfile instance. |
| 69 | pub fn push_initial_argrust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder]push_initial_arg().(mut self, arg: Argrust-analyzer cargo dockerfile 0.2.1 instructions/Arg#) -> Selfrust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder] { |
| 70 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref mut args) = self.initial_argsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_args. { |
| 71 | args.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(arg); |
| 72 | } else { |
| 73 | self.initial_argsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_args. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![arg]); |
| 74 | } |
| 75 | self |
| 76 | } |
| 77 | |
| 78 | /// Push a new instruction into this Dockerfile instance. |
| 79 | pub fn pushrust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder]push().<I: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#>>(mut self, instruction: I) -> Selfrust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder] { |
| 80 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref mut instructions) = self.instructionsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#instructions. { |
| 81 | instructions.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(instruction.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()); |
| 82 | } else { |
| 83 | self.instructionsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#instructions. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![instruction.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()]); |
| 84 | } |
| 85 | self |
| 86 | } |
| 87 | |
| 88 | /// Append a vector of instructions to this Dockerfile instance. |
| 89 | pub fn appendrust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder]append().<I: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#>>(mut self, new: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<I>) -> Selfrust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder] { |
| 90 | let mut new = new.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 91 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref mut instructions) = self.instructionsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#instructions. { |
| 92 | instructions.appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut new); |
| 93 | } else { |
| 94 | self.instructionsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#instructions. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(new); |
| 95 | } |
| 96 | self |
| 97 | } |
| 98 | |
| 99 | /// Generate the output Dockerfile as a string. |
| 100 | pub fn finishrust-analyzer cargo dockerfile 0.2.1 builder/impl#[DockerfileBuilder]finish().(self) -> Dockerfilerust-analyzer cargo dockerfile 0.2.1 builder/Dockerfile# { |
| 101 | // Add directives to the Dockerfile. |
| 102 | let mut all_instructions: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 103 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(directives) = self.initial_directivesrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_directives. { |
| 104 | all_instructions.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(directives.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|inst| Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#::Directiverust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#Directive#(inst))); |
| 105 | } |
| 106 | |
| 107 | // Add initial args to the Dockerfile. |
| 108 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(args) = self.initial_argsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#initial_args. { |
| 109 | all_instructions.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(args.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|inst| Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#::Argrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#Arg#(inst))); |
| 110 | } |
| 111 | |
| 112 | // Add from instruction to Dockerfile. |
| 113 | all_instructions.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Instructionrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#::Fromrust-analyzer cargo dockerfile 0.2.1 instructions/Instruction#From#(self.fromrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#from.)); |
| 114 | |
| 115 | // Append any other instructions in serial order. |
| 116 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(instructions) = self.instructionsrust-analyzer cargo dockerfile 0.2.1 builder/DockerfileBuilder#instructions. { |
| 117 | all_instructions.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(instructions.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().()); |
| 118 | } |
| 119 | |
| 120 | Dockerfilerust-analyzer cargo dockerfile 0.2.1 builder/Dockerfile#(all_instructions) |
| 121 | } |
| 122 | } |