pptr 0.3.0

Type-Driven Asynchronous Actor Runtime
Documentation
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
//! Error types and related functionality for the `pptr` library.
//!
//! This module defines various error types that can occur within the `pptr` library, such as
//! errors related to puppets, resources, permissions, and message handling. It provides a
//! consistent and structured way to handle and propagate errors throughout the library.
//!
//! The main error types defined in this module include:
//!
//! - [`PuppetDoesNotExistError`]: Represents an error when a referenced puppet does not exist.
//! - [`ResourceAlreadyExist`]: Represents an error when attempting to create a resource that already exists.
//! - [`PuppetAlreadyExist`]: Represents an error when attempting to create a puppet with an ID that already exists.
//! - [`PermissionDeniedError`]: Represents an error when a master tries to perform an operation on a puppet without sufficient permissions.
//! - [`PuppetCannotHandleMessage`]: Represents an error when a puppet cannot handle a message due to its current lifecycle status.
//! - [`NonCriticalError`]: Represents a non-critical error that occurred in a puppet.
//! - [`CriticalError`]: Represents a critical error that occurred in a puppet.
//! - [`PuppetError`]: An enum that represents either a non-critical or critical error in a puppet.
//! - [`RetryError`]: Represents an error that occurs when the maximum retry limit is reached.
//! - [`PuppetSendMessageError`]: Represents an error that can occur when sending a message to a puppet.
//! - [`PuppetSendCommandError`]: Represents an error that can occur when sending a command to a puppet.
//! - [`PostmanError`]: Represents errors that can occur in the postman.
//! - [`PuppetRegisterError`]: Represents errors that can occur when registering a puppet.
//! - [`PuppetOperationError`]: Represents errors that can occur during puppet operations.
//!
//! These error types provide detailed information about the nature of the error, including the
//! associated puppet ID, error messages, and other relevant details. They are designed to be used
//! in conjunction with the `thiserror` crate for easy error handling and propagation.
use std::fmt;

use thiserror::Error;

use crate::{
    pid::{Id, Pid},
    puppet::{Puppet, PuppetStatus},
};

/// Error returned when a referenced puppet does not exist.
///
/// This error is constructed with the [`Pid`] of the non-existent puppet.
#[derive(Debug, Error)]
#[error("Puppet does not exist: {puppet}")]
pub struct PuppetDoesNotExistError {
    pub(crate) puppet: Pid,
}

impl PuppetDoesNotExistError {
    /// Creates a new `PuppetDoesNotExistError` with the given `puppet` ID.
    ///
    /// The `puppet` parameter is the [`Pid`] of the non-existent puppet that caused the error.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::pid::Pid;
    /// # use pptr::errors::PuppetDoesNotExistError;
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let pid = Pid::new::<SomePuppet>();
    /// let err = PuppetDoesNotExistError::new(pid);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn new(puppet: Pid) -> Self {
        Self { puppet }
    }

    /// Creates a new `PuppetDoesNotExistError` for a non-existent puppet of type `P`.
    ///
    /// This function generates a new [`Pid`] for the given puppet type `P` and constructs
    /// a `PuppetDoesNotExistError` with that ID.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::PuppetDoesNotExistError;
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let err = PuppetDoesNotExistError::from_type::<SomePuppet>();
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn from_type<P>() -> Self
    where
        P: Puppet,
    {
        Self::new(Pid::new::<P>())
    }
}

impl From<PuppetDoesNotExistError> for PuppetError {
    fn from(value: PuppetDoesNotExistError) -> Self {
        Self::critical(value.puppet, &value)
    }
}

/// Error type representing a resource that already exists.
///
/// This error is returned when attempting to create a resource that already exists,
/// as identified by its unique `id`.
#[derive(Debug, Error)]
#[error("Resource already exist")]
pub struct ResourceAlreadyExist {
    pub(crate) id: Id,
}

impl ResourceAlreadyExist {
    /// Creates a new `ResourceAlreadyExist` error with the given `id`.
    ///
    /// The `id` parameter is the unique identifier of the existing resource that caused the error.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::ResourceAlreadyExist;
    /// # use pptr::pid::Id;
    /// let id = Id::new::<i32>();
    /// let error = ResourceAlreadyExist::new(id);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn new(id: Id) -> Self {
        Self { id }
    }
}

/// Error type representing a puppet that already exists.
///
/// This error is returned when attempting to create a puppet with an ID that is
/// already associated with an existing puppet.
#[derive(Debug, Error)]
#[error("Puppet already exist: {puppet}")]
pub struct PuppetAlreadyExist {
    pub(crate) puppet: Pid,
}

impl From<PuppetAlreadyExist> for PuppetError {
    fn from(value: PuppetAlreadyExist) -> Self {
        Self::critical(value.puppet, &value)
    }
}

impl PuppetAlreadyExist {
    /// Creates a new `PuppetAlreadyExist` error with the given `puppet` ID.
    ///
    /// The `puppet` parameter is the [`Pid`] of the already existing puppet that caused the error.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::pid::Pid;
    /// # use pptr::errors::PuppetAlreadyExist;
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let pid = Pid::new::<SomePuppet>();
    /// let err = PuppetAlreadyExist::new(pid);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn new(puppet: Pid) -> Self {
        Self { puppet }
    }

    /// Creates a new `PuppetAlreadyExist` error for an existing puppet of type `P`.
    ///
    /// This function generates a new [`Pid`] for the given puppet type `P` and constructs
    /// a `PuppetAlreadyExist` error with that ID.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::PuppetAlreadyExist;
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let err = PuppetAlreadyExist::from_type::<SomePuppet>();
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn from_type<P>() -> Self
    where
        P: Puppet,
    {
        Self::new(Pid::new::<P>())
    }
}

/// Error type representing a permission denied error.
///
/// This error occurs when a master tries to perform an operation on a puppet
/// without sufficient permissions.
///
/// # Example
///
/// ```
/// # use pptr::errors::PermissionDeniedError;
/// # use pptr::pid::Pid;
/// #
/// # #[derive(Debug, Clone, Default)]
/// # struct SomePuppet;
/// # impl pptr::puppet::Puppet for SomePuppet {
/// #     type Supervision = pptr::supervision::strategy::OneToOne;
/// # }
/// #
/// # #[derive(Debug, Clone, Default)]
/// # struct Master;
/// # impl pptr::puppet::Puppet for Master {
/// #     type Supervision = pptr::supervision::strategy::OneToOne;
/// # }
/// let master_pid = Pid::new::<SomePuppet>();
/// let puppet_pid = Pid::new::<Master>();
/// let error = PermissionDeniedError {
///     master: master_pid,
///     puppet: puppet_pid,
///     message: Some("Operation not allowed".to_string()),
/// };
/// ```
#[derive(Debug, Error)]
pub struct PermissionDeniedError {
    pub master: Pid,
    pub puppet: Pid,
    pub message: Option<String>,
}

impl fmt::Display for PermissionDeniedError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Permission denied. Master: {}, Puppet: {}, Message: {}",
            self.master,
            self.puppet,
            self.message_or_default()
        )
    }
}

impl PermissionDeniedError {
    #[must_use]
    /// Creates a new `PermissionDeniedError` with the given master and puppet PIDs.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::PermissionDeniedError;
    /// # use pptr::pid::Pid;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct Master;
    /// # impl pptr::puppet::Puppet for Master {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let master_pid = Pid::new::<Master>();
    /// let puppet_pid = Pid::new::<SomePuppet>();
    /// let error = PermissionDeniedError::new(master_pid, puppet_pid);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn new(master: Pid, puppet: Pid) -> Self {
        Self {
            master,
            puppet,
            message: None,
        }
    }
    /// Creates a new `PermissionDeniedError` using the generic types `M` and `P` to infer the master and puppet PIDs.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::PermissionDeniedError;
    /// # use pptr::pid::Pid;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct Master;
    /// # impl pptr::puppet::Puppet for Master {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let master_pid = Pid::new::<Master>();
    /// let puppet_pid = Pid::new::<SomePuppet>();
    /// let error = PermissionDeniedError::from_type::<Master, SomePuppet>();
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn from_type<P, M>() -> Self
    where
        P: Puppet,
        M: Puppet,
    {
        Self::new(Pid::new::<M>(), Pid::new::<P>())
    }

    /// Sets a custom error message for the `PermissionDeniedError`.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::PermissionDeniedError;
    /// # use pptr::pid::Pid;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct Master;
    /// # impl pptr::puppet::Puppet for Master {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let master_pid = Pid::new::<Master>();
    /// let puppet_pid = Pid::new::<SomePuppet>();
    /// let error = PermissionDeniedError::new(master_pid, puppet_pid)
    ///     .with_message("Operation not allowed");
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn with_message<T: Into<String>>(mut self, message: T) -> Self {
        self.message = Some(message.into());
        self
    }

    /// Returns the error message if set, or a default message if not.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::PermissionDeniedError;
    /// # use pptr::pid::Pid;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct Master;
    /// # impl pptr::puppet::Puppet for Master {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let master_pid = Pid::new::<Master>();
    /// let puppet_pid = Pid::new::<SomePuppet>();
    /// let error = PermissionDeniedError::new(master_pid, puppet_pid);
    /// assert_eq!(error.message_or_default(), "No message");
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn message_or_default(&self) -> String {
        self.message
            .clone()
            .unwrap_or_else(|| "No message".to_owned())
    }
}

impl From<PermissionDeniedError> for PuppetError {
    fn from(value: PermissionDeniedError) -> Self {
        Self::critical(value.puppet, &value)
    }
}

/// Error type representing a scenario where a puppet cannot handle a message due to its current lifecycle status.
///
/// This error occurs when a message is sent to a puppet that is not in a state to handle the message based on its
/// current lifecycle status. The error includes the puppet's PID and the current lifecycle status.
#[derive(Debug, Error)]
#[error("Puppet {puppet} cannot handle message. Status: {status}.")]
pub struct PuppetCannotHandleMessage {
    pub puppet: Pid,
    pub status: PuppetStatus,
}

impl From<PuppetCannotHandleMessage> for PuppetError {
    fn from(value: PuppetCannotHandleMessage) -> Self {
        Self::non_critical(value.puppet, &value)
    }
}

impl PuppetCannotHandleMessage {
    /// Creates a new `PuppetCannotHandleMessage` error using the specified puppet PID and lifecycle status.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::PuppetCannotHandleMessage;
    /// # use pptr::pid::Pid;
    /// # use pptr::puppet::PuppetStatus;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct Puppet;
    /// # impl pptr::puppet::Puppet for Puppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let puppet_pid = Pid::new::<Puppet>();
    /// let status = PuppetStatus::Inactive;
    /// let error = PuppetCannotHandleMessage::new(puppet_pid, status);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn new(puppet: Pid, status: PuppetStatus) -> Self {
        Self { puppet, status }
    }
    /// Creates a new `PuppetCannotHandleMessage` error using the generic type `P` to infer the puppet PID.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::PuppetCannotHandleMessage;
    /// # use pptr::pid::Pid;
    /// # use pptr::puppet::PuppetStatus;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// let status = PuppetStatus::Inactive;
    /// let error = PuppetCannotHandleMessage::from_type::<SomePuppet>(status);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    #[must_use]
    pub fn from_type<P>(status: PuppetStatus) -> Self
    where
        P: Puppet,
    {
        Self::new(Pid::new::<P>(), status)
    }
}

/// Non-critical error type representing an error that occurred in a puppet.
///
/// This error indicates a non-critical error condition in a puppet, meaning that the supervisor will not be notified.
/// The error includes the puppet's PID and an error message describing the issue.
#[derive(Error, Debug, Clone)]
#[error(
    "Non-critical error occurred in puppet {puppet}: '{message}'. Supervisor will not be notified."
)]
pub struct NonCriticalError {
    pub puppet: Pid,
    pub message: String,
}

/// Critical error type representing a severe error that occurred in a puppet.
///
/// This error indicates a critical error condition in a puppet, meaning that the supervisor will be notified.
/// The error includes the puppet's PID and an error message describing the issue.
#[derive(Error, Debug, Clone)]
#[error("Critical error occurred in puppet {puppet}: '{message}'. Supervisor will be notified.")]
pub struct CriticalError {
    pub puppet: Pid,
    pub message: String,
}

impl CriticalError {
    /// Creates a new `CriticalError` with the specified puppet PID and error message.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::CriticalError;
    /// # use pptr::pid::Pid;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    ///
    /// let puppet_pid = Pid::new::<SomePuppet>();
    /// let error = CriticalError::new(puppet_pid, "Something went wrong");
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn new<T: ToString + ?Sized>(puppet: Pid, message: &T) -> Self {
        Self {
            puppet,
            message: message.to_string(),
        }
    }
}

/// An error type representing errors that can occur in a puppet.
///
/// `PuppetError` is an enum with two variants:
///
/// - `NonCritical`: Represents a non-critical error that occurred in a puppet. This variant does
///   not cause a notification to the supervisor, but is reported if the caller is waiting for a
///   response.
/// - `Critical`: Represents a critical error that occurred in a puppet. This error causes a
///   notification to the supervisor and a restart according to the selected strategy.
#[derive(Error, Debug, Clone)]
pub enum PuppetError {
    #[error(transparent)]
    NonCritical(#[from] NonCriticalError),
    #[error(transparent)]
    Critical(#[from] CriticalError),
}

impl PuppetError {
    /// Constructs a new `PuppetError` variant representing a non-critical error.
    ///
    /// This function creates a `PuppetError::NonCritical` variant from the provided `puppet` identifier and
    /// `message`. The `message` can be any type that implements `ToString` and is converted to a `String`.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::{PuppetError, NonCriticalError};
    /// # use pptr::pid::Pid;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// #
    /// let puppet_pid = Pid::new::<SomePuppet>();
    /// let error = PuppetError::non_critical(puppet_pid, "Something went wrong");
    /// assert!(matches!(error, PuppetError::NonCritical(NonCriticalError { .. })));
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn non_critical<T: ToString + ?Sized>(puppet: Pid, message: &T) -> Self {
        NonCriticalError {
            puppet,
            message: message.to_string(),
        }
        .into()
    }
    /// Constructs a new `PuppetError` variant representing a critical error.
    ///
    /// This function creates a `PuppetError::Critical` variant from the provided `puppet` identifier and
    /// `message`. The `message` can be any type that implements `ToString` and is converted to a `String`.
    ///
    /// # Example
    ///
    /// ```
    /// # use pptr::errors::{PuppetError, CriticalError};
    /// # use pptr::pid::Pid;
    /// #
    /// # #[derive(Debug, Clone, Default)]
    /// # struct SomePuppet;
    /// # impl pptr::puppet::Puppet for SomePuppet {
    /// #     type Supervision = pptr::supervision::strategy::OneToOne;
    /// # }
    /// #
    /// let puppet_pid = Pid::new::<SomePuppet>();
    /// let error = PuppetError::critical(puppet_pid, "Something went wrong");
    /// assert!(matches!(error, PuppetError::Critical(CriticalError { .. })));
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn critical<T: ToString + ?Sized>(puppet: Pid, message: &T) -> Self {
        CriticalError {
            puppet,
            message: message.to_string(),
        }
        .into()
    }
}

/// Represents an error that occurs when the maximum retry limit is reached.
///
/// This error type is used to indicate that an operation has been retried multiple times
/// and has reached the maximum retry limit without succeeding.
///
/// The `message` field contains a string describing the reason for the retry failure.
#[derive(Debug, Error)]
#[error("Reached max retry limit: {message}")]
pub struct RetryError {
    pub message: String,
}

impl RetryError {
    pub fn new<T: ToString + ?Sized>(message: &T) -> Self {
        Self {
            message: message.to_string(),
        }
    }
}

/// Represents an error that can occur when sending a message to a puppet.
///
/// This error type encompasses two possible scenarios:
///
/// - `PuppetDosNotExist`: The specified puppet does not exist.
/// - `PostmanError`: An error occurred in the postman while attempting to send the message.
#[derive(Debug, Error)]
pub enum PuppetSendMessageError {
    #[error(transparent)]
    PuppetDosNotExist(#[from] PuppetDoesNotExistError),
    #[error(transparent)]
    PostmanError(#[from] PostmanError),
}

impl From<PuppetError> for PuppetSendMessageError {
    fn from(err: PuppetError) -> Self {
        PostmanError::PuppetError(err).into()
    }
}

impl PuppetSendMessageError {
    #[must_use]
    pub fn get_puppet_error(&self) -> Option<&PuppetError> {
        match self {
            PuppetSendMessageError::PostmanError(PostmanError::PuppetError(err)) => Some(err),
            _ => None,
        }
    }
}

impl From<PuppetSendMessageError> for PuppetError {
    fn from(err: PuppetSendMessageError) -> Self {
        match err {
            PuppetSendMessageError::PuppetDosNotExist(err) => err.into(),
            PuppetSendMessageError::PostmanError(err) => err.into(),
        }
    }
}

/// Represents an error that can occur when sending a command to a puppet.
///
/// This error type encompasses three possible scenarios:
///
/// - `PuppetDosNotExist`: The specified puppet does not exist.
/// - `PermissionDenied`: The caller does not have permission to send the command to the puppet.
/// - `PostmanError`: An error occurred in the postman while attempting to send the command.
#[derive(Debug, Error)]
pub enum PuppetSendCommandError {
    #[error(transparent)]
    PuppetDosNotExist(#[from] PuppetDoesNotExistError),
    #[error(transparent)]
    PermissionDenied(#[from] PermissionDeniedError),
    #[error(transparent)]
    PostmanError(#[from] PostmanError),
}

impl From<PuppetSendCommandError> for PuppetError {
    fn from(err: PuppetSendCommandError) -> Self {
        match err {
            PuppetSendCommandError::PuppetDosNotExist(err) => err.into(),
            PuppetSendCommandError::PermissionDenied(err) => err.into(),
            PuppetSendCommandError::PostmanError(err) => err.into(),
        }
    }
}

/// Represents errors that can occur in the postman.
///
/// This error type encompasses three possible scenarios:
///
/// - `SendError`: The message could not be sent because the channel is closed.
/// - `ResponseReceiveError`: The response could not be received because the channel is closed.
/// - `PuppetError`: An error occurred in the puppet while processing the message or command.
#[derive(Debug, Error)]
pub enum PostmanError {
    #[error("Can't send message. Channel closed.")]
    SendError { puppet: Pid },
    #[error("Can't receive message. Channel closed.")]
    ResponseReceiveError { puppet: Pid },
    #[error(transparent)]
    PuppetError(#[from] PuppetError),
}

impl From<PostmanError> for PuppetError {
    fn from(err: PostmanError) -> Self {
        match err {
            PostmanError::SendError { puppet } | PostmanError::ResponseReceiveError { puppet } => {
                Self::critical(puppet, &err)
            }
            PostmanError::PuppetError(err) => err,
        }
    }
}

/// Represents errors that can occur when registering a puppet.
///
/// This error type encompasses two possible scenarios:
///
/// - `PuppetDoesNotExist`: The specified puppet does not exist.
/// - `PuppetAlreadyExist`: The specified puppet already exists and cannot be registered again.
///
/// # Panics
///
/// This type does not panic.
#[derive(Debug, Error)]
pub enum PuppetRegisterError {
    #[error(transparent)]
    PuppetDoesNotExist(#[from] PuppetDoesNotExistError),
    #[error(transparent)]
    PuppetAlreadyExist(#[from] PuppetAlreadyExist),
}

impl From<PuppetRegisterError> for PuppetError {
    fn from(err: PuppetRegisterError) -> Self {
        match err {
            PuppetRegisterError::PuppetDoesNotExist(err) => err.into(),
            PuppetRegisterError::PuppetAlreadyExist(err) => err.into(),
        }
    }
}

/// Represents errors that can occur during puppet operations.
///
/// This error type encompasses two possible scenarios:
///
/// - `PermissionDenied`: The caller does not have permission to perform the operation on the puppet.
/// - `PuppetDoesNotExist`: The specified puppet does not exist.
///
/// # Panics
///
/// This type does not panic.
#[derive(Debug, Error)]
pub enum PuppetOperationError {
    #[error(transparent)]
    PermissionDenied(#[from] PermissionDeniedError),
    #[error(transparent)]
    PuppetDoesNotExist(#[from] PuppetDoesNotExistError),
}