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::num::ParseIntError;
#[derive(Debug)]
pub enum Address {
@ -8,19 +9,12 @@ pub enum Address {
FromStartTo(usize),
FromToEnd(usize), // pas sur ?
FromTo(usize, usize),
FromActTo(usize),
FromActToEnd,
All,
Null,
}
impl Address {
pub fn is_null(&self) -> bool {
match self {
Self::Null => true,
_ => false,
}
}
}
pub fn parse_address(addr: &str) -> Address {
let mut tmp = String::from(addr);
tmp.retain(|c| c != ' ');
@ -28,7 +22,7 @@ pub fn parse_address(addr: &str) -> Address {
if addr.is_empty() {
Address::Actual
} else if addr.contains(',') {
} else if addr.contains(',') || addr.contains(';') {
parse_multi_addr(addr)
} else if let Ok(l) = usize::from_str(addr) {
Address::Line(l)
@ -43,7 +37,13 @@ pub fn parse_address(addr: &str) -> Address {
fn parse_multi_addr(addr: &str) -> Address {
// 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 {
Address::Null
} else {
@ -56,14 +56,30 @@ fn parse_multi_addr(addr: &str) -> Address {
} else {
Address::Line(a)
}
} else if let Ok(a) = end {
Address::FromStartTo(a)
} else if is_comma {
parse_comma(end)
} else {
Address::All
parse_semicolon(end)
}
}
}
fn parse_comma(end: Result<usize, ParseIntError>) -> Address {
if let Ok(a) = end {
Address::FromStartTo(a)
} else {
Address::All
}
}
fn parse_semicolon(end: Result<usize, ParseIntError>) -> Address {
if let Ok(a) = end {
Address::FromActTo(a)
} else {
Address::FromActToEnd
}
}
#[derive(Debug)]
pub enum Command {
Append(Address),

View file

@ -52,7 +52,7 @@ fn check_address(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 {