chiark / gitweb /
compiles again
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Jun 2020 22:45:24 +0000 (23:45 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Jun 2020 22:45:24 +0000 (23:45 +0100)
junk/global.rs
src/bin/server.rs
src/global.rs
src/imports.rs

index 5872f43711b6150066f99362efd85ca197a7b58f..009215cf780b047e94744432953df95c5c4f1a98 100644 (file)
@@ -22,13 +22,3 @@ impl<'r> FromParam<'r> for InstanceGuard<'r> {
 }
 */
 
-impl<'r> FromParam<'r> for InstanceAccess<'r> {
-  type Error = AE;
-  #[throws(E)]
-  fn from_param(param: &'r RawStr) -> Option<Self> {
-    let g = GLOBAL.instances().read();
-    let iname = param.as_str();
-    g.get(iname);
-  }
-}
-
index 7309e3a399973dc33de03299d20716f98ddba429..43a0d23f1dd2c5597c86ad2247febcbcf0b5033f 100644 (file)
@@ -35,6 +35,13 @@ impl<'r> FromParam<'r> for CheckedResourceLeaf {
   }
 }
 
+#[get("/<token>")]
+fn loading(token : InstanceAccess) -> Result<Template,RE> {
+  let _ = token;
+  let c = TestRenderContext { };
+  Ok(Template::render("loading",&c))
+}
+
 #[derive(Serialize)]
 enum Update {
   TestCounter { value: usize },
@@ -83,6 +90,7 @@ fn main() {
     .attach(Template::fairing())
     .mount("/", routes![
       index,
+      loading,
       resource,
       updates,
     ])
index 60f6a2637d83d6287d3072d21f49c3a921369b56..3733692b23b4702e49a3979208dcb780ea7b999f 100644 (file)
@@ -1,10 +1,12 @@
 
+#![allow(dead_code)]
+
 use crate::imports::*;
 use lazy_static::lazy_static;
 
 slotmap::new_key_type!{
-  struct UserId;
-  struct ClientId;
+  pub struct UserId;
+  pub struct ClientId;
 }
 
 #[derive(Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)]
@@ -13,30 +15,36 @@ impl Borrow<str> for RawToken {
   fn borrow(&self) -> &str { &self.0 }
 }
 
-struct Client {
+pub struct Client {
   user : UserId,
 }
 
-struct User {
+pub struct User {
   nick : String,
 }
 
-struct Instance {
+pub struct Instance {
   /* game state goes here */
   users : DenseSlotMap<UserId,User>,
   clients : DenseSlotMap<ClientId,Client>,
 }
 
 #[derive(Clone)]
-struct InstanceAccess {
-  i : Arc<Mutex<Instance>>,
-  user : UserId,
+pub struct InstanceAccessDetails {
+  pub i : Arc<Mutex<Instance>>,
+  pub user : UserId,
+}
+
+#[derive(Clone)]
+pub struct InstanceAccess<'i> {
+  pub raw_token : &'i str,
+  pub i : InstanceAccessDetails,
 }
 
 #[derive(Default)]
 struct Global {
   // lock hierarchy: this is the innermost lock
-  tokens : RwLock<HashMap<RawToken, InstanceAccess>>,
+  tokens : RwLock<HashMap<RawToken, InstanceAccessDetails>>,
   // xxx delete instances at some point!
 }
 
@@ -44,7 +52,7 @@ lazy_static! {
   static ref GLOBAL : Global = Default::default();
 }
 
-fn lookup_token(s : &str) -> Option<InstanceAccess> {
+fn lookup_token(s : &str) -> Option<InstanceAccessDetails> {
   GLOBAL.tokens.read().unwrap().get(s).cloned()
 }
 
@@ -53,6 +61,17 @@ const XXX_USERS_TOKENS : &[(&str, &str)] = &[
   ("ccg9kzoTh758QrVE1xMY7BQWB36dNJTx", "bob"),
 ];
 
+impl<'r> FromParam<'r> for InstanceAccess<'r> {
+  type Error = E;
+  #[throws(E)]
+  fn from_param(param: &'r RawStr) -> Self {
+    let g = GLOBAL.tokens.read().unwrap();
+    let token = param.as_str();
+    let i = g.get(token).ok_or_else(|| anyhow!("unknown token"))?;
+    InstanceAccess { raw_token : token, i : i.clone() }
+  }
+}
+
 pub fn xxx_global_setup() {
   let i = Instance {
     users : Default::default(),
@@ -63,7 +82,7 @@ pub fn xxx_global_setup() {
   for (token, nick) in XXX_USERS_TOKENS {
     let nu = User { nick : nick.to_string() };
     let user = ig.users.insert(nu);
-    let ia = InstanceAccess { i : i.clone(), user };
+    let ia = InstanceAccessDetails { i : i.clone(), user };
     GLOBAL.tokens.write().unwrap().insert(
       RawToken(token.to_string()), ia
     );
index 627abb36bbc92ff4e229b1a1e9174041fc89266f..f7a2171efbd434550a922a4c0ea85433930fdd56 100644 (file)
@@ -9,6 +9,7 @@ pub use std::borrow::Borrow;
 
 pub use thiserror::Error;
 pub use anyhow::{Context,anyhow};
+pub use fehler::{throws,throw};
 
 pub use serde::Deserialize;
 pub use serde::Serialize;
@@ -24,6 +25,7 @@ pub use rocket::response::NamedFile;
 pub use rocket::response;
 
 pub use slotmap::dense::{DenseSlotMap};
+pub use crate::global::InstanceAccess;
 
 pub type E = anyhow::Error;