add ; address support

This commit is contained in:
rick 2023-04-16 01:03:46 +02:00
parent cbd2669ef4
commit 7bbda6fa66
2 changed files with 31 additions and 15 deletions

View file

@ -1,4 +1,5 @@
use std::str::FromStr; use std::str::FromStr;
use std::num::ParseIntError;
#[derive(Debug)] #[derive(Debug)]
pub enum Address { pub enum Address {
@ -8,19 +9,12 @@ pub enum Address {
FromStartTo(usize), FromStartTo(usize),
FromToEnd(usize), // pas sur ? FromToEnd(usize), // pas sur ?
FromTo(usize, usize), FromTo(usize, usize),
FromActTo(usize),
FromActToEnd,
All, All,
Null, Null,
} }
impl Address {
pub fn is_null(&self) -> bool {
match self {
Self::Null => true,
_ => false,
}
}
}
pub fn parse_address(addr: &str) -> Address { pub fn parse_address(addr: &str) -> Address {
let mut tmp = String::from(addr); let mut tmp = String::from(addr);
tmp.retain(|c| c != ' '); tmp.retain(|c| c != ' ');
@ -28,7 +22,7 @@ pub fn parse_address(addr: &str) -> Address {
if addr.is_empty() { if addr.is_empty() {
Address::Actual Address::Actual
} else if addr.contains(',') { } else if addr.contains(',') || addr.contains(';') {
parse_multi_addr(addr) parse_multi_addr(addr)
} else if let Ok(l) = usize::from_str(addr) { } else if let Ok(l) = usize::from_str(addr) {
Address::Line(l) Address::Line(l)
@ -43,7 +37,13 @@ pub fn parse_address(addr: &str) -> Address {
fn parse_multi_addr(addr: &str) -> Address { fn parse_multi_addr(addr: &str) -> Address {
// garde con ou pas ? // garde con ou pas ?
let tmp: Vec<&str> = addr.split(",").collect(); let is_comma = addr.contains(',');
let tmp: Vec<&str> = if is_comma {
addr.split(",").collect()
} else {
addr.split(";").collect()
};
if tmp.len() != 2 { if tmp.len() != 2 {
Address::Null Address::Null
} else { } else {
@ -56,11 +56,27 @@ fn parse_multi_addr(addr: &str) -> Address {
} else { } else {
Address::Line(a) Address::Line(a)
} }
} else if let Ok(a) = end { } else if is_comma {
parse_comma(end)
} else {
parse_semicolon(end)
}
}
}
fn parse_comma(end: Result<usize, ParseIntError>) -> Address {
if let Ok(a) = end {
Address::FromStartTo(a) Address::FromStartTo(a)
} else { } else {
Address::All Address::All
} }
}
fn parse_semicolon(end: Result<usize, ParseIntError>) -> Address {
if let Ok(a) = end {
Address::FromActTo(a)
} else {
Address::FromActToEnd
} }
} }

View file

@ -52,7 +52,7 @@ fn check_address(c: char) -> bool {
} }
fn check_address_complete(c: char) -> bool { fn check_address_complete(c: char) -> bool {
check_address(c) || c == ',' check_address(c) || c == ',' || c == ';'
} }
fn execute_command(buffer: &mut Buffer, c: Command) -> bool { fn execute_command(buffer: &mut Buffer, c: Command) -> bool {