chiark / gitweb /
wip save filename parse
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 2 Aug 2020 22:45:59 +0000 (23:45 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 2 Aug 2020 22:49:12 +0000 (23:49 +0100)
src/global.rs
src/imports.rs
src/lib.rs

index c4c7347425ca6ded94711e31d994d3290091a71f..aec84d2669cd4943652d12f4db787fc5ef4fce20 100644 (file)
@@ -515,6 +515,16 @@ impl InstanceGuard<'_> {
 
 // ---------- save/load ----------
 
+enum SavefilenameParseResult {
+  NotGameFile,
+  AccessFile,
+  TempToDelete,
+  GameFile {
+    access_leaf : Vec<u8>,
+    name : InstanceName,
+  },
+}
+
 impl InstanceGuard<'_> {
   fn savefile(name: &InstanceName, prefix: &str, suffix: &str) -> String {
     let scope_prefix = { use ManagementScope::*; match &name.scope {
@@ -528,6 +538,37 @@ impl InstanceGuard<'_> {
       .chain([ suffix ].iter().map(Deref::deref))
       .collect()
   }
+
+  #[throws(anyhow::Error)]
+  fn savefilename_parse(leaf: &[u8]) -> SavefilenameParseResult {
+    use SavefilenameParseResult::*;
+
+    if leaf.starts_with(b"a-") { return AccessFile }
+    let rhs = match leaf.strip_prefix(b"g-") {
+      Some(rhs) => rhs,
+      None => return NotGameFile,
+    };
+    let after_ftype_prefix = rhs;
+    let rhs = str::from_utf8(rhs)?;
+    let (rhs, scope) = match rhs.find(':') {
+      None => {
+        (rhs, ManagementScope::Server)
+      },
+      Some(colon) => {
+        let (lhs, rhs) = rhs.split_at(colon);
+        assert_eq!(rhs.chars().next(), Some(':'));
+        (rhs, ManagementScope::Unix { user: lhs.to_owned() })
+      },
+    };
+    if rhs.rfind('.').is_some() { return TempToDelete }
+    let scoped_name = percent_decode_str(rhs).decode_utf8()?.into();
+    GameFile {
+      access_leaf : [ b"a-", after_ftype_prefix ]
+        .iter().flat_map(|s| s.iter()).map(|c| *c).collect(),
+      name : InstanceName { scope, scoped_name },
+    }
+  }
+  
   #[throws(ServerFailure)]
   fn save_something(
     &self, prefix: &str,
index fdeb2d535caa4cfe45fa6e2a80ba21aa605c8f21..0f69f9d1c2413f7fa65e64028c73f066cb1d3d04 100644 (file)
@@ -38,6 +38,7 @@ pub use rocket_contrib::helmet::*;
 pub use rocket_contrib::templates::Template;
 
 pub use percent_encoding::utf8_percent_encode;
+pub use percent_encoding::percent_decode_str;
 pub use percent_encoding::NON_ALPHANUMERIC;
 
 pub use rocket::{State,Rocket};
index 5fc4ccad96a2d6168d631f3ca667f8c0b766d66f..5ed5f2f28662db35b123cab4637a1da06cd15a18 100644 (file)
@@ -1,5 +1,6 @@
 
 #![feature(proc_macro_hygiene, decl_macro)]
+#![feature(slice_strip)]
 
 pub mod imports;
 pub mod global;