diff --git a/README.md b/README.md index 3c7d782..79a8b40 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ code between applets, making for an overall smaller binary. - sync - touch - true +- truncate - umount - unlink - wc diff --git a/corebox/commands/truncate/mod.rs b/corebox/commands/truncate/mod.rs index 8758b9b..1d09801 100644 --- a/corebox/commands/truncate/mod.rs +++ b/corebox/commands/truncate/mod.rs @@ -1,9 +1,7 @@ -use std::fs::File; - use { super::Cmd, clap::{Arg, ArgAction, ArgGroup, Command, ValueHint}, - std::{error::Error, fmt, fs, num::ParseIntError, path::PathBuf}, + std::{error::Error, fmt, fs::{self, File}, num::ParseIntError, path::PathBuf}, }; #[derive(Debug, Default)] @@ -118,7 +116,7 @@ impl Cmd for Truncate { } } -#[derive(PartialEq)] +#[derive(Debug, PartialEq)] enum Operator { Equal, Add, @@ -226,7 +224,7 @@ fn parse_size(size: &str) -> Result { multiplier = Some(Multiplier::Tera); } } - ch if ch.is_digit(10) => { + ch if ch.is_ascii_digit() => { if multiplier.is_some() { return Err(ParseSizeError::InvalidChar); } else { @@ -254,3 +252,36 @@ fn parse_size(size: &str) -> Result { }), } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn parse_size_equal() { + let size = parse_size("10k").unwrap(); + assert_eq!(size.num, 10240); + assert_eq!(size.operator, Operator::Equal); + } + + #[test] + fn parse_size_add() { + let size = parse_size("+4M").unwrap(); + assert_eq!(size.num, 4194304); + assert_eq!(size.operator, Operator::Add); + } + + #[test] + fn parse_size_remove() { + let size = parse_size("-2G").unwrap(); + assert_eq!(size.num, 2147483648); + assert_eq!(size.operator, Operator::Remove); + } + + #[test] + fn parse_size_error() { + let size = parse_size("10.5"); + assert!(size.is_err()); + } +} +