Commit 841ac5e9 authored by Christopher Silva's avatar Christopher Silva

initial commit

parents
/target
**/*.rs.bk
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "spreadsheet"
version = "0.1.0"
[package]
name = "spreadsheet"
version = "0.1.0"
authors = ["csos95 <csoscss@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
use std::fmt;
#[derive(Clone, Debug)]
pub enum Cell {
Int(i32),
Float(f32),
String(String),
}
impl fmt::Display for Cell {
fn fmt(&self, fm: &mut fmt::Formatter) -> Result<(), fmt::Error>{
match self {
Cell::Int(i) => write!(fm, "{}", i),
Cell::Float(f) => write!(fm, "{}", f),
Cell::String(s) => write!(fm, "{}", s),
}
}
}
impl Cell {
pub fn new_int(value: i32) -> Self {
Cell::Int(value)
}
pub fn new_float(value: f32) -> Self {
Cell::Float(value)
}
pub fn new_string(value: String) -> Self {
Cell::String(value)
}
}
use std::default::Default;
use crate::cell::Cell;
use crate::sheet::Range;
struct Acc {
int: i32,
float: f32,
flag: bool,
}
impl Default for Acc {
fn default() -> Self {
Acc {
int: 0,
float: 0.0,
flag: false,
}
}
}
impl Acc {
fn finish(&self) -> Cell {
match (self.int, self.float, self.flag) {
(i, _, false) => Cell::Int(i),
(_, f, true) => Cell::Float(f),
}
}
}
pub fn sum(cells: Range) -> Cell {
cells.iter()
.flatten()
.fold(Acc::default(), |mut acc, cell| {
match cell {
Some(Cell::Int(i)) => acc.int += i,
Some(Cell::Float(f)) => {
acc.flag = true;
acc.float += f;
},
_ => {},
}
acc
}).finish()
}
pub mod cell;
pub mod function;
pub mod sheet;
pub mod workbook;
use spreadsheet::function;
use spreadsheet::workbook::Workbook;
fn main() {
let mut workbook = Workbook::new();
let sheet = workbook.add_sheet("Test".into());
println!("inserting cells");
for i in 0..200 {
for j in 0..200 {
sheet.write_string(i, j, format!("string {} {}", i, j));
}
}
println!("rendering ranges");
for i in 0..100 {
for j in 0..100 {
let rendered = sheet.render_range(i, j, 100, 100);
}
}
println!("range r0c0:r3c3: {:?}", sheet.render_range(0, 0, 10, 10));
for i in 0..10 {
for j in 0..10 {
sheet.write_int(i, j, (100 * i + j) as i32);
}
}
let range = sheet.range(0, 0, 10, 10);
let sum = function::sum(range);
println!("sum(r0c0:r9c9): {}", sum);
for i in 0..10 {
for j in 0..10 {
sheet.write_float(i, j, (100 * i + j) as f32);
}
}
let range = sheet.range(0, 0, 10, 1);
let sum = function::sum(range);
println!("sum(r0c0:r9c0): {}", sum);
}
use std::collections::HashMap;
use crate::cell::Cell;
pub type Range<'a> = Vec<Vec<Option<&'a Cell>>>;
pub type RenderedRange = Vec<Vec<String>>;
#[derive(Clone, Debug)]
pub struct Sheet {
name: String,
cells: HashMap<(usize, usize), Cell>,
}
impl Sheet {
pub fn new_with_name(name: String) -> Self {
Sheet {
name,
cells: HashMap::new(),
}
}
pub fn cell_count(&self) -> usize {
self.cells.len()
}
pub fn write_int(&mut self, r: usize, c: usize, value: i32) {
self.cells.insert((r, c), Cell::Int(value));
}
pub fn write_float(&mut self, r: usize, c: usize, value: f32) {
self.cells.insert((r, c), Cell::Float(value));
}
pub fn write_string(&mut self, r: usize, c: usize, value: String) {
self.cells.insert((r, c), Cell::String(value));
}
pub fn range(&self, r: usize, c: usize, h: usize, w: usize) -> Range {
(r..r+h).into_iter()
.map(|i| (c..c+w).into_iter()
.map(|j| self.cells.get(&(i, j)))
.collect()
).collect()
}
pub fn render(&self, range: Range) -> RenderedRange {
range.iter()
.map(|row| row.iter()
.map(|cell| match cell {
Some(cell) => format!("{}", cell),
None => String::new(),
})
.collect()
).collect()
}
pub fn render_range(&self, r: usize, c: usize, h: usize, w: usize) -> Vec<Vec<String>> {
self.render(self.range(r, c, h, w))
}
}
use std::collections::HashMap;
use crate::sheet::Sheet;
#[derive(Clone, Debug)]
pub struct Workbook {
sheets: HashMap<String, Sheet>,
}
impl Workbook {
pub fn new() -> Self {
Workbook {
sheets: HashMap::new(),
}
}
pub fn add_sheet(&mut self, name: String) -> &mut Sheet {
let sheet = Sheet::new_with_name(name.clone());
self.sheets.insert(name.clone(), sheet);
self.sheets.get_mut(&name).unwrap()
}
pub fn get_sheet(&self, name: String) -> Option<&Sheet> {
self.sheets.get(&name)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment