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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! A Rust library for creating and parsing Launchd files.
//!
//! It's still in early development and all help is welcome.
//!
//! ## Example
//!
//! ``` rust
//! use launchd::{CalendarInterval, Error, Launchd};
//!
//! fn main() -> Result<(), Error> {
//!     let ci = CalendarInterval::default()
//!         .with_hour(12)?
//!         .with_minute(10)?
//!         .with_weekday(7)?;
//!
//!     let launchd = Launchd::new("LABEL", "./foo/bar.txt")?
//!             .with_user_name("Henk")
//!             .with_program_arguments(vec!["Hello".to_string(), "World!".to_string()])
//!             .with_start_calendar_intervals(vec![ci])
//!             .disabled();
//!
//!     #[cfg(feature="io")] // Default
//!     return launchd.to_writer_xml(std::io::stdout());
//!
//!     #[cfg(not(feature="io"))] // If you don't want to build any optional dependencies
//!     return Ok(());
//! }
//! ```
//!
//! Results in:
//!
//! ``` xml
//! <?xml version="1.0" encoding="UTF-8"?>
//! <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
//! <plist version="1.0">
//! <dict>
//!         <key>Label</key>
//!         <string>LABEL</string>
//!         <key>Disabled</key>
//!         <true />
//!         <key>UserName</key>
//!         <string>Henk</string>
//!         <key>Program</key>
//!         <string>./foo/bar.txt</string>
//!         <key>ProgramArguments</key>
//!         <array>
//!                 <string>Hello</string>
//!                 <string>World!</string>
//!         </array>
//!         <key>StartCalendarIntervals</key>
//!         <array>
//!                 <dict>
//!                         <key>Minute</key>
//!                         <integer>10</integer>
//!                         <key>Hour</key>
//!                         <integer>12</integer>
//!                         <key>Weekday</key>
//!                         <integer>7</integer>
//!                 </dict>
//!         </array>
//! </dict>
//! </plist>
//! ```

mod error;
pub mod keep_alive;
pub mod mach_services;
pub mod process_type;
pub mod resource_limits;
pub mod sockets;

pub use self::error::Error;
pub use self::keep_alive::{KeepAliveOptions, KeepAliveType};
pub use self::mach_services::{MachServiceEntry, MachServiceOptions};
pub use self::process_type::ProcessType;
pub use self::resource_limits::ResourceLimits;
pub use self::sockets::{BonjourType, Socket, SocketOptions, Sockets};

#[cfg(feature = "cron")]
use cron::{Schedule, TimeUnitSpec};
#[cfg(feature = "plist")]
use plist::Value;
#[cfg(feature = "io")]
use plist::{from_bytes, from_file, from_reader, from_reader_xml};
#[cfg(feature = "io")]
use plist::{to_file_binary, to_file_xml, to_writer_binary, to_writer_xml};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[cfg(feature = "cron")]
use std::convert::TryInto;
#[cfg(feature = "io")]
use std::io::{Read, Seek, Write};
use std::path::Path;

/// Representation of a launchd.plist file.
/// The definition of which can be found [here](https://www.manpagez.com/man/5/launchd.plist/).
///
/// Usage:
/// ```
/// use launchd::{Launchd, Error, CalendarInterval};
/// use std::path::Path;
///
/// fn example() -> Result<Launchd, Error> {
///     Ok(Launchd::new("LABEL", Path::new("./foo/bar.txt"))?
///         .with_user_name("Henk")
///         .with_program_arguments(vec!["Hello".to_string(), "World!".to_string()])
///         .with_start_calendar_intervals(vec![CalendarInterval::default().with_hour(12)?])
///         .disabled()
///         // etc...
///     )
/// }
///
/// let launchd = example();
///
/// ```
/// This will create a launchd representation with the label "LABEL", running "./foo/bar.txt"
/// with the args "Hello" and "World!", for the user "Henk", each day at 12.
///
/// NB: The usage is still subject to change.
// TODO: Fill with all options in https://www.manpagez.com/man/5/launchd.plist/
// TODO: remove owned Strings (?)
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "io", serde(rename_all = "PascalCase"))]
#[derive(Debug, Default, PartialEq)]
pub struct Launchd {
    label: String,
    disabled: Option<bool>,
    user_name: Option<String>,
    group_name: Option<String>,
    #[cfg_attr(feature = "serde", serde(rename = "inetdCompatibility"))]
    inetd_compatibility: Option<HashMap<InetdCompatibility, bool>>,
    limit_load_to_hosts: Option<Vec<String>>,
    limit_load_from_hosts: Option<Vec<String>>,
    limit_load_to_session_type: Option<LoadSessionType>,
    limit_load_to_hardware: Option<HashMap<String, Vec<String>>>,
    limit_load_from_hardware: Option<HashMap<String, Vec<String>>>,
    program: Option<String>, // TODO: Ensure this: "NOTE: The Program key must be an absolute path."
    bundle_program: Option<String>,
    program_arguments: Option<Vec<String>>,
    enable_globbing: Option<bool>,
    enable_transactions: Option<bool>,
    enable_pressured_exit: Option<bool>,
    on_demand: Option<bool>, // NB: deprecated (see KeepAlive), but still needed for reading old plists.
    #[cfg_attr(feature = "serde", serde(rename = "ServiceIPC"))]
    service_ipc: Option<bool>, // NB: "Please remove this key from your launchd.plist."
    keep_alive: Option<KeepAliveType>,
    run_at_load: Option<bool>,
    root_directory: Option<String>,
    working_directory: Option<String>,
    environment_variables: Option<HashMap<String, String>>,
    umask: Option<u16>, // NB: This is a Unix permission mask. Defined as: typedef __uint16_t __darwin_mode_t;
    time_out: Option<u32>,
    exit_time_out: Option<u32>,
    throttle_interval: Option<u32>,
    init_groups: Option<bool>,
    watch_paths: Option<Vec<String>>,
    queue_directories: Option<Vec<String>>,
    start_on_mount: Option<bool>,
    start_interval: Option<u32>,
    start_calendar_intervals: Option<Vec<CalendarInterval>>,
    standard_in_path: Option<String>,
    standard_out_path: Option<String>,
    standard_error_path: Option<String>,
    debug: Option<bool>,
    wait_for_debugger: Option<bool>,
    soft_resource_limits: Option<ResourceLimits>,
    hard_resource_limits: Option<ResourceLimits>,
    nice: Option<i32>,
    process_type: Option<ProcessType>,
    abandon_process_group: Option<bool>,
    #[cfg_attr(feature = "serde", serde(rename = "LowPriorityIO"))]
    low_priority_io: Option<bool>,
    #[cfg_attr(feature = "serde", serde(rename = "LowPriorityBackgroundIO"))]
    low_priority_background_io: Option<bool>,
    materialize_dataless_files: Option<bool>,
    launch_only_once: Option<bool>,
    mach_services: Option<HashMap<String, MachServiceEntry>>,
    sockets: Option<Sockets>,
    launch_events: Option<LaunchEvents>,
    hopefully_exits_last: Option<bool>, // NB: Deprecated, keep for reading old plists.
    hopefully_exits_first: Option<bool>, // NB: Deprecated, keep for reading old plists.
    session_create: Option<bool>,
    legacy_timers: Option<bool>, // NB: Deprecated, keep for reading old plists.
                                 // associated_bundle_identifiers: Option<<string or array of strings>>
}

// Defined as a "<dictionary of dictionaries of dictionaries>" in launchd.plist(5)
// Use plist::Value as the value can be String, Integer, Boolean, etc.
// Doing this precludes the use of #[derive(Eq)] on the Launchd struct, but in practice "PartialEq" is fine.
#[cfg(feature = "plist")]
type LaunchEvents = HashMap<String, HashMap<String, HashMap<String, Value>>>;

// TODO: the current implementation is dependent on plist. Is this necessary?
#[cfg(not(feature = "plist"))]
type LaunchEvents = ();

/// Representation of a CalendarInterval
///
/// Usage:
/// ```
/// use launchd::{CalendarInterval, Error};
/// fn example() -> Result<(), Error> {
///     let calendarinterval = CalendarInterval::default()
///             .with_hour(12)?
///             .with_minute(0)?
///             .with_weekday(7);
///     Ok(())
/// }
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "io", serde(rename_all = "PascalCase"))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct CalendarInterval {
    minute: Option<u8>,
    hour: Option<u8>,
    day: Option<u8>,
    weekday: Option<u8>,
    month: Option<u8>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum InetdCompatibility {
    Wait, // Exclude a "NoWait" as that is not a valid key.
}

// Move LoadSessionType to it's own module?
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoadSessionType {
    BareString(String),
    Array(Vec<String>),
}

impl From<String> for LoadSessionType {
    fn from(value: String) -> Self {
        LoadSessionType::BareString(value)
    }
}

impl From<&str> for LoadSessionType {
    fn from(value: &str) -> Self {
        LoadSessionType::BareString(value.to_owned())
    }
}

impl From<Vec<String>> for LoadSessionType {
    fn from(value: Vec<String>) -> Self {
        LoadSessionType::Array(value)
    }
}

impl From<Vec<&str>> for LoadSessionType {
    fn from(value: Vec<&str>) -> Self {
        LoadSessionType::Array(value.into_iter().map(|s| s.to_owned()).collect())
    }
}

// TODO: This can be generated by a macro (maybe derive_builder?)
impl Launchd {
    pub fn new<S: AsRef<str>, P: AsRef<Path>>(label: S, program: P) -> Result<Self, Error> {
        let pathstr = program
            .as_ref()
            .to_str()
            .ok_or(Error::PathConversion)?
            .to_owned();
        Ok(Launchd {
            label: String::from(label.as_ref()),
            program: Some(pathstr),
            ..Default::default()
        })
    }

    pub fn with_label<S: AsRef<str>>(mut self, label: S) -> Self {
        self.label = String::from(label.as_ref());
        self
    }

    pub fn with_disabled(mut self, disabled: bool) -> Self {
        self.disabled = Some(disabled);
        self
    }

    pub fn disabled(self) -> Self {
        self.with_disabled(true)
    }

    pub fn with_user_name<S: AsRef<str>>(mut self, user_name: S) -> Self {
        self.user_name = Some(String::from(user_name.as_ref()));
        self
    }

    pub fn with_group_name<S: AsRef<str>>(mut self, group_name: S) -> Self {
        self.group_name = Some(String::from(group_name.as_ref()));
        self
    }

    pub fn with_program<P: AsRef<Path>>(mut self, program: P) -> Result<Self, Error> {
        let pathstr = program
            .as_ref()
            .to_str()
            .ok_or(Error::PathConversion)?
            .to_owned();
        self.program = Some(pathstr);
        Ok(self)
    }

    pub fn with_bundle_program<S: AsRef<str>>(mut self, bundle: S) -> Self {
        self.bundle_program = Some(String::from(bundle.as_ref()));
        self
    }

    pub fn with_program_arguments(mut self, program_arguments: Vec<String>) -> Self {
        self.program_arguments = Some(program_arguments);
        self
    }

    pub fn with_run_at_load(mut self, run_at_load: bool) -> Self {
        self.run_at_load = Some(run_at_load);
        self
    }

    pub fn run_at_load(self) -> Self {
        self.with_run_at_load(true)
    }

    pub fn with_queue_directories(mut self, queue_directories: Vec<String>) -> Self {
        self.queue_directories = Some(queue_directories);
        self
    }

    pub fn with_watch_paths(mut self, watch_paths: Vec<String>) -> Self {
        self.watch_paths = Some(watch_paths);
        self
    }

    pub fn with_start_on_mount(mut self, start_on_mount: bool) -> Self {
        self.start_on_mount = Some(start_on_mount);
        self
    }

    pub fn start_on_mount(self) -> Self {
        self.with_start_on_mount(true)
    }

    pub fn with_start_interval(mut self, start_interval: u32) -> Self {
        self.start_interval = Some(start_interval);
        self
    }

    pub fn with_start_calendar_intervals(
        mut self,
        start_calendar_intervals: Vec<CalendarInterval>,
    ) -> Self {
        self.start_calendar_intervals = Some(start_calendar_intervals);
        self
    }

    pub fn with_abandon_process_group(mut self, value: bool) -> Self {
        self.abandon_process_group = Some(value);
        self
    }

    pub fn abandon_process_group(self) -> Self {
        self.with_abandon_process_group(true)
    }

    pub fn with_debug(mut self, value: bool) -> Self {
        self.debug = Some(value);
        self
    }

    pub fn debug(self) -> Self {
        self.with_debug(true)
    }

    pub fn with_enable_globbing(mut self, value: bool) -> Self {
        self.enable_globbing = Some(value);
        self
    }

    pub fn enable_globbing(self) -> Self {
        self.with_enable_globbing(true)
    }

    pub fn with_enable_transactions(mut self, value: bool) -> Self {
        self.enable_transactions = Some(value);
        self
    }

    pub fn enable_transactions(self) -> Self {
        self.with_enable_transactions(true)
    }

    pub fn with_enable_pressured_exit(mut self, value: bool) -> Self {
        self.enable_pressured_exit = Some(value);
        self
    }

    pub fn enable_pressured_exit(self) -> Self {
        self.with_enable_pressured_exit(true)
    }

    pub fn with_environment_variables(mut self, env: HashMap<String, String>) -> Self {
        self.environment_variables = Some(env);
        self
    }

    pub fn with_exit_timeout(mut self, timeout: u32) -> Self {
        self.exit_time_out = Some(timeout);
        self
    }

    pub fn with_init_groups(mut self, value: bool) -> Self {
        self.init_groups = Some(value);
        self
    }

    pub fn init_groups(self) -> Self {
        self.with_init_groups(true)
    }

    pub fn with_launch_only_once(mut self, value: bool) -> Self {
        self.launch_only_once = Some(value);
        self
    }

    pub fn launch_only_once(self) -> Self {
        self.with_launch_only_once(true)
    }

    pub fn with_limit_load_from_hosts(mut self, value: Vec<String>) -> Self {
        self.limit_load_from_hosts = Some(value);
        self
    }

    pub fn with_limit_to_from_hosts(mut self, value: Vec<String>) -> Self {
        self.limit_load_to_hosts = Some(value);
        self
    }

    pub fn with_limit_load_to_session_type(mut self, value: LoadSessionType) -> Self {
        self.limit_load_to_session_type = Some(value);
        self
    }

    pub fn with_limit_load_to_hardware(mut self, value: HashMap<String, Vec<String>>) -> Self {
        self.limit_load_to_hardware = Some(value);
        self
    }

    pub fn with_limit_load_from_hardware(mut self, value: HashMap<String, Vec<String>>) -> Self {
        self.limit_load_from_hardware = Some(value);
        self
    }

    pub fn with_low_priority_io(mut self, value: bool) -> Self {
        self.low_priority_io = Some(value);
        self
    }

    pub fn low_priority_io(self) -> Self {
        self.with_low_priority_io(true)
    }

    pub fn with_low_priority_background_io(mut self, value: bool) -> Self {
        self.low_priority_background_io = Some(value);
        self
    }

    pub fn low_priority_background_io(self) -> Self {
        self.with_low_priority_background_io(true)
    }

    pub fn with_mach_services(mut self, services: HashMap<String, MachServiceEntry>) -> Self {
        self.mach_services = Some(services);
        self
    }

    pub fn with_nice(mut self, nice: i32) -> Self {
        self.nice = Some(nice);
        self
    }

    pub fn with_root_directory<P: AsRef<Path>>(mut self, path: P) -> Result<Self, Error> {
        let pathstr = path
            .as_ref()
            .to_str()
            .ok_or(Error::PathConversion)?
            .to_owned();
        self.root_directory = Some(pathstr);
        Ok(self)
    }

    pub fn with_standard_error_path<P: AsRef<Path>>(mut self, path: P) -> Result<Self, Error> {
        let pathstr = path
            .as_ref()
            .to_str()
            .ok_or(Error::PathConversion)?
            .to_owned();
        self.standard_error_path = Some(pathstr);
        Ok(self)
    }

    pub fn with_standard_in_path<P: AsRef<Path>>(mut self, path: P) -> Result<Self, Error> {
        let pathstr = path
            .as_ref()
            .to_str()
            .ok_or(Error::PathConversion)?
            .to_owned();
        self.standard_in_path = Some(pathstr);
        Ok(self)
    }

    pub fn with_standard_out_path<P: AsRef<Path>>(mut self, path: P) -> Result<Self, Error> {
        let pathstr = path
            .as_ref()
            .to_str()
            .ok_or(Error::PathConversion)?
            .to_owned();
        self.standard_out_path = Some(pathstr);
        Ok(self)
    }

    pub fn with_throttle_interval(mut self, value: u32) -> Self {
        self.throttle_interval = Some(value);
        self
    }

    pub fn with_timeout(mut self, timeout: u32) -> Self {
        self.time_out = Some(timeout);
        self
    }

    pub fn with_umask(mut self, umask: u16) -> Self {
        self.umask = Some(umask);
        self
    }

    pub fn with_wait_for_debugger(mut self, value: bool) -> Self {
        self.wait_for_debugger = Some(value);
        self
    }

    pub fn wait_for_debugger(self) -> Self {
        self.with_wait_for_debugger(true)
    }

    pub fn with_materialize_dataless_files(mut self, value: bool) -> Self {
        self.materialize_dataless_files = Some(value);
        self
    }

    pub fn materialize_dataless_files(self) -> Self {
        self.with_materialize_dataless_files(true)
    }

    pub fn with_working_directory<P: AsRef<Path>>(mut self, path: P) -> Result<Self, Error> {
        let pathstr = path
            .as_ref()
            .to_str()
            .ok_or(Error::PathConversion)?
            .to_owned();
        self.working_directory = Some(pathstr);
        Ok(self)
    }

    pub fn with_inetd_compatibility(mut self, wait: bool) -> Self {
        self.inetd_compatibility = Some(HashMap::from([(InetdCompatibility::Wait, wait)]));
        self
    }

    pub fn with_keep_alive(mut self, keep_alive: KeepAliveType) -> Self {
        self.keep_alive = Some(keep_alive);
        self
    }

    pub fn with_process_type(mut self, process_type: ProcessType) -> Self {
        self.process_type = Some(process_type);
        self
    }

    pub fn with_hard_resource_limits(mut self, limits: ResourceLimits) -> Self {
        self.hard_resource_limits = Some(limits);
        self
    }

    pub fn with_soft_resource_limits(mut self, limits: ResourceLimits) -> Self {
        self.soft_resource_limits = Some(limits);
        self
    }

    pub fn with_socket(mut self, socket: Sockets) -> Self {
        if let Some(sockets) = self.sockets.take() {
            match (sockets, socket) {
                (Sockets::Array(mut arr), Sockets::Array(mut new_arr)) => {
                    arr.append(&mut new_arr);
                    self.sockets = Some(Sockets::Array(arr));
                }
                (Sockets::Array(mut arr), Sockets::Dictionary(new_dict)) => {
                    arr.push(new_dict);
                    self.sockets = Some(Sockets::Array(arr));
                }
                (Sockets::Dictionary(dict), Sockets::Dictionary(new_dict)) => {
                    self.sockets = Some(Sockets::Array(vec![dict, new_dict]))
                }
                (Sockets::Dictionary(dict), Sockets::Array(mut new_arr)) => {
                    new_arr.insert(0, dict);
                    self.sockets = Some(Sockets::Array(new_arr));
                }
            }
        } else {
            self.sockets = Some(socket);
        }
        self
    }

    pub fn with_launch_events(mut self, value: LaunchEvents) -> Self {
        self.launch_events = Some(value);
        self
    }

    pub fn with_session_create(mut self, value: bool) -> Self {
        self.session_create = Some(value);
        self
    }

    pub fn session_create(self) -> Self {
        self.with_session_create(true)
    }
}

#[cfg(feature = "io")]
impl Launchd {
    // Write --
    pub fn to_writer_xml<W: Write>(&self, writer: W) -> Result<(), Error> {
        to_writer_xml(writer, self).map_err(Error::Write)
    }

    pub fn to_file_xml<P: AsRef<Path>>(&self, file: P) -> Result<(), Error> {
        to_file_xml(file, self).map_err(Error::Write)
    }

    pub fn to_writer_binary<W: Write>(&self, writer: W) -> Result<(), Error> {
        to_writer_binary(writer, self).map_err(Error::Write)
    }

    pub fn to_file_binary<P: AsRef<Path>>(&self, file: P) -> Result<(), Error> {
        to_file_binary(file, self).map_err(Error::Write)
    }

    // Read --
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
        from_bytes(bytes).map_err(Error::Read)
    }

    pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Self, Error> {
        from_file(file).map_err(Error::Read)
    }

    pub fn from_reader<R: Read + Seek>(reader: R) -> Result<Self, Error> {
        from_reader(reader).map_err(Error::Read)
    }

    pub fn from_reader_xml<R: Read + Seek>(reader: R) -> Result<Self, Error> {
        from_reader_xml(reader).map_err(Error::Read)
    }
}

impl CalendarInterval {
    #[cfg(feature = "cron")] // This has some use for launchd::with_start_calendar_intervals as well
    fn is_initialized(&self) -> bool {
        self.minute.is_some()
            || self.hour.is_some()
            || self.day.is_some()
            || self.weekday.is_some()
            || self.month.is_some()
    }

    pub fn with_minute(self, minute: u8) -> Result<Self, Error> {
        if minute > 59 {
            Err(Error::CalendarFieldOutOfBounds(0..=59, minute))
        } else {
            let mut result = self;
            result.minute = Some(minute);
            Ok(result)
        }
    }

    pub fn with_hour(self, hour: u8) -> Result<Self, Error> {
        if hour > 23 {
            Err(Error::CalendarFieldOutOfBounds(0..=23, hour))
        } else {
            let mut result = self;
            result.hour = Some(hour);
            Ok(result)
        }
    }

    pub fn with_day(self, day: u8) -> Result<Self, Error> {
        if day == 0 || day > 31 {
            Err(Error::CalendarFieldOutOfBounds(1..=31, day))
        } else {
            let mut result = self;
            result.day = Some(day);
            Ok(result)
        }
    }

    pub fn with_weekday(self, weekday: u8) -> Result<Self, Error> {
        if weekday > 7 {
            Err(Error::CalendarFieldOutOfBounds(0..=7, weekday))
        } else {
            let mut result = self;
            result.weekday = Some(weekday);
            Ok(result)
        }
    }

    pub fn with_month(self, month: u8) -> Result<Self, Error> {
        if month == 0 || month > 12 {
            Err(Error::CalendarFieldOutOfBounds(1..=12, month))
        } else {
            let mut result = self;
            result.month = Some(month);
            Ok(result)
        }
    }

    #[cfg(feature = "cron")]
    pub fn from_cron_schedule(schedule: Schedule) -> Result<Vec<Self>, Error> {
        let mut result_vec = Vec::new();
        for month in schedule.months().iter() {
            for weekday in schedule.days_of_week().iter() {
                for day in schedule.days_of_month().iter() {
                    for hour in schedule.hours().iter() {
                        for minute in schedule.minutes().iter() {
                            let result = Self::default();

                            // TODO: clean this mess up (thiserror + anyhow ?)
                            if !schedule.months().is_all() {
                                result.with_month(
                                    month
                                        .try_into()
                                        .map_err(|_| Error::InvalidCronField(month))?,
                                )?;
                            }
                            if !schedule.days_of_week().is_all() {
                                result.with_weekday(
                                    weekday
                                        .try_into()
                                        .map_err(|_| Error::InvalidCronField(weekday))?,
                                )?;
                            }
                            if !schedule.days_of_month().is_all() {
                                result.with_day(
                                    day.try_into().map_err(|_| Error::InvalidCronField(day))?,
                                )?;
                            }
                            if !schedule.hours().is_all() {
                                result.with_hour(
                                    hour.try_into().map_err(|_| Error::InvalidCronField(hour))?,
                                )?;
                            }
                            if !schedule.minutes().is_all() {
                                result.with_minute(
                                    minute
                                        .try_into()
                                        .map_err(|_| Error::InvalidCronField(minute))?,
                                )?;
                            }

                            if result.is_initialized() {
                                result_vec.push(result);
                            }

                            if schedule.minutes().is_all() {
                                break;
                            }
                        }
                        if schedule.hours().is_all() {
                            break;
                        }
                    }
                    if schedule.days_of_month().is_all() {
                        break;
                    }
                }
                if schedule.days_of_week().is_all() {
                    break;
                }
            }
            if schedule.months().is_all() {
                break;
            }
        }
        Ok(result_vec)
    }
}

#[cfg(test)]
mod tests {

    #[cfg(feature = "io")]
    macro_rules! test_case {
        ($fname:expr) => {
            concat!(env!("CARGO_MANIFEST_DIR"), "/tests/resources/", $fname)
        };
    }

    use super::*;

    #[test]
    fn create_valid_launchd() {
        let check = Launchd {
            abandon_process_group: None,
            debug: None,
            disabled: None,
            enable_globbing: None,
            enable_transactions: None,
            enable_pressured_exit: None,
            on_demand: None,
            service_ipc: None,
            environment_variables: None,
            exit_time_out: None,
            group_name: None,
            inetd_compatibility: None,
            init_groups: None,
            hard_resource_limits: None,
            keep_alive: None,
            label: "Label".to_string(),
            launch_only_once: None,
            launch_events: None,
            legacy_timers: None,
            limit_load_from_hosts: None,
            limit_load_to_hosts: None,
            limit_load_to_session_type: None,
            limit_load_to_hardware: None,
            limit_load_from_hardware: None,
            low_priority_io: None,
            low_priority_background_io: None,
            hopefully_exits_first: None,
            hopefully_exits_last: None,
            mach_services: None,
            materialize_dataless_files: None,
            session_create: None,
            nice: None,
            process_type: None,
            program_arguments: None,
            program: Some("./henk.sh".to_string()),
            bundle_program: None,
            queue_directories: None,
            root_directory: None,
            run_at_load: None,
            sockets: None,
            soft_resource_limits: None,
            standard_error_path: None,
            standard_in_path: None,
            standard_out_path: None,
            start_calendar_intervals: None,
            start_interval: None,
            start_on_mount: None,
            throttle_interval: None,
            time_out: None,
            umask: None,
            user_name: None,
            wait_for_debugger: None,
            watch_paths: None,
            working_directory: None,
        };
        let test = Launchd::new("Label", "./henk.sh");
        assert!(test.is_ok());
        assert_eq!(test.unwrap(), check);
    }

    #[test]
    fn create_valid_calendar_interval() {
        let check = CalendarInterval {
            minute: Some(5),
            hour: Some(5),
            day: Some(5),
            weekday: Some(5),
            month: Some(5),
        };

        let test = CalendarInterval::default()
            .with_day(5)
            .and_then(|ci| ci.with_minute(5))
            .and_then(|ci| ci.with_hour(5))
            .and_then(|ci| ci.with_weekday(5))
            .and_then(|ci| ci.with_month(5));

        assert!(test.is_ok());
        assert_eq!(test.unwrap(), check);
    }

    #[test]
    fn create_invalid_calendar_interval() {
        let test = CalendarInterval::default()
            .with_day(32)
            .and_then(|ci| ci.with_minute(5))
            .and_then(|ci| ci.with_hour(5))
            .and_then(|ci| ci.with_weekday(5))
            .and_then(|ci| ci.with_month(5));
        assert!(test.is_err());
        eprintln!("{}", test.unwrap_err());
    }

    #[test]
    #[cfg(feature = "io")]
    fn load_complex_launch_events_1_plist() {
        let test = Launchd::from_file(test_case!("launchevents-1.plist")).unwrap();

        match test.launch_events {
            Some(events) => assert!(events.contains_key("com.apple.distnoted.matching")),
            _ => panic!("No launch events found"),
        };
    }

    #[test]
    #[cfg(feature = "io")]
    fn load_complex_launch_events_2_plist() {
        let check: LaunchEvents = vec![(
            "com.apple.iokit.matching".to_string(),
            vec![(
                "com.apple.device-attach".to_string(),
                vec![
                    ("IOMatchLaunchStream".to_string(), Value::from(true)),
                    ("idProduct".to_string(), Value::from("*")),
                    ("idVendor".to_string(), Value::from(4176)),
                    ("IOProviderClass".to_string(), Value::from("IOUSBDevice")),
                ]
                .into_iter()
                .collect(),
            )]
            .into_iter()
            .collect(),
        )]
        .into_iter()
        .collect();

        let test = Launchd::from_file(test_case!("launchevents-2.plist")).unwrap();

        match test.launch_events {
            Some(events) => assert_eq!(events, check),
            _ => panic!("No launch events found"),
        };
    }

    #[test]
    #[cfg(feature = "io")]
    fn load_complex_machservices_1_plist() {
        let check = vec![
            (
                "com.apple.private.alloy.accessibility.switchcontrol-idswake".to_string(),
                MachServiceEntry::from(true),
            ),
            (
                "com.apple.AssistiveControl.startup".to_string(),
                MachServiceEntry::from(MachServiceOptions::new().reset_at_close()),
            ),
            (
                "com.apple.AssistiveControl.running".to_string(),
                MachServiceEntry::from(
                    MachServiceOptions::new()
                        .hide_until_check_in()
                        .reset_at_close(),
                ),
            ),
        ]
        .into_iter()
        .collect();

        let test = Launchd::from_file(test_case!("machservices-1.plist")).unwrap();

        match test.mach_services {
            Some(events) => assert_eq!(events, check),
            _ => panic!("No launch events found"),
        };
    }
}