veilid-core 0.5.7

Core library used to create a Veilid node and operate it as part of an application
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
use super::*;

/// Return early with a [VeilidAPIError::NotInitialized] error.
#[macro_export]
macro_rules! apibail_not_initialized {
    () => {
        return Err(VeilidAPIError::not_initialized())
    };
}

/// Return early with a [VeilidAPIError::Timeout] error.
#[macro_export]
macro_rules! apibail_timeout {
    () => {
        return Err(VeilidAPIError::timeout())
    };
}

/// Return early with a [VeilidAPIError::TryAgain] error carrying the given message.
#[macro_export]
macro_rules! apibail_try_again {
    ($x:expr) => {
        return Err(VeilidAPIError::try_again($x))
    };
    ($fmt:literal, $($args:tt)*) => {
        return Err(VeilidAPIError::try_again( format!($fmt, $($args)*) ))
    };
}

/// Return early with a [VeilidAPIError::Generic] error carrying the given message.
#[macro_export]
macro_rules! apibail_generic {
    ($x:expr) => {
        return Err(VeilidAPIError::generic($x))
    };
    ($fmt:literal, $($args:tt)*) => {
        return Err(VeilidAPIError::generic( format!($fmt, $($args)*) ))
    };
}

/// Return early with a [VeilidAPIError::Internal] error carrying the given message.
#[macro_export]
macro_rules! apibail_internal {
    ($x:expr) => {
        return Err(VeilidAPIError::internal($x))
    };
    ($fmt:literal, $($args:tt)*) => {
        return Err(VeilidAPIError::internal( format!($fmt, $($args)*) ))
    };
}

/// Return early with a [VeilidAPIError::ParseError] error carrying the given message and value.
#[macro_export]
macro_rules! apibail_parse_error {
    ($x:expr, $y:expr) => {
        return Err(VeilidAPIError::parse_error($x, $y))
    };
}

/// Return early with a [VeilidAPIError::MissingArgument] error naming the calling context and the missing argument.
#[macro_export]
macro_rules! apibail_missing_argument {
    ($x:expr, $y:expr) => {
        return Err(VeilidAPIError::missing_argument($x, $y))
    };
}

/// Return early with a [VeilidAPIError::InvalidArgument] error naming the calling context, the argument, and its rejected value.
#[macro_export]
macro_rules! apibail_invalid_argument {
    ($x:expr, $y:expr, $z:expr) => {
        return Err(VeilidAPIError::invalid_argument($x, $y, $z))
    };
}

/// Return early with a [VeilidAPIError::NoConnection] error carrying the given message.
#[macro_export]
macro_rules! apibail_no_connection {
    ($x:expr) => {
        return Err(VeilidAPIError::no_connection($x))
    };
    ($fmt:literal, $($args: tt)* ) => {
        return Err(VeilidAPIError::no_connection( format!($fmt, arg $($args)*) ))
    };

}

/// Return early with a [VeilidAPIError::KeyNotFound] error carrying the missing record key.
#[macro_export]
macro_rules! apibail_key_not_found {
    ($x:expr) => {
        return Err(VeilidAPIError::key_not_found($x))
    };
}

/// Return early with a [VeilidAPIError::InvalidTarget] error carrying the given message.
#[macro_export]
macro_rules! apibail_invalid_target {
    ($x:expr) => {
        return Err(VeilidAPIError::invalid_target($x))
    };
}

/// Return early with a [VeilidAPIError::TransactionNotFound] error carrying the given message.
#[macro_export]
macro_rules! apibail_transaction_not_found {
    ($x:expr) => {
        return Err(VeilidAPIError::transaction_not_found($x))
    };
    ($fmt:literal, $($args:tt)*) => {
        return Err(VeilidAPIError::transaction_not_found( format!($fmt, $($args)*) ))
    };
}

/// Return early with a [VeilidAPIError::AlreadyInitialized] error.
#[macro_export]
macro_rules! apibail_already_initialized {
    () => {
        return Err(VeilidAPIError::already_initialized())
    };
}

/// Error type returned by all fallible Veilid API operations.
#[apply(api_data_enum!)]
#[api(eq, ord, ts(into_wasm_abi))]
#[derive(ThisError)]
#[serde(tag = "kind")]
pub enum VeilidAPIError {
    /// The API was used before [VeilidAPI](crate::VeilidAPI) was attached, or after it was detached.
    #[error("Not initialized")]
    NotInitialized,
    /// An attempt was made to initialize Veilid while it was already running.
    #[error("Already initialized")]
    AlreadyInitialized,
    /// The operation did not complete within its time budget.
    #[error("Timeout")]
    Timeout,
    /// The operation could not be completed yet and should be retried later.
    #[error("TryAgain: {message}")]
    TryAgain {
        /// Why the operation could not complete this time.
        message: String,
    },
    /// The destination for an operation could not be reached or is malformed.
    #[error("Invalid target: {message}")]
    InvalidTarget {
        /// Details about the unreachable or malformed target.
        message: String,
    },
    /// No network connection could be established to carry out the operation.
    #[error("No connection: {message}")]
    NoConnection {
        /// Details about the connection failure.
        message: String,
    },
    /// The API is shutting down and can no longer service requests.
    #[error("Shutdown")]
    Shutdown,
    /// The requested DHT record key is not present in local storage.
    #[error("Key not found: {key}")]
    KeyNotFound {
        /// The record key that was not found.
        #[cfg_attr(feature = "schemars", schemars(with = "String"))]
        key: OpaqueRecordKey,
    },
    /// An internal invariant was violated; indicates a bug in Veilid itself.
    #[error("Internal: {message}")]
    Internal {
        /// Details about the internal failure.
        message: String,
    },
    /// The requested feature exists in the API surface but is not yet implemented on this platform or build.
    #[error("Unimplemented: {message}")]
    Unimplemented {
        /// Which functionality is unimplemented.
        message: String,
    },
    /// A value could not be parsed into its expected form.
    #[error("Parse error: '{message}' with value '{value}'")]
    ParseError {
        /// What went wrong while parsing.
        message: String,
        /// The input value that failed to parse.
        value: String,
    },
    /// An argument was supplied but its value was rejected.
    #[error("Invalid argument: '{context}' for '{argument}' with value '{value}'")]
    InvalidArgument {
        /// The calling context that rejected the argument.
        context: String,
        /// The name of the offending argument.
        argument: String,
        /// The rejected value.
        value: String,
    },
    /// A required argument was not supplied.
    #[error("Missing argument: '{context}' for '{argument}'")]
    MissingArgument {
        /// The calling context that required the argument.
        context: String,
        /// The name of the missing argument.
        argument: String,
    },
    /// A failure that does not fit any more specific category.
    #[error("Generic: {message}")]
    Generic {
        /// Details about the failure.
        message: String,
    },
    /// The referenced DHT transaction does not exist, having expired or never been opened.
    #[error("Transaction not found: {message}")]
    TransactionNotFound {
        /// Details about the missing transaction.
        message: String,
    },
}

impl VeilidAPIError {
    /// Construct a [VeilidAPIError::NotInitialized] error.
    pub fn not_initialized() -> Self {
        Self::NotInitialized
    }
    /// Construct a [VeilidAPIError::AlreadyInitialized] error.
    pub fn already_initialized() -> Self {
        Self::AlreadyInitialized
    }
    /// Construct a [VeilidAPIError::Timeout] error.
    pub fn timeout() -> Self {
        Self::Timeout
    }
    /// Construct a [VeilidAPIError::TryAgain] error with the given message.
    pub fn try_again<T: ToVeilidAPIErrorArgument>(msg: T) -> Self {
        Self::TryAgain {
            message: msg.to_veilid_api_error_argument(),
        }
    }
    /// Construct a [VeilidAPIError::Shutdown] error.
    pub fn shutdown() -> Self {
        Self::Shutdown
    }
    /// Construct a [VeilidAPIError::InvalidTarget] error with the given message.
    pub fn invalid_target<T: ToVeilidAPIErrorArgument>(msg: T) -> Self {
        Self::InvalidTarget {
            message: msg.to_veilid_api_error_argument(),
        }
    }
    /// Construct a [VeilidAPIError::NoConnection] error with the given message.
    pub fn no_connection<T: ToVeilidAPIErrorArgument>(msg: T) -> Self {
        Self::NoConnection {
            message: msg.to_veilid_api_error_argument(),
        }
    }
    /// Construct a [VeilidAPIError::KeyNotFound] error for the given record key.
    pub fn key_not_found(key: OpaqueRecordKey) -> Self {
        Self::KeyNotFound { key }
    }
    /// Construct a [VeilidAPIError::ParseError] error with the given message and offending value.
    pub fn parse_error<T: ToVeilidAPIErrorArgument, S: ToVeilidAPIErrorArgument>(
        msg: T,
        value: S,
    ) -> Self {
        Self::ParseError {
            message: msg.to_veilid_api_error_argument(),
            value: value.to_veilid_api_error_argument(),
        }
    }
    /// Construct a [VeilidAPIError::InvalidArgument] error naming the context, argument, and rejected value.
    pub fn invalid_argument<
        T: ToVeilidAPIErrorArgument,
        S: ToVeilidAPIErrorArgument,
        R: ToVeilidAPIErrorArgument,
    >(
        context: T,
        argument: S,
        value: R,
    ) -> Self {
        Self::InvalidArgument {
            context: context.to_veilid_api_error_argument(),
            argument: argument.to_veilid_api_error_argument(),
            value: value.to_veilid_api_error_argument(),
        }
    }
    /// Construct a [VeilidAPIError::MissingArgument] error naming the context and the missing argument.
    pub fn missing_argument<T: ToVeilidAPIErrorArgument, S: ToVeilidAPIErrorArgument>(
        context: T,
        argument: S,
    ) -> Self {
        Self::MissingArgument {
            context: context.to_veilid_api_error_argument(),
            argument: argument.to_veilid_api_error_argument(),
        }
    }
    /// Construct a [VeilidAPIError::Generic] error with the given message.
    pub fn generic<T: ToVeilidAPIErrorArgument>(msg: T) -> Self {
        Self::Generic {
            message: msg.to_veilid_api_error_argument(),
        }
    }
    pub(crate) fn transaction_not_found<T: ToVeilidAPIErrorArgument>(msg: T) -> Self {
        Self::TransactionNotFound {
            message: msg.to_veilid_api_error_argument(),
        }
    }

    /// Convert a [NetworkResult] into a [VeilidAPIResult], mapping each non-value outcome to the matching error variant.
    pub fn from_network_result<T>(nr: NetworkResult<T>) -> Result<T, Self> {
        match nr {
            NetworkResult::Timeout => Err(VeilidAPIError::timeout()),
            NetworkResult::ServiceUnavailable(m) => Err(VeilidAPIError::invalid_target(m)),
            NetworkResult::NoConnection(m) => Err(VeilidAPIError::no_connection(m.to_string())),
            NetworkResult::AlreadyExists(m) => Err(VeilidAPIError::no_connection(format!(
                "Already exists: {}",
                m
            ))),
            NetworkResult::InvalidMessage(m) => {
                Err(VeilidAPIError::parse_error("Invalid message", m))
            }
            NetworkResult::Value(v) => Ok(v),
        }
    }

    /// The [tracing] log level at which this error should be reported.
    pub fn log_level(&self) -> Level {
        match self {
            VeilidAPIError::NotInitialized
            | VeilidAPIError::AlreadyInitialized
            | VeilidAPIError::InvalidTarget { message: _ }
            | VeilidAPIError::Internal { message: _ }
            | VeilidAPIError::Generic { message: _ }
            | VeilidAPIError::ParseError {
                message: _,
                value: _,
            }
            | VeilidAPIError::InvalidArgument {
                context: _,
                argument: _,
                value: _,
            }
            | VeilidAPIError::MissingArgument {
                context: _,
                argument: _,
            }
            | VeilidAPIError::Shutdown => Level::ERROR,

            VeilidAPIError::NoConnection { message: _ }
            | VeilidAPIError::KeyNotFound { key: _ }
            | VeilidAPIError::Unimplemented { message: _ } => Level::WARN,

            VeilidAPIError::Timeout
            | VeilidAPIError::TryAgain { message: _ }
            | VeilidAPIError::TransactionNotFound { message: _ } => Level::DEBUG,
        }
    }

    /// Construct a [VeilidAPIError::Internal] error with the given message. Constructing one signals a bug in Veilid.
    pub fn internal<T: ToString>(msg: T) -> Self {
        let message = msg.to_string();
        // Constructing an internal error should get logged because it must be a programming error on our part
        // veilid_log!(acc error "Internal error: {}", &message);
        Self::Internal { message }
    }
    /// Construct a [VeilidAPIError::Unimplemented] error with the given message.
    pub fn unimplemented<T: ToString>(msg: T) -> Self {
        let message = msg.to_string();
        // Constructing an unimplemented error should get logged because it must be a programming error on our part
        // veilid_log!(acc error "Unimplemented: {}", &message);
        Self::Unimplemented { message }
    }
}

/////////////////////////////////////////////////////////////////////////////////////////

/// Trait for types that can be directly converted into parameters for VeilidAPIError
pub trait ToVeilidAPIErrorArgument {
    /// Render this value as a string for inclusion in a [VeilidAPIError] message field.
    fn to_veilid_api_error_argument(&self) -> String;
}

impl ToVeilidAPIErrorArgument for String {
    fn to_veilid_api_error_argument(&self) -> String {
        self.clone()
    }
}
impl ToVeilidAPIErrorArgument for str {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for [u8] {
    fn to_veilid_api_error_argument(&self) -> String {
        hex::encode(self)
    }
}
impl ToVeilidAPIErrorArgument for Vec<u8> {
    fn to_veilid_api_error_argument(&self) -> String {
        hex::encode(self)
    }
}

impl ToVeilidAPIErrorArgument for usize {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for u64 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for u32 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for u16 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for u8 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for isize {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for i64 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for i32 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for i16 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for i8 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for f64 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for f32 {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}

impl<T: ToVeilidAPIErrorArgument> ToVeilidAPIErrorArgument for core::ops::Range<T> {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}..{}",
            self.start.to_veilid_api_error_argument(),
            self.end.to_veilid_api_error_argument()
        )
    }
}

impl ToVeilidAPIErrorArgument for Timestamp {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for TimestampDuration {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for ValueSeqNum {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for ValueSubkeyRangeSet {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for CryptoKind {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for VeilidCapability {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}

impl ToVeilidAPIErrorArgument for RouteId {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for NodeId {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for PublicKey {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for SecretKey {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}:{}",
            self.kind(),
            self.ref_value().to_veilid_api_error_argument()
        )
    }
}
impl ToVeilidAPIErrorArgument for SharedSecret {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}:{}",
            self.kind(),
            self.ref_value().to_veilid_api_error_argument()
        )
    }
}
impl ToVeilidAPIErrorArgument for Signature {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}:{}",
            self.kind(),
            self.ref_value().to_veilid_api_error_argument()
        )
    }
}
impl ToVeilidAPIErrorArgument for HashDigest {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}:{}",
            self.kind(),
            self.ref_value().to_veilid_api_error_argument()
        )
    }
}
impl ToVeilidAPIErrorArgument for KeyPair {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}:{}",
            self.kind(),
            self.ref_value().to_veilid_api_error_argument(),
        )
    }
}
impl ToVeilidAPIErrorArgument for RecordKey {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}:{}",
            self.kind(),
            self.value().to_veilid_api_error_argument()
        )
    }
}
impl ToVeilidAPIErrorArgument for MemberId {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for OpaqueRecordKey {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}:{}",
            self.kind(),
            self.ref_value().to_veilid_api_error_argument()
        )
    }
}
impl ToVeilidAPIErrorArgument for BareRecordKey {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}{}",
            self.ref_key(),
            self.ref_encryption_key()
                .map(|ek| ek.to_veilid_api_error_argument())
                .unwrap_or("".to_string())
        )
    }
}
impl ToVeilidAPIErrorArgument for BareKeyPair {
    fn to_veilid_api_error_argument(&self) -> String {
        format!(
            "{}:{}",
            self.ref_key().to_veilid_api_error_argument(),
            self.ref_secret().to_veilid_api_error_argument()
        )
    }
}
impl ToVeilidAPIErrorArgument for BarePublicKey {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for BareSecretKey {
    fn to_veilid_api_error_argument(&self) -> String {
        "*".repeat(self.to_string().len())
    }
}
impl ToVeilidAPIErrorArgument for BareSharedSecret {
    fn to_veilid_api_error_argument(&self) -> String {
        "*".repeat(self.to_string().len())
    }
}
impl ToVeilidAPIErrorArgument for BareSignature {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for BareHashDigest {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for BareOpaqueRecordKey {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}
impl ToVeilidAPIErrorArgument for BareMemberId {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}

impl ToVeilidAPIErrorArgument for serde_json::Error {
    fn to_veilid_api_error_argument(&self) -> String {
        self.to_string()
    }
}

impl<T: ToVeilidAPIErrorArgument + ?Sized> ToVeilidAPIErrorArgument for &T {
    fn to_veilid_api_error_argument(&self) -> String {
        (**self).to_veilid_api_error_argument()
    }
}

/////////////////////////////////////////////////////////////////////////////////////////

/// Result type for public Veilid API errors
pub type VeilidAPIResult<T> = Result<T, VeilidAPIError>;

/// Extension methods for turning recoverable [VeilidAPIError] outcomes into `Ok(None)`.
pub trait OkVeilidAPIResult<T> {
    /// Map a [VeilidAPIError::TryAgain] error to `Ok(None)`, passing through other results unchanged.
    fn ok_try_again(self) -> VeilidAPIResult<Option<T>>;
    /// Map a [VeilidAPIError::TryAgain] or [VeilidAPIError::Timeout] error to `Ok(None)`, passing through other results unchanged.
    fn ok_try_again_timeout(self) -> VeilidAPIResult<Option<T>>;
}

impl<T> OkVeilidAPIResult<T> for VeilidAPIResult<Option<T>> {
    fn ok_try_again(self) -> VeilidAPIResult<Option<T>> {
        match self {
            Ok(v) => Ok(v),
            Err(VeilidAPIError::TryAgain { message: _ }) => Ok(None),
            Err(e) => Err(e),
        }
    }
    fn ok_try_again_timeout(self) -> VeilidAPIResult<Option<T>> {
        match self {
            Ok(v) => Ok(v),
            Err(VeilidAPIError::TryAgain { message: _ }) => Ok(None),
            Err(VeilidAPIError::Timeout) => Ok(None),
            Err(e) => Err(e),
        }
    }
}

impl From<std::io::Error> for VeilidAPIError {
    fn from(e: std::io::Error) -> Self {
        match e.kind() {
            std::io::ErrorKind::TimedOut => VeilidAPIError::timeout(),
            std::io::ErrorKind::ConnectionRefused => VeilidAPIError::no_connection(e.to_string()),
            std::io::ErrorKind::ConnectionReset => VeilidAPIError::no_connection(e.to_string()),
            // #[cfg(feature = "io_error_more")]
            // std::io::ErrorKind::HostUnreachable => VeilidAPIError::no_connection(e.to_string()),
            // #[cfg(feature = "io_error_more")]
            // std::io::ErrorKind::NetworkUnreachable => VeilidAPIError::no_connection(e.to_string()),
            std::io::ErrorKind::ConnectionAborted => VeilidAPIError::no_connection(e.to_string()),
            std::io::ErrorKind::NotConnected => VeilidAPIError::no_connection(e.to_string()),
            std::io::ErrorKind::AddrInUse => VeilidAPIError::no_connection(e.to_string()),
            std::io::ErrorKind::AddrNotAvailable => VeilidAPIError::no_connection(e.to_string()),
            // #[cfg(feature = "io_error_more")]
            // std::io::ErrorKind::NetworkDown => VeilidAPIError::no_connection(e.to_string()),
            // #[cfg(feature = "io_error_more")]
            // std::io::ErrorKind::ReadOnlyFilesystem => VeilidAPIError::internal(e.to_string()),
            // #[cfg(feature = "io_error_more")]
            // std::io::ErrorKind::NotSeekable => VeilidAPIError::internal(e.to_string()),
            // #[cfg(feature = "io_error_more")]
            // std::io::ErrorKind::FilesystemQuotaExceeded => VeilidAPIError::internal(e.to_string()),
            // #[cfg(feature = "io_error_more")]
            // std::io::ErrorKind::Deadlock => VeilidAPIError::internal(e.to_string()),
            std::io::ErrorKind::Unsupported => VeilidAPIError::internal(e.to_string()),
            std::io::ErrorKind::OutOfMemory => VeilidAPIError::internal(e.to_string()),
            _ => VeilidAPIError::generic(e.to_string()),
        }
    }
}