smart-config 0.4.0-pre.2

Schema-driven layered configuration system with support of multiple configuration formats
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
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
//! Param deserializers based on units of measurement.

use std::{fmt, marker::PhantomData, str::FromStr, time::Duration};

use serde::{
    Deserialize, Deserializer,
    de::{self, EnumAccess, Error as DeError, VariantAccess},
};

use crate::{
    ByteSize, EtherAmount,
    de::{CustomKnownOption, DeserializeContext, DeserializeParam, Optional, WellKnown},
    error::ErrorWithOrigin,
    metadata::{BasicTypes, ParamMetadata, SizeUnit, TimeUnit, TypeDescription, TypeSuffixes},
    utils::{Decimal, FromStrStart},
    value::Value,
};

impl TimeUnit {
    fn overflow_err(self, raw_val: Decimal) -> serde_json::Error {
        let plural = self.plural();
        DeError::custom(format!(
            "{raw_val} {plural} does not fit into `u64` when converted to milliseconds"
        ))
    }

    fn into_duration(self, raw_value: Decimal) -> Result<Duration, serde_json::Error> {
        let millis_in_unit = match self {
            Self::Millis => Decimal::from(1),
            Self::Seconds => Decimal::new(1, 3),
            Self::Minutes => Decimal::new(60, 3),
            Self::Hours => Decimal::new(3_600, 3),
            Self::Days => Decimal::new(86_400, 3),
            Self::Weeks => Decimal::new(7 * 86_400, 3),
        };
        let millis = raw_value
            .checked_mul(millis_in_unit)
            .ok_or_else(|| self.overflow_err(raw_value))?;
        let millis = millis
            .to_int()
            .ok_or_else(|| self.overflow_err(raw_value))?;

        u64::try_from(millis)
            .map(Duration::from_millis)
            .or_else(|_| {
                // Try converting to seconds to cover a wider range of values. Losing precision
                // in subsecond millis is not a concern for such large values.
                let secs =
                    u64::try_from(millis / 1_000).map_err(|_| self.overflow_err(raw_value))?;
                Ok(Duration::from_secs(secs))
            })
    }
}

/// Supports deserializing a [`Duration`] from a number, with `self` being the unit of measurement.
///
/// # Examples
///
/// ```
/// # use std::time::Duration;
/// # use smart_config::{metadata::TimeUnit, DescribeConfig, DeserializeConfig};
/// use smart_config::testing;
///
/// #[derive(DescribeConfig, DeserializeConfig)]
/// struct TestConfig {
///     #[config(with = TimeUnit::Millis)]
///     time_ms: Duration,
/// }
///
/// let source = smart_config::config!("time_ms": 100);
/// let config = testing::test::<TestConfig>(source)?;
/// assert_eq!(config.time_ms, Duration::from_millis(100));
/// # anyhow::Ok(())
/// ```
impl DeserializeParam<Duration> for TimeUnit {
    const EXPECTING: BasicTypes = BasicTypes::INTEGER;

    fn describe(&self, description: &mut TypeDescription) {
        description
            .set_details("time duration")
            .set_unit((*self).into());
    }

    fn deserialize_param(
        &self,
        ctx: DeserializeContext<'_>,
        param: &'static ParamMetadata,
    ) -> Result<Duration, ErrorWithOrigin> {
        let deserializer = ctx.current_value_deserializer(param.name)?;
        let raw_value = Decimal::deserialize(deserializer)?;
        self.into_duration(raw_value)
            .map_err(|err| deserializer.enrich_err(err))
    }

    fn serialize_param(&self, param: &Duration) -> serde_json::Value {
        match self {
            Self::Millis => serde_json::to_value(param.as_millis()).unwrap(),
            Self::Seconds => param.as_secs().into(),
            Self::Minutes => (param.as_secs() / 60).into(),
            Self::Hours => (param.as_secs() / 3_600).into(),
            Self::Days => (param.as_secs() / 86_400).into(),
            Self::Weeks => (param.as_secs() / 86_400 / 7).into(),
        }
    }
}

/// Supports deserializing a [`ByteSize`] from a number, with `self` being the unit of measurement.
///
/// # Examples
///
/// ```
/// # use std::time::Duration;
/// # use smart_config::{metadata::SizeUnit, DescribeConfig, DeserializeConfig, ByteSize};
/// use smart_config::testing;
///
/// #[derive(DescribeConfig, DeserializeConfig)]
/// struct TestConfig {
///     #[config(with = SizeUnit::MiB)]
///     size_mb: ByteSize,
/// }
///
/// let source = smart_config::config!("size_mb": 4);
/// let config = testing::test::<TestConfig>(source)?;
/// assert_eq!(config.size_mb, ByteSize(4 << 20));
/// # anyhow::Ok(())
/// ```
impl DeserializeParam<ByteSize> for SizeUnit {
    const EXPECTING: BasicTypes = BasicTypes::INTEGER;

    fn describe(&self, description: &mut TypeDescription) {
        description
            .set_details("byte size")
            .set_unit((*self).into());
    }

    fn deserialize_param(
        &self,
        ctx: DeserializeContext<'_>,
        param: &'static ParamMetadata,
    ) -> Result<ByteSize, ErrorWithOrigin> {
        let deserializer = ctx.current_value_deserializer(param.name)?;
        let raw_value = u64::deserialize(deserializer)?;
        ByteSize::checked(raw_value, *self).ok_or_else(|| {
            let err = DeError::custom(format!(
                "{raw_value} {unit} does not fit into `u64`",
                unit = self.as_str()
            ));
            deserializer.enrich_err(err)
        })
    }

    fn serialize_param(&self, param: &ByteSize) -> serde_json::Value {
        match self {
            Self::Bytes => param.0.into(),
            Self::KiB => (param.0 >> 10).into(),
            Self::MiB => (param.0 >> 20).into(),
            Self::GiB => (param.0 >> 30).into(),
        }
    }
}

/// Default deserializer for [`Duration`]s, [`ByteSize`]s and [`EtherAmount`]s.
///
/// Values can be deserialized from 2 formats:
///
/// - String consisting of a number, optional whitespace and a unit, such as "30 secs" or "500ms" (for `Duration`) /
///   "4 MiB" (for `ByteSize`). The unit must correspond to a [`TimeUnit`] / [`SizeUnit`] / [`EtherUnit`](crate::metadata::EtherUnit).
///   `Duration`s and `EtherAmount`s support decimal numbers, such as `3.5 sec` or `1.5e-5 ether`; `ByteSize`s only support integers.
/// - Object with a single key and a numeric value, such as `{ "hours": 3 }` (for `Duration`) / `{ "kb": 512 }` (for `SizeUnit`).
///   To prevent precision loss, decimal values may be enclosed in a string (e.g., `{ "ether": "0.000123456" }`).
///
/// Thanks to nesting of object params, the second approach automatically means that a duration can be parsed
/// from a param name suffixed with a unit. For example, a value `latency_ms: 500` for parameter `latency`
/// will be recognized as 500 ms.
///
/// # Examples
///
/// ```
/// # use std::time::Duration;
/// # use smart_config::{testing, ByteSize, EtherAmount, Environment, DescribeConfig, DeserializeConfig};
/// #[derive(DescribeConfig, DeserializeConfig)]
/// struct TestConfig {
///     latency: Duration,
///     disk: ByteSize,
///     #[config(default)]
///     fee: EtherAmount,
/// }
///
/// // Parsing from a string
/// let source = smart_config::config!(
///     "latency": "30 secs",
///     "disk": "256 MiB",
///     "fee": "100 gwei",
/// );
/// let config: TestConfig = testing::test(source)?;
/// assert_eq!(config.latency, Duration::from_secs(30));
/// assert_eq!(config.disk, ByteSize(256 << 20));
/// assert_eq!(config.fee, EtherAmount(100_000_000_000));
///
/// // Parsing from an object
/// let source = smart_config::config!(
///     "latency": serde_json::json!({ "hours": 3.5 }),
///     "disk": serde_json::json!({ "gigabytes": 2 }),
///     "fee": serde_json::json!({ "ether": "0.000125" }),
/// );
/// let config: TestConfig = testing::test(source)?;
/// assert_eq!(config.latency, Duration::from_secs(3_600 * 7 / 2));
/// assert_eq!(config.disk, ByteSize(2 << 30));
/// assert_eq!(config.fee, EtherAmount(125_000_000_000_000));
///
/// // Parsing from a suffixed parameter name
/// let source = Environment::from_iter("", [
///     ("LATENCY_SEC", "1.5"),
///     ("DISK_GB", "10"),
///     ("FEE_IN_ETHER", "1.5e-5"),
/// ]);
/// let config: TestConfig = testing::test(source)?;
/// assert_eq!(config.latency, Duration::from_millis(1_500));
/// assert_eq!(config.disk, ByteSize(10 << 30));
/// assert_eq!(config.fee, EtherAmount(15_000_000_000_000));
/// # anyhow::Ok(())
/// ```
#[derive(Debug, Clone, Copy)]
pub struct WithUnit;

impl WithUnit {
    const EXPECTED_TYPES: BasicTypes = BasicTypes::STRING.or(BasicTypes::OBJECT);

    fn deserialize<Raw, T>(
        ctx: &DeserializeContext<'_>,
        param: &'static ParamMetadata,
    ) -> Result<T, ErrorWithOrigin>
    where
        Raw: EnumWithUnit + TryInto<T, Error = serde_json::Error>,
    {
        let deserializer = ctx.current_value_deserializer(param.name)?;
        let raw = if let Value::String(s) = deserializer.value() {
            s.expose()
                .parse::<Raw>()
                .map_err(|err| deserializer.enrich_err(err))?
        } else {
            deserializer.deserialize_enum("Raw", Raw::VARIANTS, EnumVisitor(PhantomData::<Raw>))?
        };
        raw.try_into().map_err(|err| deserializer.enrich_err(err))
    }

    // We need special handling for `{ "suffix": null }` values (incl. ones produced by suffixed param names like `param_ms: null`).
    // Without it, we'd error when parsing `null` value as `u64`.
    fn deserialize_opt<Raw, T>(
        ctx: &DeserializeContext<'_>,
        param: &'static ParamMetadata,
    ) -> Result<Option<T>, ErrorWithOrigin>
    where
        Raw: EnumWithUnit + TryInto<T, Error = serde_json::Error>,
    {
        let deserializer = ctx.current_value_deserializer(param.name)?;
        let raw = if let Value::String(s) = deserializer.value() {
            Some(
                s.expose()
                    .parse::<Raw>()
                    .map_err(|err| deserializer.enrich_err(err))?,
            )
        } else {
            deserializer.deserialize_enum(
                "Raw",
                Raw::VARIANTS,
                EnumVisitor(PhantomData::<Option<Raw>>),
            )?
        };
        let Some(raw) = raw else {
            return Ok(None);
        };
        raw.try_into()
            .map(Some)
            .map_err(|err| deserializer.enrich_err(err))
    }
}

/// Helper trait allowing to unify enum parsing for durations and byte sizes.
trait EnumWithUnit: FromStr<Err = serde_json::Error> {
    type Value: FromStrStart + de::DeserializeOwned;

    const EXPECTING: &'static str;
    const VARIANTS: &'static [&'static str];

    fn extract_variant(unit: &str) -> Option<fn(Self::Value) -> Self>;

    fn parse<E: de::Error>(unit: &str, value: Self::Value) -> Result<Self, E> {
        let variant_mapper = Self::extract_variant(unit)
            .ok_or_else(|| DeError::unknown_variant(unit, Self::VARIANTS))?;
        Ok(variant_mapper(value))
    }

    fn parse_opt<E: de::Error>(unit: &str, value: Option<Self::Value>) -> Result<Option<Self>, E> {
        let variant_mapper = Self::extract_variant(unit)
            .ok_or_else(|| DeError::unknown_variant(unit, Self::VARIANTS))?;
        // We want to check the variant first, and only then return `Ok(None)`.
        Ok(value.map(variant_mapper))
    }

    fn from_unit_str(s: &str, lowercase_unit: bool) -> Result<Self, serde_json::Error> {
        let (value, rem) = <Self::Value as FromStrStart>::from_str_start(s)?;
        let value =
            value.ok_or_else(|| DeError::invalid_type(de::Unexpected::Str(s), &Self::EXPECTING))?;

        let mut unit = rem.trim();
        if unit.is_empty() {
            return Err(DeError::invalid_type(
                de::Unexpected::Str(s),
                &Self::EXPECTING,
            ));
        }

        let lowercase_unit_string;
        if lowercase_unit {
            lowercase_unit_string = unit.to_lowercase();
            unit = &lowercase_unit_string;
        }
        Self::parse(unit, value)
    }
}

#[derive(Debug)]
struct EnumVisitor<T>(PhantomData<T>);

impl<'v, T> de::Visitor<'v> for EnumVisitor<T>
where
    T: EnumWithUnit<Value: de::DeserializeOwned>,
{
    type Value = T;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "enum with one of {:?} variants", T::VARIANTS)
    }

    fn visit_enum<A: EnumAccess<'v>>(self, data: A) -> Result<Self::Value, A::Error> {
        let (tag, payload) = data.variant::<String>()?;
        let value = payload.newtype_variant()?;
        let unit = tag.strip_prefix("in_").unwrap_or(&tag);
        T::parse(unit, value)
    }
}

impl<'v, T: EnumWithUnit> de::Visitor<'v> for EnumVisitor<Option<T>> {
    type Value = Option<T>;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "enum with one of {:?} variants", T::VARIANTS)
    }

    fn visit_enum<A: EnumAccess<'v>>(self, data: A) -> Result<Self::Value, A::Error> {
        let (tag, payload) = data.variant::<String>()?;
        let value = payload.newtype_variant()?;
        let unit = tag.strip_prefix("in_").unwrap_or(&tag);
        T::parse_opt(unit, value)
    }
}

/// Raw `Duration` representation used by the `WithUnit` deserializer.
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
enum RawDuration {
    Millis(Decimal),
    Seconds(Decimal),
    Minutes(Decimal),
    Hours(Decimal),
    Days(Decimal),
    Weeks(Decimal),
}

macro_rules! impl_enum_with_unit {
    ($($($name:tt)|+ => $func:expr,)+) => {
        const VARIANTS: &'static [&'static str] = &[$($($name,)+)+];

        fn extract_variant(unit: &str) -> Option<fn(Self::Value) -> Self> {
            Some(match unit {
                $($($name )|+ => $func,)+
                _ => return None,
            })
        }
    };
}

impl EnumWithUnit for RawDuration {
    type Value = Decimal;

    const EXPECTING: &'static str = "value with unit, like '10 ms'";

    impl_enum_with_unit!(
        "milliseconds" | "millis" | "ms" => Self::Millis,
        "seconds" | "second" | "secs" | "sec" | "s" => Self::Seconds,
        "minutes" | "minute" | "mins" | "min" | "m" => Self::Minutes,
        "hours" | "hour" | "hr" | "h" => Self::Hours,
        "days" | "day" | "d" => Self::Days,
        "weeks" | "week" | "w" => Self::Weeks,
    );
}

impl FromStr for RawDuration {
    type Err = serde_json::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_unit_str(s, false)
    }
}

impl TryFrom<RawDuration> for Duration {
    type Error = serde_json::Error;

    fn try_from(value: RawDuration) -> Result<Self, Self::Error> {
        let (unit, raw_value) = match value {
            RawDuration::Millis(val) => (TimeUnit::Millis, val),
            RawDuration::Seconds(val) => (TimeUnit::Seconds, val),
            RawDuration::Minutes(val) => (TimeUnit::Minutes, val),
            RawDuration::Hours(val) => (TimeUnit::Hours, val),
            RawDuration::Days(val) => (TimeUnit::Days, val),
            RawDuration::Weeks(val) => (TimeUnit::Weeks, val),
        };
        unit.into_duration(raw_value)
    }
}

impl DeserializeParam<Duration> for WithUnit {
    const EXPECTING: BasicTypes = Self::EXPECTED_TYPES;

    fn describe(&self, description: &mut TypeDescription) {
        description.set_details("duration with unit, or object with single unit key");
        description.set_suffixes(TypeSuffixes::DurationUnits);
    }

    fn deserialize_param(
        &self,
        ctx: DeserializeContext<'_>,
        param: &'static ParamMetadata,
    ) -> Result<Duration, ErrorWithOrigin> {
        Self::deserialize::<RawDuration, _>(&ctx, param)
    }

    fn serialize_param(&self, param: &Duration) -> serde_json::Value {
        if param.is_zero() {
            // Special case to produce a more "expected" string.
            return "0s".into();
        }

        let duration_string = if param.subsec_millis() != 0 {
            format!("{}ms", param.as_millis())
        } else {
            let seconds = param.as_secs();
            if !seconds.is_multiple_of(60) {
                format!("{seconds}s")
            } else if !seconds.is_multiple_of(3_600) {
                format!("{}min", seconds / 60)
            } else if !seconds.is_multiple_of(86_400) {
                format!("{}h", seconds / 3_600)
            } else if !seconds.is_multiple_of(86_400 * 7) {
                format!("{}d", seconds / 86_400)
            } else {
                format!("{}w", seconds / (86_400 * 7))
            }
        };
        duration_string.into()
    }
}

macro_rules! impl_deserialize_opt_param {
    ($ty:ty => $raw:ty) => {
        impl DeserializeParam<Option<$ty>> for WithUnit {
            const EXPECTING: BasicTypes = Self::EXPECTED_TYPES;

            fn describe(&self, description: &mut TypeDescription) {
                <Self as DeserializeParam<$ty>>::describe(self, description);
            }

            fn deserialize_param(
                &self,
                ctx: DeserializeContext<'_>,
                param: &'static ParamMetadata,
            ) -> Result<Option<$ty>, ErrorWithOrigin> {
                Self::deserialize_opt::<$raw, _>(&ctx, param)
            }

            fn serialize_param(&self, param: &Option<$ty>) -> serde_json::Value {
                match param {
                    Some(val) => self.serialize_param(val),
                    None => serde_json::Value::Null,
                }
            }
        }
    };
}

impl_deserialize_opt_param!(Duration => RawDuration);

macro_rules! impl_well_known_with_unit {
    ($ty:ty) => {
        impl WellKnown for $ty {
            type Deserializer = WithUnit;
            const DE: Self::Deserializer = WithUnit;
        }

        impl CustomKnownOption for $ty {
            type OptDeserializer = Optional<WithUnit, true>;
            const OPT_DE: Self::OptDeserializer = Optional(WithUnit);
        }
    };
}

impl_well_known_with_unit!(Duration);

#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
enum RawByteSize {
    Bytes(u64),
    Kilobytes(u64),
    Megabytes(u64),
    Gigabytes(u64),
}

impl EnumWithUnit for RawByteSize {
    type Value = u64;

    const EXPECTING: &'static str = "value with unit, like '32 MB'";

    impl_enum_with_unit!(
        "bytes" | "b" => Self::Bytes,
        "kilobytes" | "kb" | "kib" => Self::Kilobytes,
        "megabytes" | "mb" | "mib" => Self::Megabytes,
        "gigabytes" | "gb" | "gib" => Self::Gigabytes,
    );
}

impl TryFrom<RawByteSize> for ByteSize {
    type Error = serde_json::Error;

    fn try_from(value: RawByteSize) -> Result<Self, Self::Error> {
        let (unit, raw_value) = match value {
            RawByteSize::Bytes(val) => (SizeUnit::Bytes, val),
            RawByteSize::Kilobytes(val) => (SizeUnit::KiB, val),
            RawByteSize::Megabytes(val) => (SizeUnit::MiB, val),
            RawByteSize::Gigabytes(val) => (SizeUnit::GiB, val),
        };
        ByteSize::checked(raw_value, unit).ok_or_else(|| {
            DeError::custom(format!(
                "{raw_value} {unit} does not fit into `u64`",
                unit = unit.as_str()
            ))
        })
    }
}

// Inapplicable to `Duration` because it's not a local type.
macro_rules! impl_deserialize_param {
    ($ty:ty, raw: $raw:ty, name: $name:tt, units: $units:ident) => {
        impl<'de> Deserialize<'de> for $raw {
            fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                deserializer.deserialize_enum(
                    stringify!($raw),
                    Self::VARIANTS,
                    EnumVisitor(PhantomData::<Self>),
                )
            }
        }

        impl FromStr for $raw {
            type Err = serde_json::Error;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Self::from_unit_str(s, true)
            }
        }

        impl FromStr for $ty {
            type Err = serde_json::Error;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                <$raw>::from_unit_str(s, true)?.try_into()
            }
        }

        impl DeserializeParam<$ty> for WithUnit {
            const EXPECTING: BasicTypes = Self::EXPECTED_TYPES;

            fn describe(&self, description: &mut TypeDescription) {
                description
                    .set_details(concat!($name, " with unit, or object with single unit key"));
                description.set_suffixes(TypeSuffixes::$units);
            }

            fn deserialize_param(
                &self,
                ctx: DeserializeContext<'_>,
                param: &'static ParamMetadata,
            ) -> Result<$ty, ErrorWithOrigin> {
                Self::deserialize::<$raw, _>(&ctx, param)
            }

            fn serialize_param(&self, param: &$ty) -> serde_json::Value {
                param.to_string().into()
            }
        }
    };
}

impl_deserialize_param!(ByteSize, raw: RawByteSize, name: "size", units: SizeUnits);
impl_deserialize_opt_param!(ByteSize => RawByteSize);
impl_well_known_with_unit!(ByteSize);

impl TypeSuffixes {
    pub(crate) fn contains(self, suffix: &str) -> bool {
        match self {
            Self::All => true,
            Self::DurationUnits => {
                let suffix = suffix.strip_prefix("in_").unwrap_or(suffix);
                RawDuration::VARIANTS.contains(&suffix)
            }
            Self::SizeUnits => {
                let suffix = suffix.strip_prefix("in_").unwrap_or(suffix);
                RawByteSize::VARIANTS.contains(&suffix)
            }
            Self::EtherUnits => {
                let suffix = suffix.strip_prefix("in_").unwrap_or(suffix);
                RawEtherAmount::VARIANTS.contains(&suffix)
            }
        }
    }
}

#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
enum RawEtherAmount {
    Wei(Decimal),
    Gwei(Decimal),
    Ether(Decimal),
}

impl EnumWithUnit for RawEtherAmount {
    type Value = Decimal;

    const EXPECTING: &'static str = "value with unit, like '100 gwei'";

    impl_enum_with_unit!(
        "wei" => Self::Wei,
        "gwei" => Self::Gwei,
        "ether" => Self::Ether,
    );
}

impl TryFrom<RawEtherAmount> for EtherAmount {
    type Error = serde_json::Error;

    fn try_from(value: RawEtherAmount) -> Result<Self, Self::Error> {
        let (scale, raw_value) = match value {
            RawEtherAmount::Wei(val) => (0, val),
            RawEtherAmount::Gwei(val) => (9, val),
            RawEtherAmount::Ether(val) => (18, val),
        };
        let value = raw_value.scale(scale)?;
        Ok(Self(value))
    }
}

impl_deserialize_param!(EtherAmount, raw: RawEtherAmount, name: "amount", units: EtherUnits);
impl_deserialize_opt_param!(EtherAmount => RawEtherAmount);
impl_well_known_with_unit!(EtherAmount);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parsing_time_string() {
        let duration: RawDuration = "10ms".parse().unwrap();
        assert_eq!(duration, RawDuration::Millis(10.into()));
        let duration: RawDuration = "50    seconds".parse().unwrap();
        assert_eq!(duration, RawDuration::Seconds(50.into()));
        let duration: RawDuration = "40s".parse().unwrap();
        assert_eq!(duration, RawDuration::Seconds(40.into()));
        let duration: RawDuration = "10 min".parse().unwrap();
        assert_eq!(duration, RawDuration::Minutes(10.into()));
        let duration: RawDuration = "10m".parse().unwrap();
        assert_eq!(duration, RawDuration::Minutes(10.into()));
        let duration: RawDuration = "12 hours".parse().unwrap();
        assert_eq!(duration, RawDuration::Hours(12.into()));
        let duration: RawDuration = "12h".parse().unwrap();
        assert_eq!(duration, RawDuration::Hours(12.into()));
        let duration: RawDuration = "30d".parse().unwrap();
        assert_eq!(duration, RawDuration::Days(30.into()));
        let duration: RawDuration = "1 day".parse().unwrap();
        assert_eq!(duration, RawDuration::Days(1.into()));
        let duration: RawDuration = "2 weeks".parse().unwrap();
        assert_eq!(duration, RawDuration::Weeks(2.into()));
        let duration: RawDuration = "3w".parse().unwrap();
        assert_eq!(duration, RawDuration::Weeks(3.into()));
    }

    #[test]
    fn parsing_fractional_time_string() {
        let duration: RawDuration = "10.0ms".parse().unwrap();
        assert_eq!(duration, RawDuration::Millis(10.into()));
        let duration: RawDuration = "0.2s".parse().unwrap();
        assert_eq!(duration, RawDuration::Seconds(Decimal::new(2, -1)));
        let duration: RawDuration = "0.33 days".parse().unwrap();
        assert_eq!(duration, RawDuration::Days(Decimal::new(33, -2)));
        let duration: RawDuration = "1.7e+3 hours".parse().unwrap();
        assert_eq!(duration, RawDuration::Hours(Decimal::new(17, 2)));
    }

    #[test]
    fn parsing_time_string_errors() {
        let err = "".parse::<RawDuration>().unwrap_err().to_string();
        assert!(err.starts_with("invalid type"), "{err}");
        let err = "???".parse::<RawDuration>().unwrap_err().to_string();
        assert!(err.starts_with("invalid type"), "{err}");
        let err = "10".parse::<RawDuration>().unwrap_err().to_string();
        assert!(err.starts_with("invalid type"), "{err}");
        let err = "hours".parse::<RawDuration>().unwrap_err().to_string();
        assert!(err.starts_with("invalid type"), "{err}");

        let err = "111111111111111111111111111111111111111111s"
            .parse::<RawDuration>()
            .unwrap_err()
            .to_string();
        assert!(err.contains("too many digits"), "{err}");

        let err = "10 months".parse::<RawDuration>().unwrap_err().to_string();
        assert!(err.starts_with("unknown variant"), "{err}");
    }

    #[test]
    fn parsing_byte_size_string() {
        let size: RawByteSize = "16bytes".parse().unwrap();
        assert_eq!(size, RawByteSize::Bytes(16));
        let size: RawByteSize = "128    KiB".parse().unwrap();
        assert_eq!(size, RawByteSize::Kilobytes(128));
        let size: RawByteSize = "16 kb".parse().unwrap();
        assert_eq!(size, RawByteSize::Kilobytes(16));
        let size: RawByteSize = "4MB".parse().unwrap();
        assert_eq!(size, RawByteSize::Megabytes(4));
        let size: RawByteSize = "1 GB".parse().unwrap();
        assert_eq!(size, RawByteSize::Gigabytes(1));
    }

    #[test]
    fn parsing_ether_amount_string() {
        let amount: RawEtherAmount = "1wei".parse().unwrap();
        assert_eq!(amount, RawEtherAmount::Wei(1.into()));
        let amount: EtherAmount = amount.try_into().unwrap();
        assert_eq!(amount, EtherAmount(1));

        let amount: RawEtherAmount = "123 wei".parse().unwrap();
        assert_eq!(amount, RawEtherAmount::Wei(123.into()));
        let amount: EtherAmount = amount.try_into().unwrap();
        assert_eq!(amount, EtherAmount(123));

        let amount: RawEtherAmount = "1.5 gwei".parse().unwrap();
        assert_eq!(amount, RawEtherAmount::Gwei(Decimal::new(15, -1)));
        let amount: EtherAmount = amount.try_into().unwrap();
        assert_eq!(amount, EtherAmount(1_500_000_000));

        for input in [
            "0.0015 ether",
            "0.001_5ether",
            "0.0015ether",
            "1.5e-3 ether",
            "15e-4ether",
            ".015e-1 ether",
        ] {
            let amount: RawEtherAmount = input.parse().unwrap();
            assert_eq!(amount, RawEtherAmount::Ether(Decimal::new(15, -4)));
            let amount: EtherAmount = amount.try_into().unwrap();
            assert_eq!(amount, EtherAmount(1_500_000_000_000_000));
        }
    }

    #[test]
    fn serializing_with_time_unit() {
        let val = TimeUnit::Millis.serialize_param(&Duration::from_millis(10));
        assert_eq!(val, 10_u32);
        let val = TimeUnit::Millis.serialize_param(&Duration::from_secs(10));
        assert_eq!(val, 10_000_u32);
        let val = TimeUnit::Seconds.serialize_param(&Duration::from_secs(10));
        assert_eq!(val, 10_u32);
        let val = TimeUnit::Minutes.serialize_param(&Duration::from_secs(10));
        assert_eq!(val, 0_u32);
        let val = TimeUnit::Minutes.serialize_param(&Duration::from_secs(120));
        assert_eq!(val, 2_u32);
    }

    #[test]
    fn serializing_with_size_unit() {
        let val = SizeUnit::Bytes.serialize_param(&ByteSize(128));
        assert_eq!(val, 128_u32);
        let val = SizeUnit::Bytes.serialize_param(&ByteSize(1 << 16));
        assert_eq!(val, 1_u32 << 16);
        let val = SizeUnit::KiB.serialize_param(&ByteSize(1 << 16));
        assert_eq!(val, 1_u32 << 6);
        let val = SizeUnit::MiB.serialize_param(&ByteSize(1 << 16));
        assert_eq!(val, 0_u32);
        let val = SizeUnit::MiB.serialize_param(&ByteSize::new(3, SizeUnit::MiB));
        assert_eq!(val, 3_u32);
    }

    #[test]
    fn serializing_with_duration() {
        let val = WithUnit.serialize_param(&Duration::ZERO);
        assert_eq!(val, "0s");
        let val = WithUnit.serialize_param(&Duration::from_millis(10));
        assert_eq!(val, "10ms");
        let val = WithUnit.serialize_param(&Duration::from_secs(5));
        assert_eq!(val, "5s");
        let val = WithUnit.serialize_param(&Duration::from_millis(5_050));
        assert_eq!(val, "5050ms");
        let val = WithUnit.serialize_param(&Duration::from_secs(300));
        assert_eq!(val, "5min");
        let val = WithUnit.serialize_param(&Duration::from_secs(7_200));
        assert_eq!(val, "2h");
        let val = WithUnit.serialize_param(&Duration::from_secs(86_400));
        assert_eq!(val, "1d");
    }

    #[test]
    fn serializing_with_byte_size() {
        let val = WithUnit.serialize_param(&ByteSize(0));
        assert_eq!(val, "0 B");
        let val = WithUnit.serialize_param(&ByteSize(128));
        assert_eq!(val, "128 B");
        let val = WithUnit.serialize_param(&ByteSize(32 << 10));
        assert_eq!(val, "32 KiB");
        let val = WithUnit.serialize_param(&ByteSize(3 << 20));
        assert_eq!(val, "3 MiB");
    }
}