Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ edition = "2018"
[dependencies]
qrcode = "0.11"
base64 = "0.13.0"
structopt = "0.3.21"

[dev-dependencies]
rand = "0.8.3"
assert_cmd = "1.0.3"
assert_fs = "1.0.1"
predicates = "1.0.7"
predicates = "1.0.7"
41 changes: 41 additions & 0 deletions src/chunk_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::io::{BufRead, Read};

use std::io;

pub struct ChunkIterator<T>
where
T: Read,
{
reader: T,
chunk_size: u64,
}

impl<T> ChunkIterator<T>
where
T: Read,
{
pub fn new(reader: T, chunk_size: u64) -> Self {
Self { reader, chunk_size }
}
}

impl<T> Iterator for ChunkIterator<T>
where
T: BufRead,
{
type Item = io::Result<Vec<u8>>;

fn next(&mut self) -> Option<Self::Item> {
let mut buf = Vec::default();
match self
.reader
.by_ref()
.take(self.chunk_size)
.read_to_end(&mut buf)
{
Ok(0) => None,
Ok(_n) => Some(Ok(buf)),
Err(e) => Some(Err(e)),
}
}
}
57 changes: 32 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
use std::env;
use std::fs;
#![feature(seek_stream_len)]
#![warn(clippy::pedantic)]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this enables pedantic lints when you run cargo clippy. It's not a bad way to learn Rust idioms (but as the name suggests, it can be quite pedantic!)

#![deny(missing_docs, missing_debug_implementations, clippy::all)]
Copy link
Copy Markdown
Contributor Author

@danieleades danieleades Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are more compiler flags. You may not want these, but there are loads more of this too if you want to enforce certain idioms in your codebase. these are some of my favourites

  • deny(missing_docs) - this will prevent your code from compiling if you have failed to document anything which is in your public API
  • deny(missing_debug_implementations) - prevent code from compiling if any struct is missing a Debug implementation (this prevents errors involving this type being printed nicely to console)
  • deny(clippy::all) - prevent compiling for any standard clippy lint. all the lints in this category are very sensible, so this is a good one to enforce


use std::io::Read;
use std::io::Write;
use std::io::{BufRead, BufReader};
use std::{fs, path::PathBuf};
// use std::io::{BufRead, BufReader};
use std::io::{Seek, SeekFrom};
use std::path::Path;
use std::process;

extern crate base64;

mod chunk_iterator;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct Args {
#[structopt(short, long = "input")]
pub input_file: PathBuf,
#[structopt(short, long = "output")]
pub output_directory: PathBuf,
}

fn main() {
let args = Args::from_args();
// Check arguments for file to open
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
let binary_name = &args[0];
println!("Usage: {} FILE_TO_SEND OUTPUT_FOLDER", binary_name);
process::exit(1);
}
let input_filename = &args[1];
let output_folder = &args[2];
let input_filename = args.input_file;
let mut file = match fs::File::open(input_filename) {
Ok(f) => f,
Err(err) => panic!("File error: {}", err),
};

// Ensure output folder exists
fs::create_dir_all(output_folder).expect("Could not create/check output folder");
let output_path = Path::new(output_folder);
fs::create_dir_all(&args.output_directory).expect("Could not create/check output folder");
let output_path = args.output_directory.join("input_b64.txt");
// Create a base64 version of our file
let mut base64_file = match fs::File::create(output_path.join("input_b64.txt")) {
Ok(f) => f,
Expand Down Expand Up @@ -63,18 +71,17 @@ fn main() {
.sync_all()
.expect("Error syncing base64 file to disks");

// let base64_filesize_bytes = base64_file
// .stream_len()
// .expect("Error checking base64 filesize");
let base64_filesize_bytes = base64_file
.stream_len()
.expect("Error checking base64 filesize");

// let chunk_reader;
// let chunk_read;
// {
// chunk_reader = BufReader::with_capacity(1024, base64_file);
let mut chunk_reader;
let chunk_read;
{
chunk_reader = BufReader::with_capacity(1024, base64_file);

// chunk_read = chunk_reader
// .fill_buf()
// .expect("Error reading chunk off base64 file");
// }
// println!("Read {:?} bytes: {:?}", chunk_read, chunk_reader);
chunk_read = chunk_reader
.fill_buf()
.expect("Error reading chunk off base64 file");
}
}