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
use crate::datatypes::{AnyType, ToStr};
use crate::{
    frame::DataFrame,
    series::{chunked_array::iterator::ChunkIterator, series::Series},
};
use num::{Num, NumCast};
use std::{
    fmt,
    fmt::{Debug, Display, Formatter},
};

impl Debug for Series {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        const LIMIT: usize = 10;

        macro_rules! format_series {
            ($a:ident, $name:expr) => {{
                write![f, "Series: {} \n[\n", $name]?;
                $a.iter().take(LIMIT).for_each(|v| {
                    match v {
                        Some(v) => {
                            write!(f, "\t{}\n", v).ok();
                        }
                        None => {
                            write!(f, "\tnull").ok();
                        }
                    };
                });
                write![f, "]"]
            }};
        }

        match self {
            Series::Int32(a) => format_series!(a, "i32"),
            Series::Int64(a) => format_series!(a, "i64"),
            Series::UInt32(a) => format_series!(a, "u32"),
            Series::Bool(a) => format_series!(a, "bool"),
            Series::Float32(a) => format_series!(a, "f32"),
            Series::Float64(a) => format_series!(a, "f64"),
            Series::Utf8(a) => {
                write![f, "Series: str \n[\n"]?;
                a.iter().take(LIMIT).for_each(|v| {
                    write!(f, "\t{}\n", &v[..LIMIT]).ok();
                });
                write![f, "]"]
            }
            _ => write!(f, "hello"),
        }
    }
}

impl Display for Series {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Debug::fmt(self, f)
    }
}

impl Debug for DataFrame {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(self, f)
    }
}

impl Display for DataFrame {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        for field in self.schema.fields() {
            write!(f, "{:>15}", field.name())?;
        }
        write!(f, "\n")?;
        for field in self.schema.fields() {
            write!(f, "{:>15}", field.data_type().to_str())?;
        }
        write!(f, "\n")?;
        for _ in self.schema.fields() {
            write!(f, "{:>15}", "---")?;
        }
        write!(f, "\n\n")?;

        for i in 0..10 {
            let opt = self.get(i);
            if let Some(row) = opt {
                for v in row {
                    write!(f, "{}", v)?;
                }
                write!(f, "\n")?;
            }
        }
        Ok(())
    }
}

fn fmt_integer<T: Num + NumCast>(f: &mut Formatter<'_>, width: usize, v: T) -> fmt::Result {
    let v: i64 = NumCast::from(v).unwrap();
    if v > 9999 {
        write!(f, "{:>width$e}", v, width = width)
    } else {
        write!(f, "{:>width$}", v, width = width)
    }
}

fn fmt_float<T: Num + NumCast>(f: &mut Formatter<'_>, width: usize, v: T) -> fmt::Result {
    let v: f64 = NumCast::from(v).unwrap();
    let v = (v * 1000.).round() / 1000.;
    if v > 9999. || v < 0.001 {
        write!(f, "{:>width$e}", v, width = width)
    } else {
        write!(f, "{:>width$}", v, width = width)
    }
}

impl Display for AnyType<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let width = 15;
        match self {
            AnyType::Null => write!(f, "{:>width$}", "null", width = width),
            AnyType::U32(v) => write!(f, "{:>width$}", v, width = width),
            AnyType::I32(v) => fmt_integer(f, width, *v),
            AnyType::I64(v) => fmt_integer(f, width, *v),
            AnyType::F32(v) => fmt_float(f, width, *v),
            AnyType::F64(v) => fmt_float(f, width, *v),
            AnyType::Bool(v) => write!(f, "{:>width$}", v, width = width),
            AnyType::Str(v) => write!(f, "{:>width$}", v, width = width),
            AnyType::Date64(v) => write!(f, "{:>width$}", v, width = width),
            AnyType::Date32(v) => write!(f, "{:>width$}", v, width = width),
            AnyType::Time64(v, _) => write!(f, "{:>width$}", v, width = width),
            AnyType::Duration(v, _) => write!(f, "{:>width$}", v, width = width),
        }
    }
}