From: Ian Jackson Date: Fri, 23 Oct 2020 21:59:32 +0000 (+0100) Subject: wip new account etc., seems to be getting there now X-Git-Tag: otter-0.2.0~606 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=6e4a6b4c51f9433361e6b8cf72ad6fd272cb1ca4;p=otter.git wip new account etc., seems to be getting there now Signed-off-by: Ian Jackson --- diff --git a/src/accounts.rs b/src/accounts.rs index 06d37865..4398dd6e 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -56,7 +56,7 @@ impl AccountScope { } #[throws(InvalidScopedName)] - pub fn parse_name(s: &str) -> (AccountScope, [String; N]) { + pub fn parse_name(s: &str, names_out: &mut [String]) -> AccountScope { let mut split = s.split(':'); let scope = { @@ -79,17 +79,20 @@ impl AccountScope { } }; - let strings = ArrayVec::new(); - while let Some(s) = split.next() { - let s = percent_decode_str(s).decode_utf8() - .map_err(|_| InvalidScopedName::BadUTF8)? - .into(); - strings.try_push(s) - .map_err(|_| InvalidScopedName::TooManyComponents)?; + for eb in names_out.iter_mut().zip_longest(split) { + use EitherOrBoth::*; + match eb { + Both(out, got) => { + *out = percent_decode_str(got) + .decode_utf8() + .map_err(|_| InvalidScopedName::BadUTF8)? + .into(); + }, + Left(_out) => throw!(InvalidScopedName::TooFewComponents), + Right(_got) => throw!(InvalidScopedName::TooFewComponents), + }; } - let strings = strings.into_inner() - .map_err(|_| InvalidScopedName::TooFewComponents)?; - (scope, strings) + scope } } @@ -97,7 +100,7 @@ impl Display for AccountName { #[throws(fmt::Error)] /// Return value is parseable, and filesystem- and html-safeb fn fmt(&self, f: &mut fmt::Formatter) { - self.scope.display_name(&[ &self.subaccount ], |s| f.write(s)) + self.scope.display_name(&[ self.subaccount.as_str() ], |s| f.write_str(s)) } } @@ -120,8 +123,10 @@ impl FromStr for AccountName { #[throws(InvalidScopedName)] fn from_str(s: &str) -> Self { - let (scope, [scoped_name]) = AccountScope::parse_name(s)?; - AccountName { scope, scoped_name } + let subaccount = default(); + let names = std::slice::from_mut(&mut subaccount); + let scope = AccountScope::parse_name(s, names)?; + AccountName { scope, subaccount } } } diff --git a/src/global.rs b/src/global.rs index 122ee402..d378df30 100644 --- a/src/global.rs +++ b/src/global.rs @@ -390,7 +390,9 @@ impl FromStr for InstanceName { type Err = InvalidScopedName; #[throws(InvalidScopedName)] fn from_str(s: &str) -> Self { - let (scope, [subaccount, game]) = AccountScope::parse_name(s)?; + let names : [_;2] = Default::default(); + let scope = AccountScope::parse_name(s, &mut names)?; + let [subaccount, game] = names; InstanceName { account: AccountName { scope, subaccount }, game, diff --git a/src/imports.rs b/src/imports.rs index 40a726b2..4dc5b5cd 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -96,6 +96,7 @@ pub use hashlink::LinkedHashMap; pub use delegate::delegate; pub use itertools::Itertools; +pub use itertools::EitherOrBoth; pub use rental::RentalError; pub use rental::common::RentRef; diff --git a/src/lib.rs b/src/lib.rs index 76df4670..51c5a11c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ #![feature(proc_macro_hygiene, decl_macro)] #![feature(slice_strip)] -#![feature(min_const_generics)] #![allow(clippy::redundant_closure_call)]