cargo fmt + clippy
This commit is contained in:
parent
db2adaae4c
commit
22d7ddca69
3 changed files with 50 additions and 51 deletions
|
@ -1,6 +1,6 @@
|
|||
use crate::enums::Address;
|
||||
use std::io;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
|
||||
pub struct Buffer {
|
||||
|
@ -17,7 +17,7 @@ impl Buffer {
|
|||
has_changed: false,
|
||||
buffer,
|
||||
current_line: tmp,
|
||||
file_name: String::from(file_name)
|
||||
file_name: String::from(file_name),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,11 +39,9 @@ impl Buffer {
|
|||
pub fn save(&mut self) {
|
||||
if let Ok(mut file) = OpenOptions::new().write(true).open(&self.file_name) {
|
||||
let mut tmp = String::new();
|
||||
for line in &self.buffer {
|
||||
if let Ok(l) = line {
|
||||
tmp.push_str(&l);
|
||||
tmp.push('\n');
|
||||
}
|
||||
for line in self.buffer.iter().flatten() {
|
||||
tmp.push_str(line);
|
||||
tmp.push('\n');
|
||||
}
|
||||
|
||||
if let Err(e) = file.write(tmp.as_bytes()) {
|
||||
|
@ -84,13 +82,12 @@ impl Buffer {
|
|||
Address::FromActTo(a) => self.print_range(self.current_line, a, print_numbers),
|
||||
Address::FromActToEnd => self.print_range(self.current_line, last_line, print_numbers),
|
||||
Address::Null => println!("nul !"),
|
||||
_ => println!("pas implémenté"),
|
||||
}
|
||||
}
|
||||
|
||||
// affiche une ligne
|
||||
fn print_line(&mut self, nb: usize, print_numbers: bool) {
|
||||
if nb >= self.buffer.len() {
|
||||
if nb >= self.buffer.len() {
|
||||
println!("?");
|
||||
} else if let Ok(line) = &self.buffer[nb] {
|
||||
self.current_line = nb;
|
||||
|
|
20
src/enums.rs
20
src/enums.rs
|
@ -1,5 +1,5 @@
|
|||
use std::str::FromStr;
|
||||
use std::num::ParseIntError;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Address {
|
||||
|
@ -17,10 +17,10 @@ pub enum Address {
|
|||
|
||||
impl Address {
|
||||
pub fn is_multi_line(&self) -> bool {
|
||||
match self {
|
||||
Address::Null | Address::Actual | Address::Last | Address::Line(_) => false,
|
||||
_ => true,
|
||||
}
|
||||
!matches!(
|
||||
self,
|
||||
Address::Null | Address::Actual | Address::Last | Address::Line(_)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,9 +48,9 @@ fn parse_multi_addr(addr: &str) -> Address {
|
|||
// garde con ou pas ?
|
||||
let is_comma = addr.contains(',');
|
||||
let tmp: Vec<&str> = if is_comma {
|
||||
addr.split(",").collect()
|
||||
addr.split(',').collect()
|
||||
} else {
|
||||
addr.split(";").collect()
|
||||
addr.split(';').collect()
|
||||
};
|
||||
|
||||
if tmp.len() != 2 {
|
||||
|
@ -121,7 +121,7 @@ pub enum Command {
|
|||
// TODO
|
||||
CopyLines,
|
||||
Undo,
|
||||
// TODO
|
||||
// TODO
|
||||
Write(Address),
|
||||
// TODO
|
||||
Line,
|
||||
|
@ -150,10 +150,10 @@ fn parse(c: char, addr: Address) -> Result<Command, String> {
|
|||
'a' => {
|
||||
if addr.is_multi_line() {
|
||||
Err(String::from("Erreur d'adresse"))
|
||||
} else {
|
||||
} else {
|
||||
Ok(Command::Append(addr))
|
||||
}
|
||||
},
|
||||
}
|
||||
'p' => Ok(Command::Print(addr)),
|
||||
'n' => Ok(Command::Number(addr)),
|
||||
'q' => Ok(Command::Quit(false)),
|
||||
|
|
66
src/main.rs
66
src/main.rs
|
@ -1,21 +1,19 @@
|
|||
mod buffer;
|
||||
mod enums;
|
||||
pub use self::buffer::Buffer;
|
||||
pub use self::enums::{Address, Command, parse_address, parse_command};
|
||||
pub use self::enums::{parse_address, parse_command, Address, Command};
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::io::prelude::*;
|
||||
use std::io;
|
||||
use nom::bytes::complete::{tag, take_while, take_till};
|
||||
use nom::bytes::complete::take_while;
|
||||
use nom::character::complete::one_of;
|
||||
use nom::sequence::Tuple;
|
||||
use nom::branch::alt;
|
||||
use nom::error::ErrorKind;
|
||||
use nom::sequence::Tuple;
|
||||
use nom::Err::Error;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
|
||||
const FILE : &'static str = "test";
|
||||
const FILE: &str = "test";
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Exit {
|
||||
|
@ -32,7 +30,7 @@ enum Exit {
|
|||
|
||||
/*
|
||||
fn read_address(input: &str) -> Result<&str, Address> {
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
@ -41,7 +39,7 @@ fn read_address(input: &str) -> Result<&str, Address> {
|
|||
*/
|
||||
|
||||
fn check_address(c: char) -> bool {
|
||||
c.is_digit(10) || c == '$' || c == ' ' || c == '.'
|
||||
c.is_ascii_digit() || c == '$' || c == ' ' || c == '.'
|
||||
}
|
||||
|
||||
fn check_address_complete(c: char) -> bool {
|
||||
|
@ -63,28 +61,30 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> Exit {
|
|||
if buffer_input != "." {
|
||||
new_lines.push(buffer_input.clone());
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(e) => panic!("{}", e),
|
||||
}
|
||||
}
|
||||
|
||||
buffer.append(a, new_lines);
|
||||
},
|
||||
}
|
||||
Command::Print(a) => buffer.print(a, false),
|
||||
Command::Number(a) => buffer.print(a, true),
|
||||
Command::Quit(q) => if buffer.is_modified() {
|
||||
if q {
|
||||
ret = Exit::Quit;
|
||||
Command::Quit(q) => {
|
||||
if buffer.is_modified() {
|
||||
if q {
|
||||
ret = Exit::Quit;
|
||||
} else {
|
||||
ret = Exit::HasBeenModified;
|
||||
}
|
||||
} else {
|
||||
ret = Exit::HasBeenModified;
|
||||
ret = Exit::Quit;
|
||||
}
|
||||
} else {
|
||||
ret = Exit::Quit;
|
||||
},
|
||||
}
|
||||
Command::Write(_) => {
|
||||
buffer.save();
|
||||
ret = Exit::Continue;
|
||||
},
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
|
@ -97,10 +97,10 @@ fn parse_line(input: &str) -> Result<Command, String> {
|
|||
|
||||
//if let Ok((input, (a, c))) = alt(address, command).parse(input) {
|
||||
let tmp = (address, command).parse(input);
|
||||
if let Ok((input, (a, c))) = tmp {
|
||||
if let Ok((_input, (a, c))) = tmp {
|
||||
let addr = parse_address(a);
|
||||
parse_command(c, addr)
|
||||
} else if let Err(Error((_, t))) = tmp {
|
||||
} else if let Err(Error((_, _))) = tmp {
|
||||
Err(String::from("Erreur de parsing"))
|
||||
} else {
|
||||
Err(String::from("Grosse erreur"))
|
||||
|
@ -108,7 +108,7 @@ fn parse_line(input: &str) -> Result<Command, String> {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let buffer : Vec<Result<String, io::Error>> = if let Ok(f) = File::open(FILE) {
|
||||
let buffer: Vec<Result<String, io::Error>> = if let Ok(f) = File::open(FILE) {
|
||||
let buff = BufReader::new(f);
|
||||
buff.lines().collect()
|
||||
} else {
|
||||
|
@ -123,15 +123,17 @@ fn main() {
|
|||
while pred != Exit::Quit {
|
||||
let mut input = String::new();
|
||||
match io::stdin().read_line(&mut input) {
|
||||
Ok(n) => {
|
||||
Ok(_) => {
|
||||
let result_parse = parse_line(&input);
|
||||
if let Ok(c) = result_parse {
|
||||
pred = match execute_command(&mut buffer_struct, c) {
|
||||
Exit::HasBeenModified => if pred == Exit::HasBeenModified {
|
||||
Exit::Quit
|
||||
} else {
|
||||
Exit::HasBeenModified
|
||||
},
|
||||
Exit::HasBeenModified => {
|
||||
if pred == Exit::HasBeenModified {
|
||||
Exit::Quit
|
||||
} else {
|
||||
Exit::HasBeenModified
|
||||
}
|
||||
}
|
||||
exit => exit,
|
||||
}
|
||||
} else if let Err(e) = result_parse {
|
||||
|
@ -139,7 +141,7 @@ fn main() {
|
|||
println!("{}", e);
|
||||
pred = Exit::Continue;
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(e) => panic!("{}", e),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue