-
Notifications
You must be signed in to change notification settings - Fork 1
a quick sketch of some ideas #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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)), | ||
| } | ||
| } | ||
| } |
| 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)] | ||
| #![deny(missing_docs, missing_debug_implementations, clippy::all)] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
|
|
||
| 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, | ||
|
|
@@ -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"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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!)