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