Skip to content

Commit

Permalink
small refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
CHRISCARLON committed Oct 12, 2024
1 parent a78a645 commit 3420661
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use reqwest::blocking::get;
use serde_json::Value;
use std::error::Error;

// Define nesting levels
#[derive(Debug)]
enum NestingLevel {
Flat, // No nesting, or a very shallow structure
Expand Down
26 changes: 19 additions & 7 deletions src/bytes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use reqwest::blocking::get;
use std::io::Read;

// Define filetypes
#[derive(Debug, PartialEq)]
pub enum FileType {
PDF,
Expand All @@ -16,17 +17,27 @@ pub enum FileType {
Unknown,
}

// Get bytes
pub fn view_bytes(url: &str) -> Result<([u8; 100], FileType), Box<dyn std::error::Error>> {
let response = get(url)?;
let mut buffer = [0u8; 100];
let bytes_read = response.take(100).read(&mut buffer)?;
if bytes_read < 100 {
buffer[bytes_read..].fill(0);
match get(url) {
Ok(response) => {
let mut buffer = [0u8; 100];
match response.take(100).read(&mut buffer) {
Ok(bytes_read) => {
if bytes_read < 100 {
buffer[bytes_read..].fill(0);
}
let file_type = identify_file_type(&buffer);
Ok((buffer, file_type))
}
Err(e) => Err(Box::new(e)),
}
}
Err(e) => Err(Box::new(e)),
}
let file_type = identify_file_type(&buffer);
Ok((buffer, file_type))
}

// Filetype logic
fn identify_file_type(bytes: &[u8]) -> FileType {
match bytes {
// PDF magic number
Expand Down Expand Up @@ -59,6 +70,7 @@ fn identify_file_type(bytes: &[u8]) -> FileType {
}
}

// Fields to match on
pub fn get_file_type_string(file_type: &FileType) -> &'static str {
match file_type {
FileType::PDF => "PDF",
Expand Down
28 changes: 22 additions & 6 deletions src/excel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use comfy_table::{Attribute, Cell, Color, Table};
use reqwest::blocking::get;
use std::io::Cursor;

// Check formatting
pub fn analyze_excel_formatting(content: Vec<u8>) -> Result<(), Box<dyn std::error::Error>> {
// Create new workbook
let mut workbook: Xlsx<_> = Xlsx::new(Cursor::new(content))?;
let cursor = Cursor::new(content);
let mut workbook: Xlsx<_> = Xlsx::new(cursor)?;

// Loop through sheets
for (index, sheet_name) in workbook.sheet_names().to_vec().iter().enumerate() {
println!("{}", format!("Analysing sheet: {}", sheet_name).cyan());
if let Some(Ok(range)) = workbook.worksheet_range_at(index) {
Expand Down Expand Up @@ -76,7 +79,9 @@ pub fn analyze_excel_formatting(content: Vec<u8>) -> Result<(), Box<dyn std::err
Ok(())
}

// Quick view
pub fn excel_quick_view(content: Vec<u8>) -> Result<(), Box<dyn std::error::Error>> {
// Create new workbook
let cursor = Cursor::new(content);
let mut workbook: Xlsx<_> = Xlsx::new(cursor)?;

Expand Down Expand Up @@ -142,9 +147,11 @@ pub fn excel_quick_view(content: Vec<u8>) -> Result<(), Box<dyn std::error::Erro
Ok(())
}

// Display basic info
pub fn display_remote_basic_info(content: Vec<u8>) -> Result<(), Box<dyn std::error::Error>> {
// Create new workbook
let mut workbook: Xlsx<_> = Xlsx::new(Cursor::new(content))?;
let cursor = Cursor::new(content);
let mut workbook: Xlsx<_> = Xlsx::new(cursor)?;

// Add sheet names to list
let sheet_names = workbook.sheet_names().to_vec();
Expand Down Expand Up @@ -235,14 +242,18 @@ pub fn display_remote_basic_info(content: Vec<u8>) -> Result<(), Box<dyn std::er
Ok(())
}

// Display basic info + add starting header
pub fn display_remote_basic_info_specify_header_idx(
content: Vec<u8>,
header_index: usize,
) -> Result<(), Box<dyn std::error::Error>> {
// Create new workbook
let mut workbook: Xlsx<_> = Xlsx::new(Cursor::new(content))?;
let cursor = Cursor::new(content);
let mut workbook: Xlsx<_> = Xlsx::new(cursor)?;

// Add sheet names to list
let sheet_names = workbook.sheet_names().to_vec();

// Loop through sheet names
for (index, sheet_name) in sheet_names.iter().enumerate() {
if let Some(Ok(range)) = workbook.worksheet_range_at(index) {
Expand Down Expand Up @@ -324,8 +335,13 @@ pub fn display_remote_basic_info_specify_header_idx(
Ok(())
}

// Fetch file
pub fn fetch_remote_file(url: &str) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let response = get(url)?;
let content = response.bytes()?.to_vec();
Ok(content)
match get(url) {
Ok(response) => match response.bytes() {
Ok(bytes) => Ok(bytes.to_vec()),
Err(e) => Err(Box::new(e)),
},
Err(e) => Err(Box::new(e)),
}
}

0 comments on commit 3420661

Please sign in to comment.