Adding some documentation.

This commit is contained in:
Patrick MARIE 2021-01-18 14:03:44 +01:00
parent 89da9b45a9
commit 85309f05b5
4 changed files with 37 additions and 0 deletions

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# mkz_aoc
## Modules
- file: Open files & prepare inputs
- parse: Parse contents (moslty from Strings to Vec/integers
)

View File

@ -1,7 +1,18 @@
//! This module proposes reading file helper functions.
use std::fs;
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> {
let _contents = fs::read_to_string(_filename);
if let Err(v) = _contents {
@ -13,6 +24,17 @@ pub fn read_to_lines(_filename: &str) -> Result<Vec<String>, String> {
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> {
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> {
let _lines = read_to_lines(_filename);

View File

@ -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 parse;

View File

@ -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> {
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> {
input
.split_whitespace()