Refactor match session name (#1582)

This commit is contained in:
Matthias Beyer 2022-07-12 11:31:29 +02:00 committed by GitHub
parent f983651759
commit cf521aaf60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -151,24 +151,23 @@ pub enum SessionNameMatch {
} }
pub(crate) fn match_session_name(prefix: &str) -> Result<SessionNameMatch, io::ErrorKind> { pub(crate) fn match_session_name(prefix: &str) -> Result<SessionNameMatch, io::ErrorKind> {
return match get_sessions() { let sessions = get_sessions()?;
Ok(sessions) => Ok({
let filtered_sessions: Vec<String> = sessions let filtered_sessions: Vec<_> = sessions.iter().filter(|s| s.starts_with(prefix)).collect();
.iter()
.filter(|s| s.starts_with(prefix)) if filtered_sessions.iter().any(|s| *s == prefix) {
.cloned() return Ok(SessionNameMatch::Exact(prefix.to_string()));
.collect(); }
if filtered_sessions.iter().any(|s| s == prefix) {
return Ok(SessionNameMatch::Exact(prefix.to_string())); Ok({
} match &filtered_sessions[..] {
match &filtered_sessions[..] { [] => SessionNameMatch::None,
[] => SessionNameMatch::None, [s] => SessionNameMatch::UniquePrefix(s.to_string()),
[s] => SessionNameMatch::UniquePrefix(s.to_string()), _ => {
_ => SessionNameMatch::AmbiguousPrefix(filtered_sessions), SessionNameMatch::AmbiguousPrefix(filtered_sessions.into_iter().cloned().collect())
} },
}), }
Err(e) => Err(e), })
};
} }
pub(crate) fn session_exists(name: &str) -> Result<bool, io::ErrorKind> { pub(crate) fn session_exists(name: &str) -> Result<bool, io::ErrorKind> {