Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
CHRISCARLON committed Sep 18, 2024
1 parent 1915494 commit b0e7ffd
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 15 deletions.
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
set -e

# Clean the project
cargo clean
# cargo clean

# Build the project in release mode
cargo build --release
Expand All @@ -15,4 +15,4 @@ sudo cp target/release/nebb /usr/local/bin/
# Verify the installation
which nebb

echo "nebby has been rebuilt and installed successfully!"
echo "nebby has been rebuilt and installed successfully!"
12 changes: 8 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
VERSION := $(shell cargo pkgid | cut -d\# -f2 | cut -d: -f2)

.PHONY: release

release:
@echo "Creating release $(VERSION)..."
@if [ -z "$(VERSION)" ]; then echo "Error: VERSION is not set"; exit 1; fi
Expand All @@ -13,9 +12,14 @@ release:
git tag -a v$(VERSION) -m "Release version $(VERSION)"
git push origin v$(VERSION)

# Create GitHub release
# Prompt for release notes
@echo "Please enter release notes (press Enter twice when finished):"
@NOTES=$$(cat <<EOF

EOF
); \
gh release create v$(VERSION) \
--title "Nebb v$(VERSION)" \
--notes "Release notes for version $(VERSION)"
--title "Nebby v$(VERSION)" \
--notes "$$NOTES"

@echo "Release v$(VERSION) created and published on GitHub"
File renamed without changes.
16 changes: 16 additions & 0 deletions src/bytes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// src/file_bytes.rs
use reqwest::blocking::get;
use std::io::Read;

pub fn view_bytes(url: &str) -> Result<[u8; 100], 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 {
// If less than 100 bytes were read, fill the rest with zeros
buffer[bytes_read..].fill(0);
}

Ok(buffer)
}
File renamed without changes.
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// src/lib.rs
pub mod excel_file_functions;
pub mod reqwest_functions;
pub mod api;
pub mod bytes;
pub mod excel;
37 changes: 32 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// src/main.rs
mod excel_file_functions;
mod reqwest_functions;
mod api;
mod bytes;
mod excel;
use api::simple_api_get_reqwest;
use bytes::view_bytes;
use clap::{Parser, Subcommand};
use colored::Colorize;
use excel_file_functions::{
use excel::{
analyze_excel_formatting, display_remote_basic_info,
display_remote_basic_info_specify_header_idx, excel_quick_view, fetch_remote_file,
};
use reqwest_functions::simple_api_get_reqwest;

#[derive(Parser, Debug)]
#[command(author = "Christopher Carlon", version = "0.1.1", about = "Nebby - quickly review basic information about remote xlsx files and API GET requests", long_about = None)]
Expand Down Expand Up @@ -46,6 +48,11 @@ enum Commands {
/// API Endpoint
url: String,
},
/// Check bytes of any file
Nibble {
/// Url of the file
url: String,
},
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -56,6 +63,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Commands::QuickView { url } => process_excel(url, "quick view"),
Commands::BasicIdx { url, header_index } => process_excel_with_header(url, *header_index),
Commands::BasicJson { url } => process_json(url),
Commands::Nibble { url } => process_view_bytes(url),
}
}

Expand Down Expand Up @@ -84,12 +92,31 @@ fn process_excel_with_header(
validate_url(url)?;
println!(
"{}",
format!("Processing Excel with header index {}...", header_index).blue()
format!(
"Processing Excel with header set at INDEX {}...",
header_index
)
.blue()
);
let bytes = fetch_remote_file(url)?;
display_remote_basic_info_specify_header_idx(bytes, header_index)
}

fn process_view_bytes(url: &str) -> Result<(), Box<dyn std::error::Error>> {
validate_url(url)?;
println!("{}", "Viewing first 100 bytes...".blue());
let bytes = view_bytes(url)?;
println!("First 100 bytes:");
for (i, byte) in bytes.iter().enumerate() {
print!("{:02X} ", byte);
if (i + 1) % 16 == 0 {
println!();
}
}
println!();
Ok(())
}

fn validate_url(url: &str) -> Result<(), Box<dyn std::error::Error>> {
if url.is_empty() {
return Err("Error: URL cannot be empty".into());
Expand Down
2 changes: 1 addition & 1 deletion tests/reqwest_tests.rs → tests/api_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nebby::reqwest_functions::simple_api_get_reqwest;
use nebby::api::simple_api_get_reqwest;

#[test]
fn test_simple_api_get_reqwest() {
Expand Down
10 changes: 10 additions & 0 deletions tests/bytes_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use nebby::bytes::view_bytes;

#[test]
fn test_view_bytes() {
let url = "https://api.carbonintensity.org.uk/regional/regionid/1";

let result = view_bytes(url);

assert!(result.is_ok(), "Failed to read in bytes")
}
2 changes: 1 addition & 1 deletion tests/excel_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nebby::excel_file_functions::{
use nebby::excel::{
analyze_excel_formatting, display_remote_basic_info,
display_remote_basic_info_specify_header_idx, excel_quick_view, fetch_remote_file,
};
Expand Down

0 comments on commit b0e7ffd

Please sign in to comment.