Skip to content

Update screeps-game-api to Rust 2018. #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cargo-screeps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "cargo-screeps"
version = "0.3.2"
edition = "2018"
authors = ["David Ross <[email protected]>"]
documentation = "https://github.com/daboross/screeps-in-rust-via-wasm/cargo-screeps/"
include = [
Expand Down
2 changes: 1 addition & 1 deletion cargo-screeps/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Cow, ffi::OsStr, fs, io::Write, path::Path, process};

use config::{BuildConfiguration, Configuration};
use crate::config::{BuildConfiguration, Configuration};

use {failure, regex};

Expand Down
2 changes: 1 addition & 1 deletion cargo-screeps/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashSet;

use failure;

use config::Configuration;
use crate::config::Configuration;

pub fn copy<P: AsRef<Path>>(root: P, config: &Configuration) -> Result<(), failure::Error> {
let root = root.as_ref();
Expand Down
2 changes: 1 addition & 1 deletion cargo-screeps/src/orientation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use failure;

use setup::CliConfig;
use crate::setup::CliConfig;

pub fn find_project_root(cli_config: &CliConfig) -> Result<PathBuf, failure::Error> {
if let Some(config_path) = cli_config.config_path.as_ref() {
Expand Down
2 changes: 1 addition & 1 deletion cargo-screeps/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::Path;

use failure;

use {
use crate::{
build,
config::{self, Configuration},
copy, orientation, setup, upload,
Expand Down
2 changes: 1 addition & 1 deletion cargo-screeps/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, fs, io::Read, path::Path};

use {base64, failure, reqwest, serde_json};

use config::{Authentication, Configuration};
use crate::config::{Authentication, Configuration};

pub fn upload(root: &Path, config: &Configuration) -> Result<(), failure::Error> {
let upload_config = config.upload.as_ref().ok_or_else(|| {
Expand Down
4 changes: 2 additions & 2 deletions screeps-game-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "screeps-game-api"
version = "0.4.0"
edition = "2018"
authors = ["David Ross <[email protected]>"]
documentation = "https://docs.rs/screeps-game-api/"
include = [
Expand All @@ -26,8 +27,7 @@ name = "screeps"
log = "0.4"
num-derive = "0.2"
num-traits = "0.2"
serde = "1"
serde_derive = "1"
serde = {version = "1", features = ["derive"]}
serde_json = "1"
scoped-tls = "0.1"
stdweb = "0.4"
Expand Down
65 changes: 65 additions & 0 deletions screeps-game-api/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = true
format_code_in_doc_comments = false
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
merge_imports = true
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
force_multiline_blocks = false
fn_args_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2018"
version = "One"
inline_attribute_width = 0
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
required_version = "1.3.0"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Never"
report_fixme = "Never"
ignore = []
emit_mode = "Files"
make_backup = false
48 changes: 28 additions & 20 deletions screeps-game-api/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
//! <https://github.com/screeps/common/commits/master/lib/constants.js>.
//!
//! [the game constants]: https://github.com/screeps/common/blob/master/lib/constants.js

use log::error;
use num_derive::FromPrimitive;
#[allow(unused_imports)]
use serde::{Deserialize, Serialize};
use stdweb::{Number, Reference, Value};

use {
use crate::{
objects::RoomObject,
traits::{FromExpectedType, TryFrom, TryInto},
ConversionError,
Expand Down Expand Up @@ -40,8 +45,8 @@ impl_serialize_as_i32!(ReturnCode);
impl ReturnCode {
/// Turns this return code into a result.
///
/// `ReturnCode::Ok` is turned into `Result::Ok`, all other codes are turned into
/// `Result::Err(code)`
/// `ReturnCode::Ok` is turned into `Result::Ok`, all other codes are turned
/// into `Result::Err(code)`
pub fn as_result(self) -> Result<(), Self> {
match self {
ReturnCode::Ok => Ok(()),
Expand Down Expand Up @@ -124,14 +129,15 @@ unsafe impl FindConstant for FindObject {
}

pub mod find {
use stdweb::unstable::TryFrom;

use objects::{
ConstructionSite, Creep, Flag, Mineral, Nuke, OwnedStructure, Resource, RoomPosition,
Source, Structure, StructureSpawn, Tombstone,
};

use super::FindConstant;
use crate::{
objects::{
ConstructionSite, Creep, Flag, Mineral, Nuke, OwnedStructure, Resource, RoomPosition,
Source, Structure, StructureSpawn, Tombstone,
},
traits::TryFrom,
};
use serde::Deserialize;

#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
#[serde(transparent)]
Expand Down Expand Up @@ -232,7 +238,8 @@ impl TryFrom<Value> for Direction {
impl ::std::ops::Neg for Direction {
type Output = Direction;

/// Negates this direction. Top goes to Bottom, TopRight goes to BottomLeft, etc.
/// Negates this direction. Top goes to Bottom, TopRight goes to BottomLeft,
/// etc.
///
/// Example usage:
///
Expand All @@ -244,7 +251,7 @@ impl ::std::ops::Neg for Direction {
/// assert_eq!(-Left, Right);
/// ```
fn neg(self) -> Direction {
use Direction::*;
use crate::Direction::*;

match self {
Top => Bottom,
Expand Down Expand Up @@ -335,10 +342,11 @@ impl TryFrom<Value> for Terrain {

/// Internal enum representing each LOOK_* constant.
///
/// It's recommended to use the constants in the `look` module instead for type safety.
/// It's recommended to use the constants in the `look` module instead for type
/// safety.
///
/// In fact, I don't believe this can be used at all without resorting to manually
/// including JS code.
/// In fact, I don't believe this can be used at all without resorting to
/// manually including JS code.
///
/// To use in JS: `__look_num_to_str(@{look as u32})` function
#[repr(u32)]
Expand Down Expand Up @@ -369,15 +377,14 @@ pub unsafe trait LookConstant {
}

pub mod look {
use {
use super::{Look, LookConstant, Terrain};
use crate::{
objects::{
ConstructionSite, Creep, Flag, Mineral, Nuke, Resource, Source, Structure, Tombstone,
},
traits::{IntoExpectedType, TryInto},
};

use super::{Look, LookConstant, Terrain};

typesafe_look_constants! {
CREEPS, Look::Creeps, Creep, IntoExpectedType::into_expected_type;
ENERGY, Look::Energy, Resource, IntoExpectedType::into_expected_type;
Expand Down Expand Up @@ -883,9 +890,10 @@ pub enum ResourceType {
}

impl ResourceType {
/// Returns `REACTION_TIME` for this resource. 0 for energy and base minerals.
/// Returns `REACTION_TIME` for this resource. 0 for energy and base
/// minerals.
pub fn reaction_time(self) -> u32 {
use ResourceType::*;
use crate::ResourceType::*;
match self {
Energy | Power | Hydrogen | Oxygen | Utrium | Lemergium | Keanium | Zynthium
| Catalyst | Ghodium => 0,
Expand Down
37 changes: 26 additions & 11 deletions screeps-game-api/src/game.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use {
use crate::{
macros::*,
objects::{HasId, RoomObject, SizedRoomObject},
traits::TryInto,
ConversionError,
Expand All @@ -13,7 +14,9 @@ use {
pub mod cpu {
use std::collections;
Copy link
Collaborator

@daboross daboross Jul 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One std:: below serde:: here, but ¯\_(ツ)_/¯


use constants::ReturnCode;
use serde::{Deserialize, Serialize};

use crate::{constants::ReturnCode, macros::*, traits::TryInto};

/// See [`v8_getheapstatistics`]
///
Expand Down Expand Up @@ -68,7 +71,6 @@ pub mod cpu {
///
/// Returns object with all 0 values if heap statistics are not available.
pub fn get_heap_statistics() -> HeapStatistics {
use stdweb::unstable::TryInto;
use stdweb::Value;

let heap_stats: Value =
Expand Down Expand Up @@ -101,6 +103,8 @@ pub mod cpu {
///
/// [http://docs.screeps.com/api/#Game.gcl]: http://docs.screeps.com/api/#Game.gcl
pub mod gcl {
use crate::macros::*;

/// See [http://docs.screeps.com/api/#Game.gcl]
///
/// [http://docs.screeps.com/api/#Game.gcl]: http://docs.screeps.com/api/#Game.gcl
Expand Down Expand Up @@ -129,10 +133,13 @@ pub mod gcl {
pub mod map {
use std::{collections, mem};

use scoped_tls::scoped_thread_local;
use serde::Deserialize;
use stdweb::Value;

use {
use crate::{
constants::{find::Exit, Direction, ReturnCode},
macros::*,
objects::RoomTerrain,
traits::{TryFrom, TryInto},
};
Expand Down Expand Up @@ -269,10 +276,14 @@ pub mod map {
pub mod market {
use std::collections::HashMap;

use stdweb::unstable::TryInto;
use serde::Deserialize;

use constants::{ResourceType, ReturnCode};
use Room;
use crate::{
constants::{ResourceType, ReturnCode},
macros::*,
traits::TryInto,
Room,
};

#[repr(u32)]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -436,6 +447,8 @@ pub mod market {
///
/// [http://docs.screeps.com/api/#Game.shard]: http://docs.screeps.com/api/#Game.shard
pub mod shard {
use crate::macros::*;

/// See [http://docs.screeps.com/api/#Game.shard]
///
/// [http://docs.screeps.com/api/#Game.shard]: http://docs.screeps.com/api/#Game.shard
Expand Down Expand Up @@ -497,10 +510,11 @@ pub fn time() -> u32 {

/// See [http://docs.screeps.com/api/#Game.getObjectById]
///
/// This gets an object expecting a specific type and will return a `ConversionError` if the type
/// does not match.
/// This gets an object expecting a specific type and will return a
/// `ConversionError` if the type does not match.
///
/// If all you want to assume is that something has an ID, use [`get_object_erased`].
/// If all you want to assume is that something has an ID, use
/// [`get_object_erased`].
/// [http://docs.screeps.com/api/#Game.getObjectById]: http://docs.screeps.com/api/#Game.getObjectById
pub fn get_object_typed<T>(id: &str) -> Result<Option<T>, ConversionError>
where
Expand All @@ -511,7 +525,8 @@ where

/// See [http://docs.screeps.com/api/#Game.getObjectById]
///
/// This gets the object in 'erased' form - all that is known about it is that it's a RoomObject.
/// This gets the object in 'erased' form - all that is known about it is that
/// it's a RoomObject.
///
/// If a more specific type is expected, [`get_object_typed`] can be used.
/// [http://docs.screeps.com/api/#Game.getObjectById]: http://docs.screeps.com/api/#Game.getObjectById
Expand Down
Loading