diff options
| author | troido <troido@protonmail.com> | 2020-04-17 18:05:31 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-17 18:05:31 +0200 |
| commit | 047b23895df301a622cfd3330787ef900c9777e9 (patch) | |
| tree | b2dd3a9191234138ad0ced6db9bf68e941780a7d | |
| parent | 9510d34c71417c4c2183240938987607b9a981c6 (diff) | |
roomtemplate can handle fields that don't match the given size
| -rw-r--r-- | src/roomtemplate.rs | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/roomtemplate.rs b/src/roomtemplate.rs index da4d3a5..c8d9918 100644 --- a/src/roomtemplate.rs +++ b/src/roomtemplate.rs @@ -38,13 +38,15 @@ impl RoomTemplate { mapping.insert(key.chars().next().ok_or(perr!("mapping key is empty string"))?, templates); } + let width = size.0 as usize; + let height = size.1 as usize; let mut field = Vec::new(); - field.resize((size.0 * size.1) as usize, Vec::new()); + field.resize_with(width * height, Vec::new); let jsonfield: &Vec<Value> = jsonroom.get("field").ok_or(perr!("no field"))?.as_array().ok_or(perr!("field not an array"))?; // todo: what if size doesn't match actual dimensions - for (y, row) in jsonfield.iter().enumerate() { - for (x, ch) in row.as_str().ok_or(perr!("field row not a string"))?.chars().enumerate() { - field[x + y * (size.0 as usize)] = mapping.get(&ch).ok_or(perr!("char not found in mapping"))?.clone(); + for (y, row) in jsonfield.iter().take(height).enumerate() { + for (x, ch) in row.as_str().ok_or(perr!("field row not a string"))?.chars().take(width).enumerate() { + field[x + y * width] = mapping.get(&ch).ok_or(perr!("char not found in mapping"))?.clone(); } } |
