1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use crate::newtypes::libcnb_newtype;
use serde::{Deserialize, Serialize, Serializer};
use std::path::PathBuf;

/// Data Structure for the launch.toml file.
#[derive(Deserialize, Serialize, Clone, Debug, Default)]
#[serde(deny_unknown_fields)]
pub struct Launch {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub labels: Vec<Label>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub processes: Vec<Process>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub slices: Vec<Slice>,
}

/// A non-consuming builder for [`Launch`] values.
///
/// # Examples
/// ```
/// use libcnb_data::launch::{LaunchBuilder, ProcessBuilder};
/// use libcnb_data::process_type;
///
/// let launch_toml = LaunchBuilder::new()
///     .process(
///         ProcessBuilder::new(process_type!("web"), ["bundle"])
///             .args(["exec", "ruby", "app.rb"])
///             .build(),
///     )
///     .build();
///
/// assert!(toml::to_string(&launch_toml).is_ok());
/// ```
#[derive(Default)]
pub struct LaunchBuilder {
    launch: Launch,
}

impl LaunchBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a process to the launch configuration.
    pub fn process<P: Into<Process>>(&mut self, process: P) -> &mut Self {
        self.launch.processes.push(process.into());
        self
    }

    /// Adds multiple processes to the launch configuration.
    pub fn processes<I: IntoIterator<Item = P>, P: Into<Process>>(
        &mut self,
        processes: I,
    ) -> &mut Self {
        for process in processes {
            self.process(process);
        }

        self
    }

    /// Adds a label to the launch configuration.
    pub fn label<L: Into<Label>>(&mut self, label: L) -> &mut Self {
        self.launch.labels.push(label.into());
        self
    }

    /// Adds multiple processes to the launch configuration.
    pub fn labels<I: IntoIterator<Item = L>, L: Into<Label>>(&mut self, labels: I) -> &mut Self {
        for label in labels {
            self.label(label);
        }

        self
    }

    /// Adds a slice to the launch configuration.
    pub fn slice<S: Into<Slice>>(&mut self, slice: S) -> &mut Self {
        self.launch.slices.push(slice.into());
        self
    }

    /// Adds multiple slices to the launch configuration.
    pub fn slices<I: IntoIterator<Item = S>, S: Into<Slice>>(&mut self, slices: I) -> &mut Self {
        for slice in slices {
            self.slice(slice);
        }

        self
    }

    /// Builds the `Launch` based on the configuration of this builder.
    #[must_use]
    pub fn build(&self) -> Launch {
        self.launch.clone()
    }
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Label {
    pub key: String,
    pub value: String,
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Process {
    pub r#type: ProcessType,
    pub command: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub args: Vec<String>,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub default: bool,
    #[serde(
        rename = "working-directory",
        default,
        skip_serializing_if = "WorkingDirectory::is_app"
    )]
    pub working_directory: WorkingDirectory,
}

#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(untagged)]
pub enum WorkingDirectory {
    // There is no explicitly defined value in the CNB spec that denotes the app directory. Since
    // we cannot enforce skipping serialization (which indicates the app directory) from this type
    // directly, we serialize this a ".". The CNB spec says that any relative path is treated
    // relative to the app directory, so "." will be the app directory itself. However, types that
    // contain this type (i.e. Process), should always add
    // `#[serde(skip_serializing_if = "WorkingDirectory::is_app")]` to a field of this type.
    App,
    Directory(PathBuf),
}

impl WorkingDirectory {
    #[must_use]
    pub fn is_app(&self) -> bool {
        matches!(self, Self::App)
    }
}

// Custom Serialize implementation since we want to always serialize as a string. Serde's untagged
// enum representation does not work here since App would serialize as null, but we want a default
// string value. #[serde(rename = ".")] doesn't work here. There are more generic solutions that can
// be found on the web, but they're much more heavyweight than this simple Serialize implementation.
impl Serialize for WorkingDirectory {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::App => serializer.serialize_str("."),
            Self::Directory(path) => path.serialize(serializer),
        }
    }
}

impl Default for WorkingDirectory {
    fn default() -> Self {
        Self::App
    }
}

pub struct ProcessBuilder {
    process: Process,
}

/// A non-consuming builder for [`Process`] values.
///
/// # Examples
/// ```
/// # use libcnb_data::process_type;
/// # use libcnb_data::launch::ProcessBuilder;
/// ProcessBuilder::new(process_type!("web"), ["java"])
///     .arg("-jar")
///     .arg("target/application-1.0.0.jar")
///     .default(true)
///     .build();
/// ```
impl ProcessBuilder {
    /// Constructs a new `ProcessBuilder` with the following defaults:
    ///
    /// * No additional, user-overridable, arguments to the command
    /// * `default` is `false`
    /// * `working_directory` will be `WorkingDirectory::App`.
    pub fn new(r#type: ProcessType, command: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self {
            process: Process {
                r#type,
                command: command.into_iter().map(Into::into).collect(),
                args: Vec::new(),
                default: false,
                working_directory: WorkingDirectory::App,
            },
        }
    }

    /// Adds a user-overridable argument to the process.
    ///
    /// Only one argument can be passed per use. So instead of:
    /// ```
    /// # use libcnb_data::process_type;
    /// # libcnb_data::launch::ProcessBuilder::new(process_type!("web"), ["command"])
    /// .arg("-C /path/to/repo")
    /// # ;
    /// ```
    ///
    /// usage would be:
    ///
    /// ```
    /// # use libcnb_data::process_type;
    /// # libcnb_data::launch::ProcessBuilder::new(process_type!("web"), ["command"])
    /// .arg("-C")
    /// .arg("/path/to/repo")
    /// # ;
    /// ```
    ///
    /// To pass multiple arguments see [`args`](Self::args).
    pub fn arg(&mut self, arg: impl Into<String>) -> &mut Self {
        self.process.args.push(arg.into());
        self
    }

    /// Adds multiple, user-overridable, arguments to the process.
    ///
    /// To pass a single argument see [`arg`](Self::arg).
    pub fn args(&mut self, args: impl IntoIterator<Item = impl Into<String>>) -> &mut Self {
        for arg in args {
            self.arg(arg);
        }

        self
    }

    /// Sets the `default` flag on the process.
    ///
    /// Indicates that the process type should be selected as the buildpack-provided
    /// default during the export phase.
    pub fn default(&mut self, value: bool) -> &mut Self {
        self.process.default = value;
        self
    }

    /// Set the working directory for the process.
    pub fn working_directory(&mut self, value: WorkingDirectory) -> &mut Self {
        self.process.working_directory = value;
        self
    }

    /// Builds the `Process` based on the configuration of this builder.
    #[must_use]
    pub fn build(&self) -> Process {
        self.process.clone()
    }
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Slice {
    /// Path globs for this slice.
    ///
    /// These globs need to follow the pattern syntax defined in the [Go standard library](https://golang.org/pkg/path/filepath/#Match)
    /// and only match files/directories inside the application directory.
    #[serde(rename = "paths")]
    pub path_globs: Vec<String>,
}

libcnb_newtype!(
    launch,
    /// Construct a [`ProcessType`] value at compile time.
    ///
    /// Passing a string that is not a valid `ProcessType` value will yield a compilation error.
    ///
    /// # Examples:
    /// ```
    /// use libcnb_data::launch::ProcessType;
    /// use libcnb_data::process_type;
    ///
    /// let process_type: ProcessType = process_type!("web");
    /// ```
    process_type,
    /// The type of a process.
    ///
    /// It MUST only contain numbers, letters, and the characters `.`, `_`, and `-`.
    ///
    /// Use the [`process_type`](crate::process_type) macro to construct a `ProcessType` from a
    /// literal string. To parse a dynamic string into a `ProcessType`, use
    /// [`str::parse`](str::parse).
    ///
    /// # Examples
    /// ```
    /// use libcnb_data::launch::ProcessType;
    /// use libcnb_data::process_type;
    ///
    /// let from_literal = process_type!("web");
    ///
    /// let input = "web";
    /// let from_dynamic: ProcessType = input.parse().unwrap();
    /// assert_eq!(from_dynamic, from_literal);
    ///
    /// let input = "!nv4lid";
    /// let invalid: Result<ProcessType, _> = input.parse();
    /// assert!(invalid.is_err());
    /// ```
    ProcessType,
    ProcessTypeError,
    r"^[[:alnum:]._-]+$"
);

#[cfg(test)]
mod tests {
    use super::*;
    use serde_test::{assert_ser_tokens, Token};

    #[test]
    fn launch_builder_add_processes() {
        let launch = LaunchBuilder::new()
            .process(ProcessBuilder::new(process_type!("web"), ["web_command"]).build())
            .processes([
                ProcessBuilder::new(process_type!("another"), ["another_command"]).build(),
                ProcessBuilder::new(process_type!("worker"), ["worker_command"]).build(),
            ])
            .build();

        assert_eq!(
            launch.processes,
            [
                ProcessBuilder::new(process_type!("web"), ["web_command"]).build(),
                ProcessBuilder::new(process_type!("another"), ["another_command"]).build(),
                ProcessBuilder::new(process_type!("worker"), ["worker_command"]).build(),
            ]
        );
    }

    #[test]
    fn process_type_validation_valid() {
        assert!("web".parse::<ProcessType>().is_ok());
        assert!("Abc123._-".parse::<ProcessType>().is_ok());
    }

    #[test]
    fn process_type_validation_invalid() {
        assert_eq!(
            "worker/foo".parse::<ProcessType>(),
            Err(ProcessTypeError::InvalidValue(String::from("worker/foo")))
        );
        assert_eq!(
            "worker:foo".parse::<ProcessType>(),
            Err(ProcessTypeError::InvalidValue(String::from("worker:foo")))
        );
        assert_eq!(
            "worker foo".parse::<ProcessType>(),
            Err(ProcessTypeError::InvalidValue(String::from("worker foo")))
        );
        assert_eq!(
            "".parse::<ProcessType>(),
            Err(ProcessTypeError::InvalidValue(String::new()))
        );
    }

    #[test]
    fn process_with_default_values_deserialization() {
        let toml_str = r#"
type = "web"
command = ["foo"]
"#;

        assert_eq!(
            toml::from_str::<Process>(toml_str),
            Ok(Process {
                r#type: process_type!("web"),
                command: vec![String::from("foo")],
                args: Vec::new(),
                default: false,
                working_directory: WorkingDirectory::App
            })
        );
    }

    #[test]
    fn process_with_default_values_serialization() {
        let process = ProcessBuilder::new(process_type!("web"), ["foo"]).build();

        let string = toml::to_string(&process).unwrap();
        assert_eq!(
            string,
            r#"type = "web"
command = ["foo"]
"#
        );
    }

    #[test]
    fn process_with_some_default_values_serialization() {
        let process = ProcessBuilder::new(process_type!("web"), ["foo"])
            .default(true)
            .working_directory(WorkingDirectory::Directory(PathBuf::from("dist")))
            .build();

        let string = toml::to_string(&process).unwrap();
        assert_eq!(
            string,
            r#"type = "web"
command = ["foo"]
default = true
working-directory = "dist"
"#
        );
    }

    #[test]
    fn process_builder() {
        let mut process_builder = ProcessBuilder::new(process_type!("web"), ["java"]);

        assert_eq!(
            process_builder.build(),
            Process {
                r#type: process_type!("web"),
                command: vec![String::from("java")],
                args: Vec::new(),
                default: false,
                working_directory: WorkingDirectory::App
            }
        );

        process_builder.default(true);

        assert_eq!(
            process_builder.build(),
            Process {
                r#type: process_type!("web"),
                command: vec![String::from("java")],
                args: Vec::new(),
                default: true,
                working_directory: WorkingDirectory::App
            }
        );

        process_builder.working_directory(WorkingDirectory::Directory(PathBuf::from("dist")));

        assert_eq!(
            process_builder.build(),
            Process {
                r#type: process_type!("web"),
                command: vec![String::from("java")],
                args: Vec::new(),
                default: true,
                working_directory: WorkingDirectory::Directory(PathBuf::from("dist"))
            }
        );
    }

    #[test]
    fn process_builder_args() {
        assert_eq!(
            ProcessBuilder::new(process_type!("web"), ["java"])
                .arg("foo")
                .args(["baz", "eggs"])
                .arg("bar")
                .build(),
            Process {
                r#type: process_type!("web"),
                command: vec![String::from("java")],
                args: vec![
                    String::from("foo"),
                    String::from("baz"),
                    String::from("eggs"),
                    String::from("bar"),
                ],
                default: false,
                working_directory: WorkingDirectory::App
            }
        );
    }

    #[test]
    fn process_working_directory_serialization() {
        assert_ser_tokens(&WorkingDirectory::App, &[Token::BorrowedStr(".")]);

        assert_ser_tokens(
            &WorkingDirectory::Directory(PathBuf::from("/")),
            &[Token::BorrowedStr("/")],
        );
        assert_ser_tokens(
            &WorkingDirectory::Directory(PathBuf::from("/foo/bar")),
            &[Token::BorrowedStr("/foo/bar")],
        );
        assert_ser_tokens(
            &WorkingDirectory::Directory(PathBuf::from("relative/foo/bar")),
            &[Token::BorrowedStr("relative/foo/bar")],
        );
    }
}