chiark / gitweb /
wip new account etc., seems to be getting there now
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 23 Oct 2020 21:59:32 +0000 (22:59 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 23 Oct 2020 21:59:32 +0000 (22:59 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/accounts.rs
src/global.rs
src/imports.rs
src/lib.rs

index 06d3786560f03c2c9d032d08132a82e42180aa24..4398dd6e90b33ffb245d83231e37bf7e8d74d8a5 100644 (file)
@@ -56,7 +56,7 @@ impl AccountScope {
   }
 
   #[throws(InvalidScopedName)]
-  pub fn parse_name<const N: usize>(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 }
   }
 }
 
index 122ee402e9b6ab73dd5670636019dc958e8cacac..d378df3074cb3700dd6045ce8b902f7ec44ffed4 100644 (file)
@@ -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,
index 40a726b21d062abb3da8c85c4cab766ef695cd14..4dc5b5cd9cbbcc0703088e73c52dd50f695ace92 100644 (file)
@@ -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;
index 76df4670890ce36be314c86eed5dc1d4094201e5..51c5a11c066af8f358acf0c9317596e3912f5468 100644 (file)
@@ -4,7 +4,6 @@
 
 #![feature(proc_macro_hygiene, decl_macro)]
 #![feature(slice_strip)]
-#![feature(min_const_generics)]
 
 #![allow(clippy::redundant_closure_call)]