From: Ian Jackson Date: Sun, 2 Aug 2020 22:45:59 +0000 (+0100) Subject: wip save filename parse X-Git-Tag: otter-0.2.0~1196 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=18c2d34d2ae6125c7eb4fde5a0130d9986d6120d;p=otter.git wip save filename parse --- diff --git a/src/global.rs b/src/global.rs index c4c73474..aec84d26 100644 --- a/src/global.rs +++ b/src/global.rs @@ -515,6 +515,16 @@ impl InstanceGuard<'_> { // ---------- save/load ---------- +enum SavefilenameParseResult { + NotGameFile, + AccessFile, + TempToDelete, + GameFile { + access_leaf : Vec, + 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, diff --git a/src/imports.rs b/src/imports.rs index fdeb2d53..0f69f9d1 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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}; diff --git a/src/lib.rs b/src/lib.rs index 5fc4ccad..5ed5f2f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![feature(proc_macro_hygiene, decl_macro)] +#![feature(slice_strip)] pub mod imports; pub mod global;