mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-11-19 19:46:09 +00:00
Compare commits
2 Commits
csv
...
d5db65467e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5db65467e | ||
|
|
ff97e6b0b6 |
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rustframe"
|
name = "rustframe"
|
||||||
authors = ["Palash Tyagi (https://github.com/Magnus167)"]
|
authors = ["Palash Tyagi (https://github.com/Magnus167)"]
|
||||||
version = "0.0.1-a.20250805"
|
version = "0.0.1-a.20250804"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|||||||
@@ -1,411 +0,0 @@
|
|||||||
use chrono::{NaiveDate, NaiveDateTime};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io::{self, BufRead, BufReader};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
/// Represents the target type for a CSV column.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum DataType {
|
|
||||||
Int,
|
|
||||||
Float,
|
|
||||||
Bool,
|
|
||||||
UInt,
|
|
||||||
String,
|
|
||||||
Date,
|
|
||||||
DateTime,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Represents a value parsed from the CSV.
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub enum Value {
|
|
||||||
Int(i64),
|
|
||||||
Float(f64),
|
|
||||||
Bool(bool),
|
|
||||||
UInt(u64),
|
|
||||||
String(String),
|
|
||||||
Date(NaiveDate),
|
|
||||||
DateTime(NaiveDateTime),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convenience alias for a parsed CSV record.
|
|
||||||
pub type Record = HashMap<String, Value>;
|
|
||||||
|
|
||||||
/// A simple CSV reader that reads records line by line.
|
|
||||||
pub struct CsvReader<R: BufRead> {
|
|
||||||
reader: R,
|
|
||||||
separators: Vec<char>,
|
|
||||||
headers: Vec<String>,
|
|
||||||
types: Option<HashMap<String, DataType>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Builder for [`CsvReader`] allowing chained configuration of headers, types, and separators.
|
|
||||||
pub struct CsvReaderBuilder<R: BufRead> {
|
|
||||||
reader: R,
|
|
||||||
separators: Vec<char>,
|
|
||||||
headers: Vec<String>,
|
|
||||||
types: Option<HashMap<String, DataType>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: BufRead> CsvReader<R> {
|
|
||||||
/// Create a new CSV reader from a [`BufRead`] source.
|
|
||||||
/// The first line is expected to contain headers.
|
|
||||||
/// `separators` is a list of characters considered as field separators.
|
|
||||||
/// `types` optionally maps column names to target data types.
|
|
||||||
pub fn new(
|
|
||||||
mut reader: R,
|
|
||||||
separators: Vec<char>,
|
|
||||||
types: Option<HashMap<String, DataType>>,
|
|
||||||
) -> io::Result<Self> {
|
|
||||||
let mut first_line = String::new();
|
|
||||||
reader.read_line(&mut first_line)?;
|
|
||||||
let headers = parse_line(&first_line, &separators);
|
|
||||||
Ok(Self {
|
|
||||||
reader,
|
|
||||||
separators,
|
|
||||||
headers,
|
|
||||||
types,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a reader with default settings (comma separator, automatic typing).
|
|
||||||
pub fn new_default(reader: R) -> io::Result<Self> {
|
|
||||||
Self::new(reader, vec![','], None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a reader with default separators and explicit type mapping.
|
|
||||||
pub fn new_with_types(reader: R, types: HashMap<String, DataType>) -> io::Result<Self> {
|
|
||||||
Self::new(reader, vec![','], Some(types))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Start building a reader from a source that lacks headers.
|
|
||||||
pub fn new_with_headers(reader: R, headers: Vec<String>) -> CsvReaderBuilder<R> {
|
|
||||||
CsvReaderBuilder {
|
|
||||||
reader,
|
|
||||||
separators: vec![','],
|
|
||||||
headers,
|
|
||||||
types: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the headers of the CSV file.
|
|
||||||
pub fn headers(&self) -> &[String] {
|
|
||||||
&self.headers
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Read the next record. Returns `Ok(None)` on EOF.
|
|
||||||
pub fn read_record(&mut self) -> io::Result<Option<Record>> {
|
|
||||||
let mut line = String::new();
|
|
||||||
if self.reader.read_line(&mut line)? == 0 {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
let fields = parse_line(&line, &self.separators);
|
|
||||||
let mut record = HashMap::new();
|
|
||||||
|
|
||||||
for (i, header) in self.headers.iter().enumerate() {
|
|
||||||
let field = fields.get(i).cloned().unwrap_or_default();
|
|
||||||
let value = match &self.types {
|
|
||||||
Some(map) => {
|
|
||||||
if let Some(dt) = map.get(header) {
|
|
||||||
parse_with_type(&field, dt)
|
|
||||||
} else {
|
|
||||||
Value::String(field)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => parse_auto(&field),
|
|
||||||
};
|
|
||||||
record.insert(header.clone(), value);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Some(record))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: BufRead> Iterator for CsvReader<R> {
|
|
||||||
type Item = io::Result<Record>;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
match self.read_record() {
|
|
||||||
Ok(Some(rec)) => Some(Ok(rec)),
|
|
||||||
Ok(None) => None,
|
|
||||||
Err(e) => Some(Err(e)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: BufRead> CsvReaderBuilder<R> {
|
|
||||||
/// Override field separators for the upcoming reader.
|
|
||||||
pub fn separators(mut self, separators: Vec<char>) -> Self {
|
|
||||||
self.separators = separators;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Finalize the builder with an explicit type mapping.
|
|
||||||
pub fn new_with_types(mut self, types: HashMap<String, DataType>) -> CsvReader<R> {
|
|
||||||
self.types = Some(types);
|
|
||||||
self.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Finalize the builder without specifying types.
|
|
||||||
pub fn build(self) -> CsvReader<R> {
|
|
||||||
CsvReader {
|
|
||||||
reader: self.reader,
|
|
||||||
separators: self.separators,
|
|
||||||
headers: self.headers,
|
|
||||||
types: self.types,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: BufRead> CsvReader<R> {
|
|
||||||
/// Read all remaining records into a vector.
|
|
||||||
pub fn read_all(&mut self) -> io::Result<Vec<Record>> {
|
|
||||||
let mut records = Vec::new();
|
|
||||||
while let Some(rec) = self.read_record()? {
|
|
||||||
records.push(rec);
|
|
||||||
}
|
|
||||||
Ok(records)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CsvReader<BufReader<File>> {
|
|
||||||
/// Create a [`CsvReader`] from a file path using comma separators and
|
|
||||||
/// automatic type detection.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use rustframe::csv::{CsvReader, Value};
|
|
||||||
/// # let path = std::env::temp_dir().join("from_path_auto.csv");
|
|
||||||
/// # std::fs::write(&path, "a,b\n1,true\n").unwrap();
|
|
||||||
/// let mut reader = CsvReader::from_path_auto(&path).unwrap();
|
|
||||||
/// let rec = reader.next().unwrap().unwrap();
|
|
||||||
/// assert_eq!(rec.get("a"), Some(&Value::Int(1)));
|
|
||||||
/// assert_eq!(rec.get("b"), Some(&Value::Bool(true)));
|
|
||||||
/// # std::fs::remove_file(path).unwrap();
|
|
||||||
/// ```
|
|
||||||
pub fn from_path_auto<P: AsRef<Path>>(path: P) -> io::Result<Self> {
|
|
||||||
let file = File::open(path)?;
|
|
||||||
let reader = BufReader::new(file);
|
|
||||||
CsvReader::new_default(reader)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an iterator over records from a file path using default settings.
|
|
||||||
pub fn reader<P: AsRef<Path>>(path: P) -> io::Result<CsvReader<BufReader<File>>> {
|
|
||||||
reader_with(path, vec![','], None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an iterator over records from a file path with custom separators and type mapping.
|
|
||||||
pub fn reader_with<P: AsRef<Path>>(
|
|
||||||
path: P,
|
|
||||||
separators: Vec<char>,
|
|
||||||
types: Option<HashMap<String, DataType>>,
|
|
||||||
) -> io::Result<CsvReader<BufReader<File>>> {
|
|
||||||
let file = File::open(path)?;
|
|
||||||
let reader = BufReader::new(file);
|
|
||||||
CsvReader::new(reader, separators, types)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Read an entire CSV file into memory using default settings.
|
|
||||||
pub fn read_file<P: AsRef<Path>>(path: P) -> io::Result<Vec<Record>> {
|
|
||||||
read_file_with(path, vec![','], None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Read an entire CSV file into memory with custom separators and type mapping.
|
|
||||||
pub fn read_file_with<P: AsRef<Path>>(
|
|
||||||
path: P,
|
|
||||||
separators: Vec<char>,
|
|
||||||
types: Option<HashMap<String, DataType>>,
|
|
||||||
) -> io::Result<Vec<Record>> {
|
|
||||||
let mut reader = reader_with(path, separators, types)?;
|
|
||||||
reader.read_all()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_with_type(s: &str, ty: &DataType) -> Value {
|
|
||||||
match ty {
|
|
||||||
DataType::Int => s
|
|
||||||
.parse::<i64>()
|
|
||||||
.map(Value::Int)
|
|
||||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
|
||||||
DataType::Float => s
|
|
||||||
.parse::<f64>()
|
|
||||||
.map(Value::Float)
|
|
||||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
|
||||||
DataType::Bool => s
|
|
||||||
.parse::<bool>()
|
|
||||||
.map(Value::Bool)
|
|
||||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
|
||||||
DataType::UInt => s
|
|
||||||
.parse::<u64>()
|
|
||||||
.map(Value::UInt)
|
|
||||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
|
||||||
DataType::String => Value::String(s.to_string()),
|
|
||||||
DataType::Date => s
|
|
||||||
.parse::<NaiveDate>()
|
|
||||||
.map(Value::Date)
|
|
||||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
|
||||||
DataType::DateTime => NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
|
|
||||||
.map(Value::DateTime)
|
|
||||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_auto(s: &str) -> Value {
|
|
||||||
if let Ok(i) = s.parse::<i64>() {
|
|
||||||
Value::Int(i)
|
|
||||||
} else if let Ok(f) = s.parse::<f64>() {
|
|
||||||
Value::Float(f)
|
|
||||||
} else if let Ok(b) = s.parse::<bool>() {
|
|
||||||
Value::Bool(b)
|
|
||||||
} else if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
|
|
||||||
Value::DateTime(dt)
|
|
||||||
} else if let Ok(d) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
|
|
||||||
Value::Date(d)
|
|
||||||
} else {
|
|
||||||
Value::String(s.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_line(line: &str, separators: &[char]) -> Vec<String> {
|
|
||||||
let mut fields = Vec::new();
|
|
||||||
let mut current = String::new();
|
|
||||||
let mut in_quotes: Option<char> = None;
|
|
||||||
let chars: Vec<char> = line.chars().collect();
|
|
||||||
let mut i = 0;
|
|
||||||
|
|
||||||
while i < chars.len() {
|
|
||||||
let c = chars[i];
|
|
||||||
if let Some(q) = in_quotes {
|
|
||||||
if c == q {
|
|
||||||
if i + 1 < chars.len() && chars[i + 1] == q {
|
|
||||||
current.push(q);
|
|
||||||
i += 1; // skip escaped quote
|
|
||||||
} else {
|
|
||||||
in_quotes = None;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
current.push(c);
|
|
||||||
}
|
|
||||||
} else if c == '"' || c == '\'' {
|
|
||||||
in_quotes = Some(c);
|
|
||||||
} else if separators.contains(&c) {
|
|
||||||
fields.push(current.clone());
|
|
||||||
current.clear();
|
|
||||||
} else if c == '\r' {
|
|
||||||
// Ignore carriage returns
|
|
||||||
} else if c == '\n' {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
current.push(c);
|
|
||||||
}
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fields.push(current);
|
|
||||||
fields
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use chrono::{NaiveDate, NaiveDateTime};
|
|
||||||
use std::io::Cursor;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_line() {
|
|
||||||
let line = "a,'b,c',\"d\"\"e\",f";
|
|
||||||
let fields = parse_line(line, &[',']);
|
|
||||||
assert_eq!(fields, vec!["a", "b,c", "d\"e", "f"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_reader_auto() {
|
|
||||||
let data = "a,b,c\n1,2.5,true\n4,5.0,false\n";
|
|
||||||
let cursor = Cursor::new(data);
|
|
||||||
let mut reader = CsvReader::new_default(cursor).unwrap();
|
|
||||||
let rec = reader.next().unwrap().unwrap();
|
|
||||||
assert_eq!(rec.get("a"), Some(&Value::Int(1)));
|
|
||||||
assert_eq!(rec.get("b"), Some(&Value::Float(2.5)));
|
|
||||||
assert_eq!(rec.get("c"), Some(&Value::Bool(true)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_reader_with_types() {
|
|
||||||
let data = "a,b,c\n1,2,3\n";
|
|
||||||
let cursor = Cursor::new(data);
|
|
||||||
let mut types = HashMap::new();
|
|
||||||
types.insert("a".to_string(), DataType::Int);
|
|
||||||
types.insert("b".to_string(), DataType::Int);
|
|
||||||
types.insert("c".to_string(), DataType::String);
|
|
||||||
let mut reader = CsvReader::new_with_types(cursor, types).unwrap();
|
|
||||||
let rec = reader.next().unwrap().unwrap();
|
|
||||||
assert_eq!(rec.get("a"), Some(&Value::Int(1)));
|
|
||||||
assert_eq!(rec.get("b"), Some(&Value::Int(2)));
|
|
||||||
assert_eq!(rec.get("c"), Some(&Value::String("3".to_string())));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_chain_headers_and_types() {
|
|
||||||
let data = "1,2\n3,4\n";
|
|
||||||
let cursor = Cursor::new(data);
|
|
||||||
let headers = vec!["x".to_string(), "y".to_string()];
|
|
||||||
let mut types = HashMap::new();
|
|
||||||
types.insert("x".to_string(), DataType::Int);
|
|
||||||
types.insert("y".to_string(), DataType::UInt);
|
|
||||||
let mut reader = CsvReader::new_with_headers(cursor, headers).new_with_types(types);
|
|
||||||
let rec = reader.next().unwrap().unwrap();
|
|
||||||
assert_eq!(rec.get("x"), Some(&Value::Int(1)));
|
|
||||||
assert_eq!(rec.get("y"), Some(&Value::UInt(2)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_date_types() {
|
|
||||||
let data = "d,dt\n2024-01-01,2024-01-01 12:00:00\n";
|
|
||||||
let cursor = Cursor::new(data);
|
|
||||||
let mut types = HashMap::new();
|
|
||||||
types.insert("d".to_string(), DataType::Date);
|
|
||||||
types.insert("dt".to_string(), DataType::DateTime);
|
|
||||||
let mut reader = CsvReader::new_with_types(cursor, types).unwrap();
|
|
||||||
let rec = reader.next().unwrap().unwrap();
|
|
||||||
let date = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
|
|
||||||
let datetime: NaiveDateTime = NaiveDate::from_ymd_opt(2024, 1, 1)
|
|
||||||
.unwrap()
|
|
||||||
.and_hms_opt(12, 0, 0)
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(rec.get("d"), Some(&Value::Date(date)));
|
|
||||||
assert_eq!(rec.get("dt"), Some(&Value::DateTime(datetime)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_read_file_all() {
|
|
||||||
let path = std::env::temp_dir().join("csv_full_test.csv");
|
|
||||||
std::fs::write(&path, "a,b\n1,2\n3,4\n").unwrap();
|
|
||||||
let records = read_file(&path).unwrap();
|
|
||||||
assert_eq!(records.len(), 2);
|
|
||||||
assert_eq!(records[1].get("b"), Some(&Value::Int(4)));
|
|
||||||
std::fs::remove_file(path).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_reader_from_path() {
|
|
||||||
let path = std::env::temp_dir().join("csv_iter_test.csv");
|
|
||||||
std::fs::write(&path, "a,b\n5,6\n").unwrap();
|
|
||||||
let mut iter = reader(&path).unwrap();
|
|
||||||
let rec = iter.next().unwrap().unwrap();
|
|
||||||
assert_eq!(rec.get("a"), Some(&Value::Int(5)));
|
|
||||||
assert_eq!(rec.get("b"), Some(&Value::Int(6)));
|
|
||||||
std::fs::remove_file(path).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_from_path_auto_method() {
|
|
||||||
let path = std::env::temp_dir().join("csv_method_auto.csv");
|
|
||||||
std::fs::write(&path, "a,b\n7,true\n").unwrap();
|
|
||||||
let mut reader = CsvReader::from_path_auto(&path).unwrap();
|
|
||||||
let rec = reader.next().unwrap().unwrap();
|
|
||||||
assert_eq!(rec.get("a"), Some(&Value::Int(7)));
|
|
||||||
assert_eq!(rec.get("b"), Some(&Value::Bool(true)));
|
|
||||||
std::fs::remove_file(path).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
//! CSV handling utilities.
|
|
||||||
//!
|
|
||||||
//! The [`csv`] module offers a flexible [`CsvReader`] with automatic type
|
|
||||||
//! detection and optional builders for custom headers and types.
|
|
||||||
//!
|
|
||||||
//! # Examples
|
|
||||||
//!
|
|
||||||
//! Read from a file with auto type detection:
|
|
||||||
//!
|
|
||||||
//! ```
|
|
||||||
//! use rustframe::csv::CsvReader;
|
|
||||||
//! # let path = std::env::temp_dir().join("docs_auto.csv");
|
|
||||||
//! # std::fs::write(&path, "a,b\n1,true\n").unwrap();
|
|
||||||
//! let mut reader = CsvReader::from_path_auto(&path).unwrap();
|
|
||||||
//! for rec in reader {
|
|
||||||
//! let rec = rec.unwrap();
|
|
||||||
//! println!("{:?}", rec);
|
|
||||||
//! }
|
|
||||||
//! # std::fs::remove_file(path).unwrap();
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! Specify column types explicitly:
|
|
||||||
//!
|
|
||||||
//! ```
|
|
||||||
//! use rustframe::csv::{CsvReader, DataType, Value};
|
|
||||||
//! use std::collections::HashMap;
|
|
||||||
//! use std::io::Cursor;
|
|
||||||
//! let data = "a,b\n1,2\n";
|
|
||||||
//! let mut types = HashMap::new();
|
|
||||||
//! types.insert("a".into(), DataType::Int);
|
|
||||||
//! types.insert("b".into(), DataType::Float);
|
|
||||||
//! let mut reader = CsvReader::new_with_types(Cursor::new(data), types).unwrap();
|
|
||||||
//! let rec = reader.next().unwrap().unwrap();
|
|
||||||
//! assert_eq!(rec.get("b"), Some(&Value::Float(2.0)));
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! Building from custom headers and types:
|
|
||||||
//!
|
|
||||||
//! ```
|
|
||||||
//! use rustframe::csv::{CsvReader, DataType, Value};
|
|
||||||
//! use std::collections::HashMap;
|
|
||||||
//! use std::io::Cursor;
|
|
||||||
//! let data = "1,2\n";
|
|
||||||
//! let headers = vec!["x".to_string(), "y".to_string()];
|
|
||||||
//! let mut types = HashMap::new();
|
|
||||||
//! types.insert("x".into(), DataType::Int);
|
|
||||||
//! types.insert("y".into(), DataType::UInt);
|
|
||||||
//! let mut reader = CsvReader::new_with_headers(Cursor::new(data), headers).new_with_types(types);
|
|
||||||
//! let rec = reader.next().unwrap().unwrap();
|
|
||||||
//! assert_eq!(rec.get("y"), Some(&Value::UInt(2)));
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! Reading an entire file into memory:
|
|
||||||
//!
|
|
||||||
//! ```
|
|
||||||
//! use rustframe::csv::read_file;
|
|
||||||
//! # let path = std::env::temp_dir().join("docs_full.csv");
|
|
||||||
//! # std::fs::write(&path, "a,b\n1,2\n3,4\n").unwrap();
|
|
||||||
//! let records = read_file(&path).unwrap();
|
|
||||||
//! assert_eq!(records.len(), 2);
|
|
||||||
//! # std::fs::remove_file(path).unwrap();
|
|
||||||
//! ```
|
|
||||||
|
|
||||||
pub mod csv_core;
|
|
||||||
|
|
||||||
pub use csv_core::{
|
|
||||||
CsvReader, CsvReaderBuilder, DataType, Record, Value, reader, reader_with,
|
|
||||||
read_file, read_file_with,
|
|
||||||
};
|
|
||||||
@@ -14,6 +14,3 @@ pub mod compute;
|
|||||||
|
|
||||||
/// Documentation for the [`crate::random`] module.
|
/// Documentation for the [`crate::random`] module.
|
||||||
pub mod random;
|
pub mod random;
|
||||||
|
|
||||||
/// Documentation for the [`crate::csv`] module.
|
|
||||||
pub mod csv;
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
//! assert_eq!(dates.count().unwrap(), 3);
|
//! assert_eq!(dates.count().unwrap(), 3);
|
||||||
//! ```
|
//! ```
|
||||||
pub mod dateutils;
|
pub mod dateutils;
|
||||||
|
pub mod spigots;
|
||||||
|
|
||||||
pub use dateutils::{BDateFreq, BDatesGenerator, BDatesList};
|
pub use dateutils::{BDateFreq, BDatesGenerator, BDatesList};
|
||||||
pub use dateutils::{DateFreq, DatesGenerator, DatesList};
|
pub use dateutils::{DateFreq, DatesGenerator, DatesList};
|
||||||
|
|||||||
243
src/utils/spigots.rs
Normal file
243
src/utils/spigots.rs
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
/// Iterator producing successive approximations of π using the Nilakantha series.
|
||||||
|
pub struct PiSpigot {
|
||||||
|
k: u64,
|
||||||
|
current: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for PiSpigot {
|
||||||
|
type Item = f64;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.k == 0 {
|
||||||
|
self.k = 1;
|
||||||
|
self.current = 3.0;
|
||||||
|
return Some(self.current);
|
||||||
|
}
|
||||||
|
let k = self.k as f64;
|
||||||
|
let term = 4.0 / ((2.0 * k) * (2.0 * k + 1.0) * (2.0 * k + 2.0));
|
||||||
|
if self.k % 2 == 1 {
|
||||||
|
self.current += term;
|
||||||
|
} else {
|
||||||
|
self.current -= term;
|
||||||
|
}
|
||||||
|
self.k += 1;
|
||||||
|
Some(self.current)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generator yielding approximations of π indefinitely.
|
||||||
|
pub fn pi_spigot() -> PiSpigot {
|
||||||
|
PiSpigot { k: 0, current: 0.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the first `n` approximations of π as a vector.
|
||||||
|
pub fn pi_values(n: usize) -> Vec<f64> {
|
||||||
|
pi_spigot().take(n).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generator yielding approximations of τ = 2π indefinitely.
|
||||||
|
pub fn tau_spigot() -> impl Iterator<Item = f64> {
|
||||||
|
pi_spigot().map(|v| v * 2.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the first `n` approximations of τ as a vector.
|
||||||
|
pub fn tau_values(n: usize) -> Vec<f64> {
|
||||||
|
tau_spigot().take(n).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Iterator producing successive approximations of the Euler-Mascheroni constant γ.
|
||||||
|
pub struct GammaSpigot {
|
||||||
|
n: u64,
|
||||||
|
harmonic: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for GammaSpigot {
|
||||||
|
type Item = f64;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.n += 1;
|
||||||
|
self.harmonic += 1.0 / self.n as f64;
|
||||||
|
let value = self.harmonic - (self.n as f64).ln();
|
||||||
|
Some(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generator yielding approximations of γ indefinitely.
|
||||||
|
pub fn gamma_spigot() -> GammaSpigot {
|
||||||
|
GammaSpigot {
|
||||||
|
n: 0,
|
||||||
|
harmonic: 0.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the first `n` approximations of γ as a vector.
|
||||||
|
pub fn gamma_values(n: usize) -> Vec<f64> {
|
||||||
|
gamma_spigot().take(n).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Iterator producing successive approximations of e using the series Σ 1/n!.
|
||||||
|
pub struct ESpigot {
|
||||||
|
n: u64,
|
||||||
|
sum: f64,
|
||||||
|
factorial: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for ESpigot {
|
||||||
|
type Item = f64;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.n == 0 {
|
||||||
|
self.n = 1;
|
||||||
|
self.sum = 1.0;
|
||||||
|
self.factorial = 1.0;
|
||||||
|
return Some(self.sum);
|
||||||
|
}
|
||||||
|
self.factorial *= self.n as f64;
|
||||||
|
self.sum += 1.0 / self.factorial;
|
||||||
|
self.n += 1;
|
||||||
|
Some(self.sum)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generator yielding approximations of e indefinitely.
|
||||||
|
pub fn e_spigot() -> ESpigot {
|
||||||
|
ESpigot {
|
||||||
|
n: 0,
|
||||||
|
sum: 0.0,
|
||||||
|
factorial: 1.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the first `n` approximations of e as a vector.
|
||||||
|
pub fn e_values(n: usize) -> Vec<f64> {
|
||||||
|
e_spigot().take(n).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Iterator producing successive approximations of √2 using Newton's method.
|
||||||
|
pub struct Sqrt2Spigot {
|
||||||
|
x: f64,
|
||||||
|
first: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for Sqrt2Spigot {
|
||||||
|
type Item = f64;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.first {
|
||||||
|
self.first = false;
|
||||||
|
Some(self.x)
|
||||||
|
} else {
|
||||||
|
self.x = 0.5 * (self.x + 2.0 / self.x);
|
||||||
|
Some(self.x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generator yielding approximations of √2 indefinitely.
|
||||||
|
pub fn sqrt2_spigot() -> Sqrt2Spigot {
|
||||||
|
Sqrt2Spigot {
|
||||||
|
x: 1.0,
|
||||||
|
first: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the first `n` approximations of √2 as a vector.
|
||||||
|
pub fn sqrt2_values(n: usize) -> Vec<f64> {
|
||||||
|
sqrt2_spigot().take(n).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn look_and_say(s: &str) -> String {
|
||||||
|
let mut chars = s.chars().peekable();
|
||||||
|
let mut result = String::new();
|
||||||
|
while let Some(c) = chars.next() {
|
||||||
|
let mut count = 1;
|
||||||
|
while let Some(&next) = chars.peek() {
|
||||||
|
if next == c {
|
||||||
|
chars.next();
|
||||||
|
count += 1;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.push_str(&format!("{}{}", count, c));
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Iterator producing successive ratios of lengths of the look-and-say sequence.
|
||||||
|
pub struct ConwaySpigot {
|
||||||
|
current: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for ConwaySpigot {
|
||||||
|
type Item = f64;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let next = look_and_say(&self.current);
|
||||||
|
let ratio = next.len() as f64 / self.current.len() as f64;
|
||||||
|
self.current = next;
|
||||||
|
Some(ratio)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generator yielding approximations of Conway's constant λ indefinitely.
|
||||||
|
pub fn conway_spigot() -> ConwaySpigot {
|
||||||
|
ConwaySpigot {
|
||||||
|
current: "1".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the first `n` approximations of Conway's constant as a vector.
|
||||||
|
pub fn conway_values(n: usize) -> Vec<f64> {
|
||||||
|
conway_spigot().take(n).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::f64::consts::{E, PI, TAU};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_pi_spigot() {
|
||||||
|
let vals = pi_values(1000);
|
||||||
|
let approx = vals.last().cloned().unwrap();
|
||||||
|
assert!((approx - PI).abs() < 1e-8);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tau_spigot() {
|
||||||
|
let vals = tau_values(1000);
|
||||||
|
let approx = vals.last().cloned().unwrap();
|
||||||
|
assert!((approx - TAU).abs() < 1e-8);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_gamma_spigot() {
|
||||||
|
let vals = gamma_values(100000);
|
||||||
|
let approx = vals.last().cloned().unwrap();
|
||||||
|
let gamma_true = 0.5772156649015329_f64;
|
||||||
|
assert!((approx - gamma_true).abs() < 1e-5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_e_spigot() {
|
||||||
|
let vals = e_values(10);
|
||||||
|
let approx = vals.last().cloned().unwrap();
|
||||||
|
assert!((approx - E).abs() < 1e-6);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sqrt2_spigot() {
|
||||||
|
let vals = sqrt2_values(6);
|
||||||
|
let approx = vals.last().cloned().unwrap();
|
||||||
|
assert!((approx - 2_f64.sqrt()).abs() < 1e-12);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_conway_spigot() {
|
||||||
|
let vals = conway_values(25);
|
||||||
|
let approx = vals.last().cloned().unwrap();
|
||||||
|
let conway = 1.3035772690342964_f64;
|
||||||
|
assert!((approx - conway).abs() < 1e-2);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user