convert config from haskell lists
authoralfadur
Wed, 20 May 2020 22:50:58 +0300
changeset 15578 0b6094660557
parent 15577 7d4f552e317f
child 15579 db710cd8df69
convert config from haskell lists
rust/hedgewars-server/src/server/demo.rs
rust/hedgewars-server/src/server/haskell.rs
--- a/rust/hedgewars-server/src/server/demo.rs	Wed May 20 19:38:44 2020 +0300
+++ b/rust/hedgewars-server/src/server/demo.rs	Wed May 20 22:50:58 2020 +0300
@@ -318,9 +318,59 @@
 
     let mut config = Vec::with_capacity(map_config.len() + game_config.len());
 
-    for item in map_config {}
+    for item in map_config {
+        let mut tuple = item.into_tuple()?;
+        let mut tuple_iter = tuple.drain(..);
+        let name = tuple_iter.next()?.into_string()?;
+        let value = tuple_iter.next()?.into_string()?;
+
+        let config_item = match &name[..] {
+            "FEATURE_SIZE" => GameCfg::FeatureSize(u32::from_str(&value).ok()?),
+            "MAP" => GameCfg::MapType(value),
+            "MAPGEN" => GameCfg::MapGenerator(u32::from_str(&value).ok()?),
+            "MAZE_SIZE" => GameCfg::MazeSize(u32::from_str(&value).ok()?),
+            "SEED" => GameCfg::Seed(value),
+            "TEMPLATE" => GameCfg::Template(u32::from_str(&value).ok()?),
+            "DRAWNMAP" => GameCfg::DrawnMap(value),
+            _ => None?,
+        };
+        config.push(config_item);
+    }
+
+    for item in game_config {
+        let mut tuple = item.into_tuple()?;
+        let mut tuple_iter = tuple.drain(..);
+        let name = tuple_iter.next()?.into_string()?;
+        let value = tuple_iter.next()?;
 
-    for item in game_config {}
+        let config_item = match &name[..] {
+            "AMMO" => {
+                let mut ammo = value.into_list()?;
+                let mut ammo_iter = ammo.drain(..);
+                GameCfg::Ammo(
+                    ammo_iter.next()?.into_string()?,
+                    ammo_iter.next().and_then(|v| v.into_string()),
+                )
+            }
+            "SCHEME" => {
+                let mut scheme = value.into_list()?;
+                let mut scheme_iter = scheme.drain(..);
+                GameCfg::Scheme(
+                    scheme_iter.next()?.into_string()?,
+                    scheme_iter
+                        .next()?
+                        .into_list()?
+                        .drain(..)
+                        .filter_map(|v| v.into_string())
+                        .collect(),
+                )
+            }
+            "SCRIPT" => GameCfg::Script(value.into_string()?),
+            "THEME" => GameCfg::Theme(value.into_string()?),
+            _ => None?,
+        };
+        config.push(config_item);
+    }
 
     let mut messages = Vec::with_capacity(engine_messages.len());
 
--- a/rust/hedgewars-server/src/server/haskell.rs	Wed May 20 19:38:44 2020 +0300
+++ b/rust/hedgewars-server/src/server/haskell.rs	Wed May 20 22:50:58 2020 +0300
@@ -1,11 +1,12 @@
 use crate::server::haskell::HaskellValue::Boolean;
+use nom::multi::many0;
 use nom::{
     branch::alt,
     bytes::complete::{escaped_transform, is_not, tag, take_while, take_while1},
     character::{is_alphanumeric, is_digit, is_space},
     combinator::{map, map_res},
     multi::separated_list,
-    sequence::{delimited, pair, preceded, separated_pair},
+    sequence::{delimited, pair, preceded, separated_pair, terminated},
     ExtendInto, IResult,
 };
 use std::{
@@ -329,7 +330,7 @@
                 identifier,
                 preceded(
                     take_while(is_space),
-                    separated_list(take_while1(is_space), value),
+                    many0(terminated(value, take_while(is_space))),
                 ),
             ),
             |(name, mut fields)| HaskellValue::AnonStruct {