1use super::DisplayWithUnit;
2
3#[must_use]
19#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
20pub struct ElectricCharge(pub(crate) f64);
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum ElectricChargeUnit {
25 Coulomb,
27 MilliCoulomb,
29 MicroCoulomb,
31 AmpereHour,
33 MilliAmpereHour,
35}
36
37impl ElectricChargeUnit {
38 const fn symbol(self) -> &'static str {
39 match self {
40 Self::Coulomb => "C",
41 Self::MilliCoulomb => "mC",
42 Self::MicroCoulomb => "uC",
43 Self::AmpereHour => "Ah",
44 Self::MilliAmpereHour => "mAh",
45 }
46 }
47
48 const fn coulombs_per_unit(self) -> f64 {
49 match self {
50 Self::Coulomb => 1.0,
51 Self::MilliCoulomb => 1e-3,
52 Self::MicroCoulomb => 1e-6,
53 Self::AmpereHour => 3_600.0,
54 Self::MilliAmpereHour => 3.6,
55 }
56 }
57}
58
59impl ElectricCharge {
60 pub const fn from_c(val: f64) -> Self {
62 Self(val)
63 }
64
65 pub const fn from_mc(val: f64) -> Self {
67 Self(val * 1e-3)
68 }
69
70 pub const fn from_uc(val: f64) -> Self {
72 Self(val * 1e-6)
73 }
74
75 pub const fn from_ah(val: f64) -> Self {
77 Self(val * 3_600.0)
78 }
79
80 pub const fn from_mah(val: f64) -> Self {
82 Self(val * 3.6)
83 }
84
85 pub const fn in_c(self) -> f64 {
87 self.0
88 }
89
90 pub const fn in_mc(self) -> f64 {
92 self.0 / 1e-3
93 }
94
95 pub const fn in_uc(self) -> f64 {
97 self.0 / 1e-6
98 }
99
100 pub const fn in_ah(self) -> f64 {
102 self.0 / 3_600.0
103 }
104
105 pub const fn in_mah(self) -> f64 {
107 self.0 / 3.6
108 }
109
110 pub fn in_unit(self, unit: ElectricChargeUnit) -> f64 {
112 self.0 / unit.coulombs_per_unit()
113 }
114
115 pub fn display_as(self, unit: ElectricChargeUnit) -> DisplayWithUnit {
117 DisplayWithUnit {
118 value: self.in_unit(unit),
119 symbol: unit.symbol(),
120 }
121 }
122
123 pub fn abs(self) -> Self {
125 Self(self.0.abs())
126 }
127}
128
129impl_quantity_display!(ElectricCharge, "C");
130
131impl_common_ops!(ElectricCharge);
132
133#[must_use]
148#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
149pub struct ElectricCurrent(pub(crate) f64);
150
151#[derive(Debug, Clone, Copy, PartialEq, Eq)]
153pub enum ElectricCurrentUnit {
154 Ampere,
156 MilliAmpere,
158 MicroAmpere,
160 KiloAmpere,
162}
163
164impl ElectricCurrentUnit {
165 const fn symbol(self) -> &'static str {
166 match self {
167 Self::Ampere => "A",
168 Self::MilliAmpere => "mA",
169 Self::MicroAmpere => "uA",
170 Self::KiloAmpere => "kA",
171 }
172 }
173
174 const fn amperes_per_unit(self) -> f64 {
175 match self {
176 Self::Ampere => 1.0,
177 Self::MilliAmpere => 1e-3,
178 Self::MicroAmpere => 1e-6,
179 Self::KiloAmpere => 1e3,
180 }
181 }
182}
183
184impl ElectricCurrent {
185 pub const fn from_a(val: f64) -> Self {
187 Self(val)
188 }
189
190 pub const fn from_ma(val: f64) -> Self {
192 Self(val * 1e-3)
193 }
194
195 pub const fn from_ua(val: f64) -> Self {
197 Self(val * 1e-6)
198 }
199
200 pub const fn from_ka(val: f64) -> Self {
202 Self(val * 1e3)
203 }
204
205 pub const fn in_a(self) -> f64 {
207 self.0
208 }
209
210 pub const fn in_ma(self) -> f64 {
212 self.0 / 1e-3
213 }
214
215 pub const fn in_ua(self) -> f64 {
217 self.0 / 1e-6
218 }
219
220 pub const fn in_ka(self) -> f64 {
222 self.0 / 1e3
223 }
224
225 pub fn in_unit(self, unit: ElectricCurrentUnit) -> f64 {
227 self.0 / unit.amperes_per_unit()
228 }
229
230 pub fn display_as(self, unit: ElectricCurrentUnit) -> DisplayWithUnit {
232 DisplayWithUnit {
233 value: self.in_unit(unit),
234 symbol: unit.symbol(),
235 }
236 }
237
238 pub fn abs(self) -> Self {
240 Self(self.0.abs())
241 }
242}
243
244impl_quantity_display!(ElectricCurrent, "A");
245
246impl_common_ops!(ElectricCurrent);
247
248#[must_use]
264#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
265pub struct Voltage(pub(crate) f64);
266
267#[derive(Debug, Clone, Copy, PartialEq, Eq)]
269pub enum VoltageUnit {
270 Volt,
272 MilliVolt,
274 KiloVolt,
276 MegaVolt,
278}
279
280impl VoltageUnit {
281 const fn symbol(self) -> &'static str {
282 match self {
283 Self::Volt => "V",
284 Self::MilliVolt => "mV",
285 Self::KiloVolt => "kV",
286 Self::MegaVolt => "MV",
287 }
288 }
289
290 const fn volts_per_unit(self) -> f64 {
291 match self {
292 Self::Volt => 1.0,
293 Self::MilliVolt => 1e-3,
294 Self::KiloVolt => 1e3,
295 Self::MegaVolt => 1e6,
296 }
297 }
298}
299
300impl Voltage {
301 pub const fn from_v(val: f64) -> Self {
303 Self(val)
304 }
305
306 pub const fn from_mv(val: f64) -> Self {
308 Self(val * 1e-3)
309 }
310
311 pub const fn from_kv(val: f64) -> Self {
313 Self(val * 1e3)
314 }
315
316 pub const fn from_megav(val: f64) -> Self {
318 Self(val * 1e6)
319 }
320
321 pub const fn in_v(self) -> f64 {
323 self.0
324 }
325
326 pub const fn in_mv(self) -> f64 {
328 self.0 / 1e-3
329 }
330
331 pub const fn in_kv(self) -> f64 {
333 self.0 / 1e3
334 }
335
336 pub const fn in_megav(self) -> f64 {
338 self.0 / 1e6
339 }
340
341 pub fn in_unit(self, unit: VoltageUnit) -> f64 {
343 self.0 / unit.volts_per_unit()
344 }
345
346 pub fn display_as(self, unit: VoltageUnit) -> DisplayWithUnit {
348 DisplayWithUnit {
349 value: self.in_unit(unit),
350 symbol: unit.symbol(),
351 }
352 }
353
354 pub fn abs(self) -> Self {
356 Self(self.0.abs())
357 }
358}
359
360impl_quantity_display!(Voltage, "V");
361
362impl_common_ops!(Voltage);
363
364#[must_use]
379#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
380pub struct Resistance(pub(crate) f64);
381
382#[derive(Debug, Clone, Copy, PartialEq, Eq)]
384pub enum ResistanceUnit {
385 Ohm,
387 MilliOhm,
389 KiloOhm,
391 MegaOhm,
393}
394
395impl ResistanceUnit {
396 const fn symbol(self) -> &'static str {
397 match self {
398 Self::Ohm => "Ohm",
399 Self::MilliOhm => "mOhm",
400 Self::KiloOhm => "kOhm",
401 Self::MegaOhm => "MOhm",
402 }
403 }
404
405 const fn ohms_per_unit(self) -> f64 {
406 match self {
407 Self::Ohm => 1.0,
408 Self::MilliOhm => 1e-3,
409 Self::KiloOhm => 1e3,
410 Self::MegaOhm => 1e6,
411 }
412 }
413}
414
415impl Resistance {
416 pub const fn from_ohm(val: f64) -> Self {
418 Self(val)
419 }
420
421 pub const fn from_mohm(val: f64) -> Self {
423 Self(val * 1e-3)
424 }
425
426 pub const fn from_kohm(val: f64) -> Self {
428 Self(val * 1e3)
429 }
430
431 pub const fn from_megaohm(val: f64) -> Self {
433 Self(val * 1e6)
434 }
435
436 pub const fn in_ohm(self) -> f64 {
438 self.0
439 }
440
441 pub const fn in_mohm(self) -> f64 {
443 self.0 / 1e-3
444 }
445
446 pub const fn in_kohm(self) -> f64 {
448 self.0 / 1e3
449 }
450
451 pub const fn in_megaohm(self) -> f64 {
453 self.0 / 1e6
454 }
455
456 pub fn in_unit(self, unit: ResistanceUnit) -> f64 {
458 self.0 / unit.ohms_per_unit()
459 }
460
461 pub fn display_as(self, unit: ResistanceUnit) -> DisplayWithUnit {
463 DisplayWithUnit {
464 value: self.in_unit(unit),
465 symbol: unit.symbol(),
466 }
467 }
468
469 pub fn abs(self) -> Self {
471 Self(self.0.abs())
472 }
473}
474
475impl_quantity_display!(Resistance, "Ω");
476
477impl_common_ops!(Resistance);
478
479#[must_use]
494#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
495pub struct MagneticFluxDensity(pub(crate) f64);
496
497#[derive(Debug, Clone, Copy, PartialEq, Eq)]
499pub enum MagneticFluxDensityUnit {
500 Tesla,
502 MilliTesla,
504 MicroTesla,
506 NanoTesla,
508 Gauss,
510}
511
512impl MagneticFluxDensityUnit {
513 const fn symbol(self) -> &'static str {
514 match self {
515 Self::Tesla => "T",
516 Self::MilliTesla => "mT",
517 Self::MicroTesla => "uT",
518 Self::NanoTesla => "nT",
519 Self::Gauss => "G",
520 }
521 }
522
523 const fn tesla_per_unit(self) -> f64 {
524 match self {
525 Self::Tesla => 1.0,
526 Self::MilliTesla => 1e-3,
527 Self::MicroTesla => 1e-6,
528 Self::NanoTesla => 1e-9,
529 Self::Gauss => 1e-4,
530 }
531 }
532}
533
534impl MagneticFluxDensity {
535 pub const fn from_t(val: f64) -> Self {
537 Self(val)
538 }
539
540 pub const fn from_mt(val: f64) -> Self {
542 Self(val * 1e-3)
543 }
544
545 pub const fn from_ut(val: f64) -> Self {
547 Self(val * 1e-6)
548 }
549
550 pub const fn from_nt(val: f64) -> Self {
552 Self(val * 1e-9)
553 }
554
555 pub const fn from_gauss(val: f64) -> Self {
557 Self(val * 1e-4)
558 }
559
560 pub const fn in_t(self) -> f64 {
562 self.0
563 }
564
565 pub const fn in_mt(self) -> f64 {
567 self.0 / 1e-3
568 }
569
570 pub const fn in_ut(self) -> f64 {
572 self.0 / 1e-6
573 }
574
575 pub const fn in_nt(self) -> f64 {
577 self.0 / 1e-9
578 }
579
580 pub const fn in_gauss(self) -> f64 {
582 self.0 / 1e-4
583 }
584
585 pub fn in_unit(self, unit: MagneticFluxDensityUnit) -> f64 {
587 self.0 / unit.tesla_per_unit()
588 }
589
590 pub fn display_as(self, unit: MagneticFluxDensityUnit) -> DisplayWithUnit {
592 DisplayWithUnit {
593 value: self.in_unit(unit),
594 symbol: unit.symbol(),
595 }
596 }
597
598 pub fn abs(self) -> Self {
600 Self(self.0.abs())
601 }
602}
603
604impl_quantity_display!(MagneticFluxDensity, "T");
605
606impl_common_ops!(MagneticFluxDensity);
607
608#[must_use]
624#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
625pub struct MagneticFlux(pub(crate) f64);
626
627#[derive(Debug, Clone, Copy, PartialEq, Eq)]
629pub enum MagneticFluxUnit {
630 Weber,
632 MilliWeber,
634 MicroWeber,
636}
637
638impl MagneticFluxUnit {
639 const fn symbol(self) -> &'static str {
640 match self {
641 Self::Weber => "Wb",
642 Self::MilliWeber => "mWb",
643 Self::MicroWeber => "uWb",
644 }
645 }
646
647 const fn webers_per_unit(self) -> f64 {
648 match self {
649 Self::Weber => 1.0,
650 Self::MilliWeber => 1e-3,
651 Self::MicroWeber => 1e-6,
652 }
653 }
654}
655
656impl MagneticFlux {
657 pub const fn from_wb(val: f64) -> Self {
659 Self(val)
660 }
661
662 pub const fn from_mwb(val: f64) -> Self {
664 Self(val * 1e-3)
665 }
666
667 pub const fn from_uwb(val: f64) -> Self {
669 Self(val * 1e-6)
670 }
671
672 pub const fn in_wb(self) -> f64 {
674 self.0
675 }
676
677 pub const fn in_mwb(self) -> f64 {
679 self.0 / 1e-3
680 }
681
682 pub const fn in_uwb(self) -> f64 {
684 self.0 / 1e-6
685 }
686
687 pub fn in_unit(self, unit: MagneticFluxUnit) -> f64 {
689 self.0 / unit.webers_per_unit()
690 }
691
692 pub fn display_as(self, unit: MagneticFluxUnit) -> DisplayWithUnit {
694 DisplayWithUnit {
695 value: self.in_unit(unit),
696 symbol: unit.symbol(),
697 }
698 }
699
700 pub fn abs(self) -> Self {
702 Self(self.0.abs())
703 }
704}
705
706impl_quantity_display!(MagneticFlux, "Wb");
707
708impl_common_ops!(MagneticFlux);
709
710#[must_use]
725#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
726pub struct Capacitance(pub(crate) f64);
727
728#[derive(Debug, Clone, Copy, PartialEq, Eq)]
730pub enum CapacitanceUnit {
731 Farad,
733 MilliFarad,
735 MicroFarad,
737 NanoFarad,
739 PicoFarad,
741}
742
743impl CapacitanceUnit {
744 const fn symbol(self) -> &'static str {
745 match self {
746 Self::Farad => "F",
747 Self::MilliFarad => "mF",
748 Self::MicroFarad => "uF",
749 Self::NanoFarad => "nF",
750 Self::PicoFarad => "pF",
751 }
752 }
753
754 const fn farads_per_unit(self) -> f64 {
755 match self {
756 Self::Farad => 1.0,
757 Self::MilliFarad => 1e-3,
758 Self::MicroFarad => 1e-6,
759 Self::NanoFarad => 1e-9,
760 Self::PicoFarad => 1e-12,
761 }
762 }
763}
764
765impl Capacitance {
766 pub const fn from_f(val: f64) -> Self {
768 Self(val)
769 }
770
771 pub const fn from_mf(val: f64) -> Self {
773 Self(val * 1e-3)
774 }
775
776 pub const fn from_uf(val: f64) -> Self {
778 Self(val * 1e-6)
779 }
780
781 pub const fn from_nf(val: f64) -> Self {
783 Self(val * 1e-9)
784 }
785
786 pub const fn from_pf(val: f64) -> Self {
788 Self(val * 1e-12)
789 }
790
791 pub const fn in_f(self) -> f64 {
793 self.0
794 }
795
796 pub const fn in_mf(self) -> f64 {
798 self.0 / 1e-3
799 }
800
801 pub const fn in_uf(self) -> f64 {
803 self.0 / 1e-6
804 }
805
806 pub const fn in_nf(self) -> f64 {
808 self.0 / 1e-9
809 }
810
811 pub const fn in_pf(self) -> f64 {
813 self.0 / 1e-12
814 }
815
816 pub fn in_unit(self, unit: CapacitanceUnit) -> f64 {
818 self.0 / unit.farads_per_unit()
819 }
820
821 pub fn display_as(self, unit: CapacitanceUnit) -> DisplayWithUnit {
823 DisplayWithUnit {
824 value: self.in_unit(unit),
825 symbol: unit.symbol(),
826 }
827 }
828
829 pub fn abs(self) -> Self {
831 Self(self.0.abs())
832 }
833}
834
835impl_quantity_display!(Capacitance, "F");
836
837impl_common_ops!(Capacitance);
838
839#[must_use]
854#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
855pub struct Inductance(pub(crate) f64);
856
857#[derive(Debug, Clone, Copy, PartialEq, Eq)]
859pub enum InductanceUnit {
860 Henry,
862 MilliHenry,
864 MicroHenry,
866 NanoHenry,
868}
869
870impl InductanceUnit {
871 const fn symbol(self) -> &'static str {
872 match self {
873 Self::Henry => "H",
874 Self::MilliHenry => "mH",
875 Self::MicroHenry => "uH",
876 Self::NanoHenry => "nH",
877 }
878 }
879
880 const fn henrys_per_unit(self) -> f64 {
881 match self {
882 Self::Henry => 1.0,
883 Self::MilliHenry => 1e-3,
884 Self::MicroHenry => 1e-6,
885 Self::NanoHenry => 1e-9,
886 }
887 }
888}
889
890impl Inductance {
891 pub const fn from_h(val: f64) -> Self {
893 Self(val)
894 }
895
896 pub const fn from_mh(val: f64) -> Self {
898 Self(val * 1e-3)
899 }
900
901 pub const fn from_uh(val: f64) -> Self {
903 Self(val * 1e-6)
904 }
905
906 pub const fn from_nh(val: f64) -> Self {
908 Self(val * 1e-9)
909 }
910
911 pub const fn in_h(self) -> f64 {
913 self.0
914 }
915
916 pub const fn in_mh(self) -> f64 {
918 self.0 / 1e-3
919 }
920
921 pub const fn in_uh(self) -> f64 {
923 self.0 / 1e-6
924 }
925
926 pub const fn in_nh(self) -> f64 {
928 self.0 / 1e-9
929 }
930
931 pub fn in_unit(self, unit: InductanceUnit) -> f64 {
933 self.0 / unit.henrys_per_unit()
934 }
935
936 pub fn display_as(self, unit: InductanceUnit) -> DisplayWithUnit {
938 DisplayWithUnit {
939 value: self.in_unit(unit),
940 symbol: unit.symbol(),
941 }
942 }
943
944 pub fn abs(self) -> Self {
946 Self(self.0.abs())
947 }
948}
949
950impl_quantity_display!(Inductance, "H");
951
952impl_common_ops!(Inductance);