diff --git a/src/api/mod.rs b/src/api/mod.rs index 15beb07..6828450 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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 diff --git a/src/bytes/mod.rs b/src/bytes/mod.rs index 037cb8a..c781337 100644 --- a/src/bytes/mod.rs +++ b/src/bytes/mod.rs @@ -1,6 +1,7 @@ use reqwest::blocking::get; use std::io::Read; +// Define filetypes #[derive(Debug, PartialEq)] pub enum FileType { PDF, @@ -16,17 +17,27 @@ pub enum FileType { Unknown, } +// Get bytes pub fn view_bytes(url: &str) -> Result<([u8; 100], FileType), Box> { - 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 @@ -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", diff --git a/src/excel/mod.rs b/src/excel/mod.rs index 74f8459..1ea4561 100644 --- a/src/excel/mod.rs +++ b/src/excel/mod.rs @@ -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) -> Result<(), Box> { // 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) { @@ -76,7 +79,9 @@ pub fn analyze_excel_formatting(content: Vec) -> Result<(), Box) -> Result<(), Box> { + // Create new workbook let cursor = Cursor::new(content); let mut workbook: Xlsx<_> = Xlsx::new(cursor)?; @@ -142,9 +147,11 @@ pub fn excel_quick_view(content: Vec) -> Result<(), Box) -> Result<(), Box> { // 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(); @@ -235,14 +242,18 @@ pub fn display_remote_basic_info(content: Vec) -> Result<(), Box, header_index: usize, ) -> Result<(), Box> { // 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) { @@ -324,8 +335,13 @@ pub fn display_remote_basic_info_specify_header_idx( Ok(()) } +// Fetch file pub fn fetch_remote_file(url: &str) -> Result, Box> { - 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)), + } }