Adding some documentation.
This commit is contained in:
parent
89da9b45a9
commit
85309f05b5
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# mkz_aoc
|
||||||
|
|
||||||
|
## Modules
|
||||||
|
|
||||||
|
- file: Open files & prepare inputs
|
||||||
|
- parse: Parse contents (moslty from Strings to Vec/integers
|
||||||
|
)
|
23
src/file.rs
23
src/file.rs
@ -1,7 +1,18 @@
|
|||||||
|
//! This module proposes reading file helper functions.
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use crate::parse::string_to_numbers;
|
use crate::parse::string_to_numbers;
|
||||||
|
|
||||||
|
/// Read a file & return its contents in a Vec<String> structure
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use mkz_aoc::file;
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let lines = file ::read_to_numbers("input.txt");
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn read_to_lines(_filename: &str) -> Result<Vec<String>, String> {
|
pub fn read_to_lines(_filename: &str) -> Result<Vec<String>, String> {
|
||||||
let _contents = fs::read_to_string(_filename);
|
let _contents = fs::read_to_string(_filename);
|
||||||
if let Err(v) = _contents {
|
if let Err(v) = _contents {
|
||||||
@ -13,6 +24,17 @@ pub fn read_to_lines(_filename: &str) -> Result<Vec<String>, String> {
|
|||||||
Ok(lines)
|
Ok(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read a file & return its contents in a Vec<i128> structure
|
||||||
|
pub fn read_to_numbers(_filename: &str) -> Result<Vec<i128>, String> {
|
||||||
|
let _lines = read_to_lines(_filename);
|
||||||
|
if let Err(v) = _lines {
|
||||||
|
return Err(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(_lines.unwrap().iter().map(|x| x.parse::<i128>().unwrap()).collect::<Vec<i128>>())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read a file & return its first line in a String
|
||||||
pub fn read_to_string(_filename: &str) -> Result<String, String> {
|
pub fn read_to_string(_filename: &str) -> Result<String, String> {
|
||||||
let _lines = read_to_lines(_filename);
|
let _lines = read_to_lines(_filename);
|
||||||
|
|
||||||
@ -26,6 +48,7 @@ pub fn read_to_string(_filename: &str) -> Result<String, String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read a file for a matrix & return it in a Vec<Vec<i128>> data structure.
|
||||||
pub fn read_to_matrix(_filename: &str) -> Result<Vec<Vec<i128>>, String> {
|
pub fn read_to_matrix(_filename: &str) -> Result<Vec<Vec<i128>>, String> {
|
||||||
let _lines = read_to_lines(_filename);
|
let _lines = read_to_lines(_filename);
|
||||||
|
|
||||||
|
@ -1,2 +1,5 @@
|
|||||||
|
#![deny(missing_docs)]
|
||||||
|
//! This create implements some useful helper functions to solve Advent of Code problems.
|
||||||
|
|
||||||
pub mod file;
|
pub mod file;
|
||||||
pub mod parse;
|
pub mod parse;
|
@ -1,7 +1,11 @@
|
|||||||
|
//! Parsing helper functions
|
||||||
|
|
||||||
|
/// Transform a String with only digits to a number array.
|
||||||
pub fn string_to_usize_vec(input: String) -> Vec<usize> {
|
pub fn string_to_usize_vec(input: String) -> Vec<usize> {
|
||||||
input.chars().map(|x| x.to_digit(10).unwrap() as usize).collect()
|
input.chars().map(|x| x.to_digit(10).unwrap() as usize).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Transform a string to a i128 array.
|
||||||
pub fn string_to_numbers(input: String) -> Vec<i128> {
|
pub fn string_to_numbers(input: String) -> Vec<i128> {
|
||||||
input
|
input
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
|
Loading…
Reference in New Issue
Block a user