clippy: use strip_prefix instead of manual stripping
it's less error-prone (off-by-one!) Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
parent
87152fbac6
commit
365915da9a
@ -651,8 +651,7 @@ impl AclTree {
|
|||||||
if !ROLE_NAMES.contains_key(role) {
|
if !ROLE_NAMES.contains_key(role) {
|
||||||
bail!("unknown role '{}'", role);
|
bail!("unknown role '{}'", role);
|
||||||
}
|
}
|
||||||
if user_or_group.starts_with('@') {
|
if let Some(group) = user_or_group.strip_prefix('@') {
|
||||||
let group = &user_or_group[1..];
|
|
||||||
node.insert_group_role(group.to_string(), role.to_string(), propagate);
|
node.insert_group_role(group.to_string(), role.to_string(), propagate);
|
||||||
} else {
|
} else {
|
||||||
node.insert_user_role(user_or_group.parse()?, role.to_string(), propagate);
|
node.insert_user_role(user_or_group.parse()?, role.to_string(), propagate);
|
||||||
|
@ -74,9 +74,9 @@ impl <R: BufRead> Lexer<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn split_line(line: &str) -> VecDeque<(Token, String)> {
|
fn split_line(line: &str) -> VecDeque<(Token, String)> {
|
||||||
if line.starts_with("#") {
|
if let Some(comment) = line.strip_prefix('#') {
|
||||||
let mut res = VecDeque::new();
|
let mut res = VecDeque::new();
|
||||||
res.push_back((Token::Comment, line[1..].trim().to_string()));
|
res.push_back((Token::Comment, comment.trim().to_string()));
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
let mut list: VecDeque<(Token, String)> = line.split_ascii_whitespace().map(|text| {
|
let mut list: VecDeque<(Token, String)> = line.split_ascii_whitespace().map(|text| {
|
||||||
|
@ -1380,14 +1380,14 @@ impl std::str::FromStr for TfaResponse {
|
|||||||
type Err = Error;
|
type Err = Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Error> {
|
fn from_str(s: &str) -> Result<Self, Error> {
|
||||||
Ok(if s.starts_with("totp:") {
|
Ok(if let Some(totp) = s.strip_prefix("totp:") {
|
||||||
TfaResponse::Totp(s[5..].to_string())
|
TfaResponse::Totp(totp.to_string())
|
||||||
} else if s.starts_with("u2f:") {
|
} else if let Some(u2f) = s.strip_prefix("u2f:") {
|
||||||
TfaResponse::U2f(serde_json::from_str(&s[4..])?)
|
TfaResponse::U2f(serde_json::from_str(u2f)?)
|
||||||
} else if s.starts_with("webauthn:") {
|
} else if let Some(webauthn) = s.strip_prefix("webauthn:") {
|
||||||
TfaResponse::Webauthn(serde_json::from_str(&s[9..])?)
|
TfaResponse::Webauthn(serde_json::from_str(webauthn)?)
|
||||||
} else if s.starts_with("recovery:") {
|
} else if let Some(recovery) = s.strip_prefix("recovery:") {
|
||||||
TfaResponse::Recovery(s[9..].to_string())
|
TfaResponse::Recovery(recovery.to_string())
|
||||||
} else {
|
} else {
|
||||||
bail!("invalid tfa response");
|
bail!("invalid tfa response");
|
||||||
})
|
})
|
||||||
|
@ -127,13 +127,13 @@ pub async fn send_command<P>(
|
|||||||
if rx.read_line(&mut data).await? == 0 {
|
if rx.read_line(&mut data).await? == 0 {
|
||||||
bail!("no response");
|
bail!("no response");
|
||||||
}
|
}
|
||||||
if data.starts_with("OK: ") {
|
if let Some(res) = data.strip_prefix("OK: ") {
|
||||||
match data[4..].parse::<Value>() {
|
match res.parse::<Value>() {
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok(v),
|
||||||
Err(err) => bail!("unable to parse json response - {}", err),
|
Err(err) => bail!("unable to parse json response - {}", err),
|
||||||
}
|
}
|
||||||
} else if data.starts_with("ERROR: ") {
|
} else if let Some(err) = data.strip_prefix("ERROR: ") {
|
||||||
bail!("{}", &data[7..]);
|
bail!("{}", err);
|
||||||
} else {
|
} else {
|
||||||
bail!("unable to parse response: {}", data);
|
bail!("unable to parse response: {}", data);
|
||||||
}
|
}
|
||||||
|
@ -68,8 +68,8 @@ impl std::str::FromStr for ApiTicket {
|
|||||||
type Err = Error;
|
type Err = Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Error> {
|
fn from_str(s: &str) -> Result<Self, Error> {
|
||||||
if s.starts_with("!tfa!") {
|
if let Some(tfa_ticket) = s.strip_prefix("!tfa!") {
|
||||||
Ok(ApiTicket::Partial(serde_json::from_str(&s[5..])?))
|
Ok(ApiTicket::Partial(serde_json::from_str(tfa_ticket)?))
|
||||||
} else {
|
} else {
|
||||||
Ok(ApiTicket::Full(s.parse()?))
|
Ok(ApiTicket::Full(s.parse()?))
|
||||||
}
|
}
|
||||||
|
@ -267,11 +267,11 @@ impl TaskState {
|
|||||||
Ok(TaskState::Unknown { endtime })
|
Ok(TaskState::Unknown { endtime })
|
||||||
} else if s == "OK" {
|
} else if s == "OK" {
|
||||||
Ok(TaskState::OK { endtime })
|
Ok(TaskState::OK { endtime })
|
||||||
} else if s.starts_with("WARNINGS: ") {
|
} else if let Some(warnings) = s.strip_prefix("WARNINGS: ") {
|
||||||
let count: u64 = s[10..].parse()?;
|
let count: u64 = warnings.parse()?;
|
||||||
Ok(TaskState::Warning{ count, endtime })
|
Ok(TaskState::Warning{ count, endtime })
|
||||||
} else if s.len() > 0 {
|
} else if s.len() > 0 {
|
||||||
let message = if s.starts_with("ERROR: ") { &s[7..] } else { s }.to_string();
|
let message = if let Some(err) = s.strip_prefix("ERROR: ") { err } else { s }.to_string();
|
||||||
Ok(TaskState::Error{ message, endtime })
|
Ok(TaskState::Error{ message, endtime })
|
||||||
} else {
|
} else {
|
||||||
bail!("unable to parse Task Status '{}'", s);
|
bail!("unable to parse Task Status '{}'", s);
|
||||||
|
@ -58,13 +58,12 @@ fn parse_drive_status(i: &str) -> IResult<&str, DriveStatus> {
|
|||||||
|
|
||||||
let mut loaded_slot = None;
|
let mut loaded_slot = None;
|
||||||
|
|
||||||
if i.starts_with("Empty") {
|
if let Some(empty) = i.strip_suffix("Empty") {
|
||||||
return Ok((&i[5..], DriveStatus { loaded_slot, status: ElementStatus::Empty }));
|
return Ok((empty, DriveStatus { loaded_slot, status: ElementStatus::Empty }));
|
||||||
}
|
}
|
||||||
let (mut i, _) = tag("Full (")(i)?;
|
let (mut i, _) = tag("Full (")(i)?;
|
||||||
|
|
||||||
if i.starts_with("Storage Element ") {
|
if let Some(n) = i.strip_prefix("Storage Element ") {
|
||||||
let n = &i[16..];
|
|
||||||
let (n, id) = parse_u64(n)?;
|
let (n, id) = parse_u64(n)?;
|
||||||
loaded_slot = Some(id);
|
loaded_slot = Some(id);
|
||||||
let (n, _) = tag(" Loaded")(n)?;
|
let (n, _) = tag(" Loaded")(n)?;
|
||||||
@ -76,8 +75,7 @@ fn parse_drive_status(i: &str) -> IResult<&str, DriveStatus> {
|
|||||||
|
|
||||||
let (i, _) = tag(")")(i)?;
|
let (i, _) = tag(")")(i)?;
|
||||||
|
|
||||||
if i.starts_with(":VolumeTag = ") {
|
if let Some(i) = i.strip_prefix(":VolumeTag = ") {
|
||||||
let i = &i[13..];
|
|
||||||
let (i, tag) = take_while(|c| !(c == ' ' || c == ':' || c == '\n'))(i)?;
|
let (i, tag) = take_while(|c| !(c == ' ' || c == ':' || c == '\n'))(i)?;
|
||||||
let (i, _) = take_while(|c| c != '\n')(i)?; // skip to eol
|
let (i, _) = take_while(|c| c != '\n')(i)?; // skip to eol
|
||||||
return Ok((i, DriveStatus { loaded_slot, status: ElementStatus::VolumeTag(tag.to_string()) }));
|
return Ok((i, DriveStatus { loaded_slot, status: ElementStatus::VolumeTag(tag.to_string()) }));
|
||||||
@ -89,14 +87,11 @@ fn parse_drive_status(i: &str) -> IResult<&str, DriveStatus> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_slot_status(i: &str) -> IResult<&str, ElementStatus> {
|
fn parse_slot_status(i: &str) -> IResult<&str, ElementStatus> {
|
||||||
if i.starts_with("Empty") {
|
if let Some(empty) = i.strip_prefix("Empty") {
|
||||||
return Ok((&i[5..], ElementStatus::Empty));
|
return Ok((empty, ElementStatus::Empty));
|
||||||
}
|
}
|
||||||
if i.starts_with("Full ") {
|
if let Some(n) = i.strip_prefix("Full ") {
|
||||||
let mut n = &i[5..];
|
if let Some(n) = n.strip_prefix(":VolumeTag=") {
|
||||||
|
|
||||||
if n.starts_with(":VolumeTag=") {
|
|
||||||
n = &n[11..];
|
|
||||||
let (n, tag) = take_while(|c| !(c == ' ' || c == ':' || c == '\n'))(n)?;
|
let (n, tag) = take_while(|c| !(c == ' ' || c == ':' || c == '\n'))(n)?;
|
||||||
let (n, _) = take_while(|c| c != '\n')(n)?; // skip to eol
|
let (n, _) = take_while(|c| c != '\n')(n)?; // skip to eol
|
||||||
return Ok((n, ElementStatus::VolumeTag(tag.to_string())));
|
return Ok((n, ElementStatus::VolumeTag(tag.to_string())));
|
||||||
|
@ -168,8 +168,8 @@ impl VirtualTapeHandle {
|
|||||||
if path.is_file() && path.extension() == Some(std::ffi::OsStr::new("json")) {
|
if path.is_file() && path.extension() == Some(std::ffi::OsStr::new("json")) {
|
||||||
if let Some(name) = path.file_stem() {
|
if let Some(name) = path.file_stem() {
|
||||||
if let Some(name) = name.to_str() {
|
if let Some(name) = name.to_str() {
|
||||||
if name.starts_with("tape-") {
|
if let Some(label) = name.strip_prefix("tape-") {
|
||||||
list.push(name[5..].to_string());
|
list.push(label.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -996,8 +996,8 @@ pub fn get_fs_uuid(disk: &Disk) -> Result<String, Error> {
|
|||||||
let output = crate::tools::run_command(command, None)?;
|
let output = crate::tools::run_command(command, None)?;
|
||||||
|
|
||||||
for line in output.lines() {
|
for line in output.lines() {
|
||||||
if line.starts_with("UUID=") {
|
if let Some(uuid) = line.strip_prefix("UUID=") {
|
||||||
return Ok(line[5..].to_string());
|
return Ok(uuid.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ pub struct ZFSPoolInfo {
|
|||||||
|
|
||||||
|
|
||||||
fn parse_optional_u64(i: &str) -> IResult<&str, Option<u64>> {
|
fn parse_optional_u64(i: &str) -> IResult<&str, Option<u64>> {
|
||||||
if i.starts_with('-') {
|
if let Some(rest) = i.strip_prefix('-') {
|
||||||
Ok((&i[1..], None))
|
Ok((rest, None))
|
||||||
} else {
|
} else {
|
||||||
let (i, value) = map_res(recognize(digit1), str::parse)(i)?;
|
let (i, value) = map_res(recognize(digit1), str::parse)(i)?;
|
||||||
Ok((i, Some(value)))
|
Ok((i, Some(value)))
|
||||||
@ -40,8 +40,8 @@ fn parse_optional_u64(i: &str) -> IResult<&str, Option<u64>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_optional_f64(i: &str) -> IResult<&str, Option<f64>> {
|
fn parse_optional_f64(i: &str) -> IResult<&str, Option<f64>> {
|
||||||
if i.starts_with('-') {
|
if let Some(rest) = i.strip_prefix('-') {
|
||||||
Ok((&i[1..], None))
|
Ok((rest, None))
|
||||||
} else {
|
} else {
|
||||||
let (i, value) = nom::number::complete::double(i)?;
|
let (i, value) = nom::number::complete::double(i)?;
|
||||||
Ok((i, Some(value)))
|
Ok((i, Some(value)))
|
||||||
|
@ -151,10 +151,9 @@ fn parse_date_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, DateTimeVa
|
|||||||
return Ok((i, DateTimeValue::Range(value, end)))
|
return Ok((i, DateTimeValue::Range(value, end)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if i.starts_with("/") {
|
if let Some(time) = i.strip_prefix('/') {
|
||||||
let i = &i[1..];
|
let (time, repeat) = parse_time_comp(max)(time)?;
|
||||||
let (i, repeat) = parse_time_comp(max)(i)?;
|
Ok((time, DateTimeValue::Repeated(value, repeat)))
|
||||||
Ok((i, DateTimeValue::Repeated(value, repeat)))
|
|
||||||
} else {
|
} else {
|
||||||
Ok((i, DateTimeValue::Single(value)))
|
Ok((i, DateTimeValue::Single(value)))
|
||||||
}
|
}
|
||||||
@ -163,15 +162,14 @@ fn parse_date_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, DateTimeVa
|
|||||||
|
|
||||||
fn parse_date_time_comp_list(start: u32, max: usize) -> impl Fn(&str) -> IResult<&str, Vec<DateTimeValue>> {
|
fn parse_date_time_comp_list(start: u32, max: usize) -> impl Fn(&str) -> IResult<&str, Vec<DateTimeValue>> {
|
||||||
move |i: &str| {
|
move |i: &str| {
|
||||||
if i.starts_with("*") {
|
if let Some(rest) = i.strip_prefix('*') {
|
||||||
let i = &i[1..];
|
if let Some(time) = rest.strip_prefix('/') {
|
||||||
if i.starts_with("/") {
|
let (n, repeat) = parse_time_comp(max)(time)?;
|
||||||
let (n, repeat) = parse_time_comp(max)(&i[1..])?;
|
|
||||||
if repeat > 0 {
|
if repeat > 0 {
|
||||||
return Ok((n, vec![DateTimeValue::Repeated(start, repeat)]));
|
return Ok((n, vec![DateTimeValue::Repeated(start, repeat)]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok((i, Vec::new()));
|
return Ok((rest, Vec::new()));
|
||||||
}
|
}
|
||||||
|
|
||||||
separated_nonempty_list(tag(","), parse_date_time_comp(max))(i)
|
separated_nonempty_list(tag(","), parse_date_time_comp(max))(i)
|
||||||
|
Loading…
Reference in New Issue
Block a user