1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
use crate::output::{Format, Formatted};
use num_bigint::BigInt;
use num_integer::Integer;
use num_rational::Ratio;
pub use num_rational::Rational64 as Rational;
use num_traits::{
    CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, One, Signed, ToPrimitive,
    Zero,
};
use std::cmp::Ordering;
use std::fmt::{self, Write};
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};

/// The actual number part of a numeric sass or css value.
///
/// Only the actual numeric value is included, not any unit.
/// Internally, a number is represented as either a rational or a
/// floating-point value as needed.
#[derive(Clone, PartialEq, Eq, PartialOrd)]
pub struct Number {
    value: NumValue,
}

#[derive(Clone, Debug)]
enum NumValue {
    Rational(Rational),
    // Note: Using a Box here is essentially a double indirection, but
    // it also makes the size of a NumValue a lot smaller (24 bytes
    // for a Ratio<i64> plus denominator and padding rather than the
    // 48 bytes (two Vec headers) that is put in this box.
    BigRational(Box<Ratio<BigInt>>),
    Float(f64),
}

impl From<Rational> for NumValue {
    fn from(value: Rational) -> Self {
        Self::Rational(value)
    }
}
impl From<i64> for NumValue {
    fn from(value: i64) -> Self {
        Self::Rational(Rational::from_integer(value))
    }
}
impl From<Ratio<BigInt>> for NumValue {
    fn from(value: Ratio<BigInt>) -> Self {
        Self::BigRational(Box::new(value))
    }
}
impl From<f64> for NumValue {
    fn from(value: f64) -> Self {
        Self::Float(value)
    }
}

impl NumValue {
    pub fn is_negative(&self) -> bool {
        match self {
            Self::Rational(s) => s.is_negative(),
            Self::BigRational(s) => s.is_negative(),
            Self::Float(s) => s.is_sign_negative(),
        }
    }
}

impl Neg for Number {
    type Output = Self;
    fn neg(self) -> Self {
        -&self
    }
}
impl Neg for &Number {
    type Output = Number;
    fn neg(self) -> Number {
        match &self.value {
            NumValue::Rational(s) => (-s).into(),
            NumValue::BigRational(s) => (-s.as_ref()).into(),
            NumValue::Float(s) => (-s).into(),
        }
    }
}

impl Eq for NumValue {}
impl PartialEq for NumValue {
    fn eq(&self, rhs: &Self) -> bool {
        self.partial_cmp(rhs) == Some(Ordering::Equal)
    }
}
impl PartialOrd for NumValue {
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        match (self, rhs) {
            (Self::Rational(s), Self::Rational(r)) => s.partial_cmp(r),
            (Self::Rational(s), Self::BigRational(r)) => {
                biggen(s).partial_cmp(r)
            }
            (Self::BigRational(s), Self::Rational(r)) => {
                s.as_ref().partial_cmp(&biggen(r))
            }
            (Self::BigRational(s), Self::BigRational(r)) => s.partial_cmp(r),
            (Self::Float(s), r) => s.partial_cmp(&r.into()),
            (s, Self::Float(r)) => f64::from(s).partial_cmp(r),
        }
    }
}

impl Mul for NumValue {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self {
        &self * &rhs
    }
}
impl Mul for &NumValue {
    type Output = NumValue;
    fn mul(self, rhs: Self) -> NumValue {
        match (self, rhs) {
            (NumValue::Rational(s), NumValue::Rational(r)) => {
                if (s.is_zero() && r.is_negative())
                    || (r.is_zero() && s.is_negative())
                {
                    (-0.0).into()
                } else {
                    s.checked_mul(r)
                        .map(Into::into)
                        .unwrap_or_else(|| (biggen(s) * biggen(r)).into())
                }
            }
            (NumValue::Rational(s), NumValue::BigRational(r)) => {
                if (s.is_zero() && r.is_negative())
                    || (r.is_zero() && s.is_negative())
                {
                    (-0.0).into()
                } else {
                    (biggen(s) * r.as_ref()).into()
                }
            }
            (NumValue::BigRational(s), NumValue::Rational(r)) => {
                if (s.is_zero() && r.is_negative())
                    || (r.is_zero() && s.is_negative())
                {
                    (-0.0).into()
                } else {
                    (s.as_ref() * biggen(r)).into()
                }
            }
            (NumValue::BigRational(s), NumValue::BigRational(r)) => {
                if (s.is_zero() && r.is_negative())
                    || (r.is_zero() && s.is_negative())
                {
                    (-0.0).into()
                } else {
                    (s.as_ref() * r.as_ref()).into()
                }
            }
            (NumValue::Float(s), r) => (s * f64::from(r)).into(),
            (s, NumValue::Float(r)) => (f64::from(s) * r).into(),
        }
    }
}
impl Mul<&Rational> for &NumValue {
    type Output = NumValue;
    fn mul(self, rhs: &Rational) -> NumValue {
        match self {
            NumValue::Rational(s) => (s * rhs).into(),
            NumValue::BigRational(s) => (s.as_ref() * biggen(rhs)).into(),
            NumValue::Float(s) => {
                rhs.to_f64().map_or(f64::NAN, |r| s * r).into()
            }
        }
    }
}
impl Mul<i64> for NumValue {
    type Output = Self;
    fn mul(self, rhs: i64) -> Self {
        match self {
            s @ Self::Rational(_) => s * Self::from(rhs),
            Self::BigRational(s) => (s.as_ref() * BigInt::from(rhs)).into(),
            Self::Float(s) => (s * (rhs as f64)).into(),
        }
    }
}

impl Mul for Number {
    type Output = Self;
    fn mul(mut self, rhs: Self) -> Self {
        self.value = self.value * rhs.value;
        self
    }
}
impl Mul for &Number {
    type Output = Number;
    fn mul(self, rhs: Self) -> Self::Output {
        Number {
            value: &self.value * &rhs.value,
        }
    }
}

impl Rem for NumValue {
    type Output = Self;
    fn rem(self, rhs: Self) -> Self {
        &self % &rhs
    }
}
impl Rem for &NumValue {
    type Output = NumValue;
    fn rem(self, rhs: Self) -> NumValue {
        if rhs.is_zero() {
            return (f64::from(self) % 0.).into();
        }
        fn float_rem(s: f64, r: f64) -> f64 {
            if r.is_infinite() && s.signum() * r.signum() < 0. {
                f64::NAN
            } else {
                s % r
            }
        }
        match (self, rhs) {
            (NumValue::Rational(s), NumValue::Rational(r)) => (s % r).into(),
            (NumValue::Rational(s), NumValue::BigRational(r)) => {
                (biggen(s) % r.as_ref()).into()
            }
            (NumValue::BigRational(s), NumValue::Rational(r)) => {
                (s.as_ref() % biggen(r)).into()
            }
            (NumValue::BigRational(s), NumValue::BigRational(r)) => {
                (s.as_ref() % r.as_ref()).into()
            }
            (NumValue::Float(s), r) => float_rem(*s, r.into()).into(),
            (s, NumValue::Float(r)) => float_rem(s.into(), *r).into(),
        }
    }
}
impl Div for NumValue {
    type Output = Self;
    fn div(self, rhs: Self) -> Self {
        &self / &rhs
    }
}
impl Div for &NumValue {
    type Output = NumValue;
    fn div(self, rhs: Self) -> NumValue {
        if rhs.is_zero() {
            return (f64::from(self) / f64::from(rhs)).into();
        }
        match (self, rhs) {
            (NumValue::Rational(s), NumValue::Rational(r)) => s
                .checked_div(r)
                .map(Into::into)
                .unwrap_or_else(|| (biggen(s) / biggen(r)).into()),
            (NumValue::Rational(s), NumValue::BigRational(r)) => {
                (biggen(s) / r.as_ref()).into()
            }
            (NumValue::BigRational(s), NumValue::Rational(r)) => {
                (s.as_ref() / biggen(r)).into()
            }
            (NumValue::BigRational(s), NumValue::BigRational(r)) => {
                (s.as_ref() / r.as_ref()).into()
            }
            (NumValue::Float(s), r) => (s / f64::from(r)).into(),
            (s, NumValue::Float(r)) => (f64::from(s) / r).into(),
        }
    }
}

impl Div<i64> for NumValue {
    type Output = Self;
    fn div(self, rhs: i64) -> Self {
        match self {
            Self::Rational(s) => (s / rhs).into(),
            Self::BigRational(s) => (s.as_ref() / BigInt::from(rhs)).into(),
            Self::Float(s) => (s / (rhs as f64)).into(),
        }
    }
}

impl Add for NumValue {
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        match (self, rhs) {
            (Self::Rational(s), Self::Rational(r)) => s
                .checked_add(&r)
                .map(Into::into)
                .unwrap_or_else(|| (biggen(&s) + biggen(&r)).into()),
            (Self::Rational(s), Self::BigRational(r)) => {
                (biggen(&s) + r.as_ref()).into()
            }
            (Self::BigRational(s), Self::Rational(r)) => {
                (s.as_ref() + biggen(&r)).into()
            }
            (Self::BigRational(s), Self::BigRational(r)) => {
                (s.as_ref() + r.as_ref()).into()
            }
            (Self::Float(s), r) => (s + f64::from(r)).into(),
            (s, Self::Float(r)) => (f64::from(s) + r).into(),
        }
    }
}
impl Sub for NumValue {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        &self - &rhs
    }
}
impl Sub for &NumValue {
    type Output = NumValue;
    fn sub(self, rhs: Self) -> NumValue {
        match (self, rhs) {
            (NumValue::Rational(s), NumValue::Rational(r)) => s
                .checked_sub(r)
                .map(Into::into)
                .unwrap_or_else(|| (biggen(s) - biggen(r)).into()),
            (NumValue::Rational(s), NumValue::BigRational(r)) => {
                (biggen(s) - r.as_ref()).into()
            }
            (NumValue::BigRational(s), NumValue::Rational(r)) => {
                (s.as_ref() - biggen(r)).into()
            }
            (NumValue::BigRational(s), NumValue::BigRational(r)) => {
                (s.as_ref() - r.as_ref()).into()
            }
            (NumValue::Float(s), r) => (s - f64::from(r)).into(),
            (s, NumValue::Float(r)) => (f64::from(s) - r).into(),
        }
    }
}

fn biggen(val: &Rational) -> Ratio<BigInt> {
    Ratio::<BigInt>::new((*val.numer()).into(), (*val.denom()).into())
}

impl One for NumValue {
    fn one() -> Self {
        Self::Rational(One::one())
    }
}
impl One for Number {
    fn one() -> Self {
        Self {
            value: NumValue::one(),
        }
    }
}

impl Zero for NumValue {
    fn zero() -> Self {
        Self::Rational(Zero::zero())
    }
    fn is_zero(&self) -> bool {
        match self {
            Self::Rational(r) => r.is_zero(),
            Self::BigRational(r) => r.is_zero(),
            Self::Float(r) => r.is_zero(),
        }
    }
}

impl NumValue {
    pub fn ceil(&self) -> Self {
        match self {
            Self::Rational(r) => r.ceil().into(),
            Self::BigRational(r) => r.ceil().into(),
            Self::Float(r) => r.ceil().into(),
        }
    }
    pub fn floor(&self) -> Self {
        match self {
            Self::Rational(r) => r.floor().into(),
            Self::BigRational(r) => r.floor().into(),
            Self::Float(r) => r.floor().into(),
        }
    }
    pub fn trunc(&self) -> Self {
        match self {
            Self::Rational(r) => r.trunc().into(),
            Self::BigRational(r) => r.trunc().into(),
            Self::Float(r) => r.trunc().into(),
        }
    }
    pub fn round(&self) -> Self {
        match self {
            Self::Rational(r) => r.round().into(),
            Self::BigRational(r) => r.round().into(),
            Self::Float(r) => r.round().into(),
        }
    }
    pub fn signum(&self) -> Self {
        match self {
            Self::Rational(r) => r.signum().into(),
            Self::BigRational(r) => r.signum().into(),
            Self::Float(r) => {
                // The sass spec says sign(-0) is -0
                // ... which may be a bug, but here's to compatibility:
                if r.is_zero() {
                    (*r).into()
                } else {
                    r.signum().into()
                }
            }
        }
    }
    pub fn as_ratio(&self) -> Result<Rational, BadNumber> {
        match self {
            Self::Rational(r) => Ok(*r),
            Self::BigRational(r) => {
                let mut numer = r.numer().clone();
                let mut denom = r.denom().clone();
                loop {
                    let tn = i64::try_from(&numer);
                    let td = i64::try_from(&denom);
                    if let (Ok(n), Ok(d)) = (tn, td) {
                        return Ok(Ratio::new(n, d));
                    }
                    numer /= 32;
                    denom /= 32;
                    if denom.is_zero() {
                        return Err(BadNumber::TooLarge);
                    }
                }
            }
            Self::Float(r) => {
                Ratio::approximate_float(*r).ok_or(BadNumber::BadFloat(*r))
            }
        }
    }
}

impl Number {
    /// Create a rational number.
    pub fn rational(num: i64, denom: i64) -> Self {
        Rational::new(num, denom).into()
    }
    /// Get this number as a rational number.
    ///
    /// If the value is bignum rational or floating point, it is
    /// approximated as long as it is withing range, otherwises an
    /// error is returned.
    pub fn as_ratio(&self) -> Result<Rational, BadNumber> {
        self.value.as_ratio()
    }

    /// Get a copy of this number, rounded away from zero.
    pub fn ceil(&self) -> Self {
        Self {
            value: self.value.ceil(),
        }
    }
    /// Get a copy of this number, rounded down.
    pub fn floor(&self) -> Self {
        Self {
            value: self.value.floor(),
        }
    }
    /// Get a copy of this number, rounded towards zero.
    pub fn trunc(&self) -> Self {
        Self {
            value: self.value.trunc(),
        }
    }
    /// Get a copy of this number, rounded to nearest integer.
    pub fn round(&self) -> Self {
        Self {
            value: self.value.round(),
        }
    }
    /// Get the sign of this number.
    pub fn signum(&self) -> Self {
        Self {
            value: self.value.signum(),
        }
    }
    /// Computes the absolute value of the number, retaining the flags.
    pub fn abs(&self) -> Self {
        match &self.value {
            NumValue::Rational(s) => s.abs().into(),
            NumValue::BigRational(s) => s.abs().into(),
            NumValue::Float(s) => s.abs().into(),
        }
    }
    /// Return true if this number is less than zero.
    pub fn is_negative(&self) -> bool {
        self.value.is_negative()
    }

    /// Return true if this value is neither inifinte nor NaN.
    pub fn is_finite(&self) -> bool {
        match self.value {
            NumValue::Rational(_) => true,
            NumValue::BigRational(_) => true,
            NumValue::Float(r) => r.is_finite(),
        }
    }

    /// Returns true if the number is an integer.
    pub fn into_integer(self) -> Result<i64, Self> {
        fn float_int(s: f64) -> Option<i64> {
            let sr = s.round();
            if (sr - s).abs() <= std::f32::EPSILON.into() {
                Some(sr as i64)
            } else {
                None
            }
        }
        match &self.value {
            NumValue::Rational(s) => {
                if s.is_integer() {
                    Ok(s.to_integer())
                } else {
                    s.to_f64().and_then(float_int).ok_or(self)
                }
            }
            NumValue::BigRational(s) => {
                if s.is_integer() {
                    i64::try_from(s.to_integer()).map_err(|_| self)
                } else {
                    s.to_f64().and_then(float_int).ok_or(self)
                }
            }
            NumValue::Float(s) => float_int(*s).ok_or(self),
        }
    }

    /// Converts to an integer, rounding towards zero.
    ///
    /// An integer that is too big to fit in an i64 returns `None`.
    pub fn to_integer(&self) -> Option<i64> {
        match &self.value {
            NumValue::Rational(s) => Some(s.to_integer()),
            NumValue::BigRational(s) => i64::try_from(s.to_integer()).ok(),
            NumValue::Float(s) => Some(s.ceil() as i64),
        }
    }
    /// Computes self^p.
    pub fn powi(self, p: i32) -> Self {
        match self.value {
            NumValue::Rational(s) => s.pow(p).into(),
            NumValue::BigRational(s) => s.pow(p).into(),
            NumValue::Float(s) => s.powi(p).into(),
        }
    }

    /// Get a reference to this `Value` bound to an output format.
    pub fn format(&self, format: Format) -> Formatted<Self> {
        Formatted {
            value: self,
            format,
        }
    }
}

impl From<i64> for Number {
    fn from(value: i64) -> Self {
        Self {
            value: value.into(),
        }
    }
}
impl From<i32> for Number {
    fn from(value: i32) -> Self {
        Self::from(i64::from(value))
    }
}
impl From<usize> for Number {
    fn from(value: usize) -> Self {
        match i64::try_from(value) {
            Ok(v) => v.into(),
            Err(_) => Ratio::from_integer(BigInt::from(value)).into(),
        }
    }
}
impl From<Rational> for Number {
    fn from(value: Rational) -> Self {
        Self {
            value: value.into(),
        }
    }
}
impl From<f64> for Number {
    fn from(value: f64) -> Self {
        Self {
            value: value.into(),
        }
    }
}
impl From<Ratio<BigInt>> for Number {
    fn from(value: Ratio<BigInt>) -> Self {
        Self {
            value: value.into(),
        }
    }
}
impl Add for Number {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self {
            value: self.value + rhs.value,
        }
    }
}
impl Mul<Rational> for Number {
    type Output = Self;
    fn mul(self, rhs: Rational) -> Self {
        Self {
            value: &self.value * &rhs,
        }
    }
}
impl Mul<i64> for Number {
    type Output = Self;
    fn mul(self, rhs: i64) -> Self {
        Self {
            value: self.value * rhs,
        }
    }
}
impl Div for Number {
    type Output = Self;
    fn div(self, rhs: Self) -> Self {
        Self {
            value: self.value / rhs.value,
        }
    }
}
impl Div<i64> for Number {
    type Output = Self;
    fn div(self, rhs: i64) -> Self {
        Self {
            value: self.value / rhs,
        }
    }
}

impl From<Number> for f64 {
    fn from(val: Number) -> Self {
        Self::from(val.value)
    }
}
impl From<NumValue> for f64 {
    fn from(val: NumValue) -> Self {
        Self::from(&val)
    }
}
impl From<&NumValue> for f64 {
    fn from(val: &NumValue) -> Self {
        match val {
            NumValue::Rational(s) => s.to_f64().unwrap_or(Self::NAN),
            NumValue::BigRational(s) => s.to_f64().unwrap_or(Self::NAN),
            NumValue::Float(s) => *s,
        }
    }
}

impl<'a> Div for &'a Number {
    type Output = Number;
    fn div(self, rhs: Self) -> Self::Output {
        Number {
            value: &self.value / &rhs.value,
        }
    }
}

impl Mul<&Rational> for &Number {
    type Output = Number;
    fn mul(self, rhs: &Rational) -> Self::Output {
        Number {
            value: &self.value * rhs,
        }
    }
}

impl Rem for &Number {
    type Output = Number;
    fn rem(self, rhs: Self) -> Self::Output {
        // The modulo operator in rust handles negative values different from
        // num-rational.
        let a = &self.value;
        let b = &rhs.value;
        let result = a % b;
        let result = if !a.is_zero() && (b.is_negative() != a.is_negative()) {
            result + b.clone()
        } else {
            result
        };
        Number { value: result }
    }
}

impl Sub for &Number {
    type Output = Number;
    fn sub(self, rhs: Self) -> Self::Output {
        Number {
            value: &self.value - &rhs.value,
        }
    }
}

impl Zero for Number {
    fn zero() -> Self {
        Self::from(0)
    }
    fn is_zero(&self) -> bool {
        self.value.is_zero()
    }
}

impl<'a> fmt::Display for Formatted<'a, Number> {
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        match self.value.value {
            NumValue::Rational(ref s) => do_fmt(out, s, self.format),
            NumValue::BigRational(ref s) => do_fmt(out, s, self.format),
            NumValue::Float(ref s) => {
                if s.is_nan() {
                    write!(out, "NaN")
                } else if s.is_infinite() {
                    write!(
                        out,
                        "{}infinity",
                        if s.is_sign_negative() { "-" } else { "" }
                    )
                } else {
                    let mut frac = s.fract();
                    let mut whole = s.trunc().abs();
                    let mut dec = String::with_capacity(if frac.is_zero() {
                        0
                    } else {
                        self.format.precision
                    });

                    if !frac.is_zero() {
                        let max_decimals = 16 - whole.log10().ceil() as usize;
                        for _ in 1..max_decimals.min(self.format.precision) {
                            frac *= 10.;
                            write!(dec, "{}", (frac as i8).abs())?;
                            frac = frac.fract();
                            if frac.is_zero() {
                                break;
                            }
                        }
                        if !frac.is_zero() {
                            let end = (frac * 10.).round().abs() as u8;
                            if end == 10 {
                                loop {
                                    match dec.pop() {
                                        Some('9') => continue,
                                        None => {
                                            whole += 1.;
                                            break;
                                        }
                                        Some(c) => {
                                            dec.push(char::from(c as u8 + 1));
                                            break;
                                        }
                                    }
                                }
                            } else if end.is_zero() {
                                loop {
                                    match dec.pop() {
                                        Some('0') => continue,
                                        None => break,
                                        Some(c) => {
                                            dec.push(c);
                                            break;
                                        }
                                    }
                                }
                            } else {
                                write!(dec, "{end}")?;
                            }
                        }
                    }

                    if s.is_sign_negative()
                        && (!whole.is_zero() || !dec.is_empty())
                    {
                        out.write_char('-')?;
                    }

                    let skip_zero = self.format.is_compressed();
                    if !(whole.is_zero() && skip_zero && !dec.is_empty()) {
                        write!(out, "{whole}")?;
                    }

                    if !dec.is_empty() {
                        write!(out, ".{dec}")?;
                    }
                    Ok(())
                }
            }
        }
    }
}

fn do_fmt<N>(
    out: &mut fmt::Formatter,
    value: &Ratio<N>,
    format: Format,
) -> fmt::Result
where
    N: Clone + fmt::Display + Integer + Signed + From<i8>,
{
    let ten: N = 10i8.into();
    let mut frac = value.fract();

    let mut whole = value.to_integer().abs();
    let mut dec = String::with_capacity(if frac.is_zero() {
        0
    } else {
        format.precision
    });

    if !frac.is_zero() {
        for _ in 0..(format.precision - 1) {
            frac = frac * &ten;
            write!(dec, "{}", frac.to_integer().abs())?;
            frac = frac.fract();
            if frac.is_zero() {
                break;
            }
        }
        if !frac.is_zero() {
            let end = (frac * &ten).round().abs().to_integer();
            if end == ten {
                loop {
                    match dec.pop() {
                        Some('9') => continue,
                        None => {
                            whole = whole + N::one();
                            break;
                        }
                        Some(c) => {
                            dec.push(char::from(c as u8 + 1));
                            break;
                        }
                    }
                }
            } else if end.is_zero() {
                loop {
                    match dec.pop() {
                        Some('0') => continue,
                        None => break,
                        Some(c) => {
                            dec.push(c);
                            break;
                        }
                    }
                }
            } else {
                write!(dec, "{end}")?;
            }
        }
    }

    if value.is_negative() && (!whole.is_zero() || !dec.is_empty()) {
        out.write_char('-')?;
    }

    let skip_zero = format.is_compressed();
    if !(whole.is_zero() && skip_zero && !dec.is_empty()) {
        write!(out, "{whole}")?;
    }

    if !dec.is_empty() {
        write!(out, ".{dec}")?;
    }
    Ok(())
}

impl fmt::Debug for Number {
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        out.write_str("Number ")?;
        match &self.value {
            NumValue::Rational(r) => {
                r.numer().fmt(out)?;
                out.write_str(" / ")?;
                r.denom().fmt(out)
            }
            NumValue::BigRational(r) => {
                out.debug_list().entry(r.numer()).entry(r.denom()).finish()
            }
            NumValue::Float(f) => f.fmt(out),
        }
    }
}

/// Error signifying that a number could not be approximated as
/// a rational.
pub enum BadNumber {
    /// The number was to large.
    TooLarge,
    /// The number was a "special" float value.
    BadFloat(f64),
}

impl From<BadNumber> for String {
    fn from(n: BadNumber) -> Self {
        n.to_string()
    }
}

impl fmt::Display for BadNumber {
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::TooLarge => out.write_str("Number too large"),
            Self::BadFloat(f) => write!(out, "Bad float: {f}"),
        }
    }
}

#[test]
fn debug_integer() {
    assert_eq!(format!("{:?}", Number::from(17)), "Number 17 / 1",);
}
#[test]
fn debug_long_integer() {
    assert_eq!(format!("{:?}", Number::from(17)), "Number 17 / 1",);
}

#[test]
fn debug_biginteger() {
    assert_eq!(
        format!(
            "{:?}",
            Number::from(Ratio::<BigInt>::new(17.into(), 1.into()))
        ),
        "Number [17, 1]",
    );
}

#[test]
fn debug_float() {
    assert_eq!(format!("{:?}", Number::from(17.5)), "Number 17.5",);
}

#[test]
fn debug_int_value() {
    assert_eq!(
        format!("{:#?}", crate::sass::Value::scalar(17)),
        "Numeric(\
         \n    Number 17 / 1; UnitSet [],\
         \n)",
    );
}