mindustry logic execution, map- and schematic- parsing and rendering
Normalize enums using Self over the type name
| -rw-r--r-- | src/access.rs | 12 | ||||
| -rw-r--r-- | src/block/mod.rs | 32 | ||||
| -rw-r--r-- | src/content.rs | 18 | ||||
| -rw-r--r-- | src/data/base64.rs | 10 | ||||
| -rw-r--r-- | src/data/dynamic.rs | 80 | ||||
| -rw-r--r-- | src/data/mod.rs | 12 | ||||
| -rw-r--r-- | src/data/schematic.rs | 84 | ||||
| -rw-r--r-- | src/exe/args.rs | 36 |
8 files changed, 142 insertions, 142 deletions
diff --git a/src/access.rs b/src/access.rs index 9b8cc58..96369c8 100644 --- a/src/access.rs +++ b/src/access.rs @@ -20,7 +20,7 @@ impl<'a, T: AsRef<B>, B> Access<'a, T, B> { match self { - Access::Borrowed(..) => true, + Self::Borrowed(..) => true, _ => false, } } @@ -29,7 +29,7 @@ impl<'a, T: AsRef<B>, B> Access<'a, T, B> { match self { - Access::Owned(..) => true, + Self::Owned(..) => true, _ => false, } } @@ -57,8 +57,8 @@ impl<'a, T: AsRef<B>, B: ?Sized> Borrow<B> for Access<'a, T, B> { match self { - Access::Borrowed(r) => *r, - Access::Owned(v) => v.as_ref(), + Self::Borrowed(r) => *r, + Self::Owned(v) => v.as_ref(), } } } @@ -79,8 +79,8 @@ impl<'a, T: AsRef<B>, B: ?Sized> Deref for Access<'a, T, B> { match self { - Access::Borrowed(r) => *r, - Access::Owned(v) => v.as_ref(), + Self::Borrowed(r) => *r, + Self::Owned(v) => v.as_ref(), } } } diff --git a/src/block/mod.rs b/src/block/mod.rs index 449c834..98c41a3 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -221,10 +221,10 @@ impl Rotation { match self { - Rotation::Right => if horizontally {Rotation::Left} else {Rotation::Right}, - Rotation::Up => if vertically {Rotation::Down} else {Rotation::Up}, - Rotation::Left => if horizontally {Rotation::Right} else {Rotation::Left}, - Rotation::Down => if vertically {Rotation::Up} else {Rotation::Down}, + Self::Right => if horizontally {Self::Left} else {Self::Right}, + Self::Up => if vertically {Self::Down} else {Self::Up}, + Self::Left => if horizontally {Self::Right} else {Self::Left}, + Self::Down => if vertically {Self::Up} else {Self::Down}, } } @@ -237,10 +237,10 @@ impl Rotation { match self { - Rotation::Right => if clockwise {Rotation::Up} else {Rotation::Down}, - Rotation::Up => if clockwise {Rotation::Left} else {Rotation::Right}, - Rotation::Left => if clockwise {Rotation::Down} else {Rotation::Up}, - Rotation::Down => if clockwise {Rotation::Right} else {Rotation::Left}, + Self::Right => if clockwise {Self::Up} else {Self::Down}, + Self::Up => if clockwise {Self::Left} else {Self::Right}, + Self::Left => if clockwise {Self::Down} else {Self::Up}, + Self::Down => if clockwise {Self::Right} else {Self::Left}, } } @@ -253,10 +253,10 @@ impl Rotation { match self { - Rotation::Right => Rotation::Left, - Rotation::Up => Rotation::Down, - Rotation::Left => Rotation::Right, - Rotation::Down => Rotation::Up, + Self::Right => Self::Left, + Self::Up => Self::Down, + Self::Left => Self::Right, + Self::Down => Self::Up, } } @@ -272,10 +272,10 @@ impl From<u8> for Rotation { match val & 3 { - 0 => Rotation::Right, - 1 => Rotation::Up, - 2 => Rotation::Left, - _ => Rotation::Down, + 0 => Self::Right, + 1 => Self::Up, + 2 => Self::Left, + _ => Self::Down, } } } diff --git a/src/content.rs b/src/content.rs index d0f7df4..5f04dc8 100644 --- a/src/content.rs +++ b/src/content.rs @@ -33,12 +33,12 @@ macro_rules!numeric_enum type Error = $error; #[allow(non_upper_case_globals)] - fn try_from(value: $numeric) -> Result<$tname, $error> + fn try_from(value: $numeric) -> Result<Self, $error> { $(const $name: $numeric = $tname::$name as $numeric;)+ match value { - $($name => Ok($tname::$name),)+ + $($name => Ok(Self::$name),)+ _ => Err($error(value)), } } @@ -77,7 +77,7 @@ macro_rules!content_enum { match self { - $($tname::$name => $vname,)* + $(Self::$name => $vname,)* } } } @@ -136,12 +136,12 @@ impl Type { match self { - Type::Item => gen_by_id!(crate::item::Type, id), - Type::Block => gen_by_id!(crate::block::content::Type, id), - Type::Fluid => gen_by_id!(crate::fluid::Type, id), - Type::Modifier => gen_by_id!(crate::modifier::Type, id), - Type::Unit => gen_by_id!(crate::unit::Type, id), - Type::Team => gen_by_id!(crate::team::Team, id), + Self::Item => gen_by_id!(crate::item::Type, id), + Self::Block => gen_by_id!(crate::block::content::Type, id), + Self::Fluid => gen_by_id!(crate::fluid::Type, id), + Self::Modifier => gen_by_id!(crate::modifier::Type, id), + Self::Unit => gen_by_id!(crate::unit::Type, id), + Self::Team => gen_by_id!(crate::team::Team, id), _ => Ok(Box::new(Generic(*self, id))), } } diff --git a/src/data/base64.rs b/src/data/base64.rs index 8475f42..b1c14e7 100644 --- a/src/data/base64.rs +++ b/src/data/base64.rs @@ -178,10 +178,10 @@ impl fmt::Display for DecodeError { match self { - DecodeError::Malformed{at, value} => write!(f, "malformed base64 character {value:?} (at {at})"), - DecodeError::Overflow{need, have} => write!(f, "decoder overflow (need {need}, but only have {have})"), - DecodeError::Truncated => write!(f, "truncated base64 input stream"), - DecodeError::TrailingData{at} => write!(f, "trailing data in base64 stream (at {at})"), + Self::Malformed{at, value} => write!(f, "malformed base64 character {value:?} (at {at})"), + Self::Overflow{need, have} => write!(f, "decoder overflow (need {need}, but only have {have})"), + Self::Truncated => write!(f, "truncated base64 input stream"), + Self::TrailingData{at} => write!(f, "trailing data in base64 stream (at {at})"), } } } @@ -200,7 +200,7 @@ impl fmt::Display for EncodeError { match self { - EncodeError::Overflow{need, have} => write!(f, "encoder overflow (need {need}, but only have {have})"), + Self::Overflow{need, have} => write!(f, "encoder overflow (need {need}, but only have {have})"), } } } diff --git a/src/data/dynamic.rs b/src/data/dynamic.rs index c42ac7d..3a487c2 100644 --- a/src/data/dynamic.rs +++ b/src/data/dynamic.rs @@ -38,27 +38,27 @@ impl DynData { match self { - DynData::Empty => DynType::Empty, - DynData::Int(..) => DynType::Int, - DynData::Long(..) => DynType::Long, - DynData::Float(..) => DynType::Float, - DynData::String(..) => DynType::String, - DynData::Content(..) => DynType::Content, - DynData::IntArray(..) => DynType::IntArray, - DynData::Point2(..) => DynType::Point2, - DynData::Point2Array(..) => DynType::Point2Array, - DynData::TechNode(..) => DynType::TechNode, - DynData::Boolean(..) => DynType::Boolean, - DynData::Double(..) => DynType::Double, - DynData::Building(..) => DynType::Building, - DynData::LogicField(..) => DynType::LogicField, - DynData::ByteArray(..) => DynType::ByteArray, - DynData::UnitCommand(..) => DynType::UnitCommand, - DynData::BoolArray(..) => DynType::BoolArray, - DynData::Unit(..) => DynType::Unit, - DynData::Vec2Array(..) => DynType::Vec2Array, - DynData::Vec2(..) => DynType::Vec2, - DynData::Team(..) => DynType::Team, + Self::Empty => DynType::Empty, + Self::Int(..) => DynType::Int, + Self::Long(..) => DynType::Long, + Self::Float(..) => DynType::Float, + Self::String(..) => DynType::String, + Self::Content(..) => DynType::Content, + Self::IntArray(..) => DynType::IntArray, + Self::Point2(..) => DynType::Point2, + Self::Point2Array(..) => DynType::Point2Array, + Self::TechNode(..) => DynType::TechNode, + Self::Boolean(..) => DynType::Boolean, + Self::Double(..) => DynType::Double, + Self::Building(..) => DynType::Building, + Self::LogicField(..) => DynType::LogicField, + Self::ByteArray(..) => DynType::ByteArray, + Self::UnitCommand(..) => DynType::UnitCommand, + Self::BoolArray(..) => DynType::BoolArray, + Self::Unit(..) => DynType::Unit, + Self::Vec2Array(..) => DynType::Vec2Array, + Self::Vec2(..) => DynType::Vec2, + Self::Team(..) => DynType::Team, } } } @@ -397,7 +397,7 @@ impl From<data::ReadError> for ReadError { fn from(err: data::ReadError) -> Self { - ReadError::Underlying(err) + Self::Underlying(err) } } @@ -407,15 +407,15 @@ impl fmt::Display for ReadError { match self { - ReadError::Underlying(e) => e.fmt(f), - ReadError::Type(id) => write!(f, "invalid dynamic data type ({id})"), - ReadError::IntArrayLen(len) => write!(f, "integer array too long ({len})"), - ReadError::Point2ArrayLen(len) => write!(f, "point2 array too long ({len})"), - ReadError::LogicField(id) => write!(f, "invalid logic field ({id})"), - ReadError::ByteArrayLen(len) => write!(f, "byte array too long ({len})"), - ReadError::UnitCommand(id) => write!(f, "invalid unit command ({id})"), - ReadError::BoolArrayLen(len) => write!(f, "boolean array too long ({len})"), - ReadError::Vec2ArrayLen(len) => write!(f, "vec2 array too long ({len})"), + Self::Underlying(e) => e.fmt(f), + Self::Type(id) => write!(f, "invalid dynamic data type ({id})"), + Self::IntArrayLen(len) => write!(f, "integer array too long ({len})"), + Self::Point2ArrayLen(len) => write!(f, "point2 array too long ({len})"), + Self::LogicField(id) => write!(f, "invalid logic field ({id})"), + Self::ByteArrayLen(len) => write!(f, "byte array too long ({len})"), + Self::UnitCommand(id) => write!(f, "invalid unit command ({id})"), + Self::BoolArrayLen(len) => write!(f, "boolean array too long ({len})"), + Self::Vec2ArrayLen(len) => write!(f, "vec2 array too long ({len})"), } } } @@ -426,7 +426,7 @@ impl Error for ReadError { match self { - ReadError::Underlying(e) => Some(e), + Self::Underlying(e) => Some(e), _ => None, } } @@ -447,7 +447,7 @@ impl From<data::WriteError> for WriteError { fn from(err: data::WriteError) -> Self { - WriteError::Underlying(err) + Self::Underlying(err) } } @@ -457,12 +457,12 @@ impl fmt::Display for WriteError { match self { - WriteError::Underlying(e) => e.fmt(f), - WriteError::IntArrayLen(len) => write!(f, "integer array too long ({len})"), - WriteError::Point2ArrayLen(len) => write!(f, "point2 array too long ({len})"), - WriteError::ByteArrayLen(len) => write!(f, "byte array too long ({len})"), - WriteError::BoolArrayLen(len) => write!(f, "boolean array too long ({len})"), - WriteError::Vec2ArrayLen(len) => write!(f, "vec2 array too long ({len})"), + Self::Underlying(e) => e.fmt(f), + Self::IntArrayLen(len) => write!(f, "integer array too long ({len})"), + Self::Point2ArrayLen(len) => write!(f, "point2 array too long ({len})"), + Self::ByteArrayLen(len) => write!(f, "byte array too long ({len})"), + Self::BoolArrayLen(len) => write!(f, "boolean array too long ({len})"), + Self::Vec2ArrayLen(len) => write!(f, "vec2 array too long ({len})"), } } } @@ -473,7 +473,7 @@ impl Error for WriteError { match self { - WriteError::Underlying(e) => Some(e), + Self::Underlying(e) => Some(e), _ => None, } } diff --git a/src/data/mod.rs b/src/data/mod.rs index 845890b..0cc2104 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -105,7 +105,7 @@ impl From<Utf8Error> for ReadError { fn from(err: Utf8Error) -> Self { - ReadError::Utf8(err) + Self::Utf8(err) } } @@ -115,8 +115,8 @@ impl fmt::Display for ReadError { match self { - ReadError::Underflow{need, have} => write!(f, "buffer underflow (expected {need} but got {have})"), - ReadError::Utf8(e) => e.fmt(f), + Self::Underflow{need, have} => write!(f, "buffer underflow (expected {need} but got {have})"), + Self::Utf8(e) => e.fmt(f), } } } @@ -127,7 +127,7 @@ impl Error for ReadError { match self { - ReadError::Utf8(e) => Some(e), + Self::Utf8(e) => Some(e), _ => None, } } @@ -262,8 +262,8 @@ impl fmt::Display for WriteError { match self { - WriteError::Overflow{need, have} => write!(f, "buffer overflow (expected {need} but got {have})"), - WriteError::TooLong{len} => write!(f, "string too long ({len} bytes of {})", u16::MAX), + Self::Overflow{need, have} => write!(f, "buffer overflow (expected {need} but got {have})"), + Self::TooLong{len} => write!(f, "string too long ({len} bytes of {})", u16::MAX), } } } diff --git a/src/data/schematic.rs b/src/data/schematic.rs index 7c43ad2..928b7e4 100644 --- a/src/data/schematic.rs +++ b/src/data/schematic.rs @@ -448,8 +448,8 @@ impl fmt::Display for NewError { match self { - NewError::Width(w) => write!(f, "invalid schematic width ({w})"), - NewError::Height(h) => write!(f, "invalid schematic height ({h})"), + Self::Width(w) => write!(f, "invalid schematic width ({w})"), + Self::Height(h) => write!(f, "invalid schematic height ({h})"), } } } @@ -497,9 +497,9 @@ impl fmt::Display for PlaceError { match self { - PlaceError::Bounds{x, y, sz, w, h} => write!(f, "block placement {x} / {y} (size {sz}) within {w} / {h}"), - PlaceError::Overlap{x, y} => write!(f, "overlapping an existing block at {x} / {y}"), - PlaceError::Deserialize(e) => e.fmt(f), + Self::Bounds{x, y, sz, w, h} => write!(f, "block placement {x} / {y} (size {sz}) within {w} / {h}"), + Self::Overlap{x, y} => write!(f, "overlapping an existing block at {x} / {y}"), + Self::Deserialize(e) => e.fmt(f), } } } @@ -988,19 +988,19 @@ impl fmt::Display for ReadError { match self { - ReadError::Read(e) => e.fmt(f), - ReadError::Header(hdr) => write!(f, "incorrect header ({hdr:08X})"), - ReadError::Version(ver) => write!(f, "unsupported version ({ver})"), - ReadError::Decompress(e) => e.fmt(f), - ReadError::DecompressStall => write!(f, "decompressor stalled before completion"), - ReadError::Dimensions(w, h) => write!(f, "invalid schematic dimensions ({w} * {h})"), - ReadError::TableSize(cnt) => write!(f, "invalid block table size ({cnt})"), - ReadError::NoSuchBlock(name) => write!(f, "unknown block {name:?}"), - ReadError::BlockCount(cnt) => write!(f, "invalid total block count ({cnt})"), - ReadError::BlockIndex(idx, cnt) => write!(f, "invalid block index ({idx} / {cnt})"), - ReadError::BlockConfig(e) => e.fmt(f), - ReadError::BlockState(e) => e.fmt(f), - ReadError::Placement(e) => e.fmt(f), + Self::Read(e) => e.fmt(f), + Self::Header(hdr) => write!(f, "incorrect header ({hdr:08X})"), + Self::Version(ver) => write!(f, "unsupported version ({ver})"), + Self::Decompress(e) => e.fmt(f), + Self::DecompressStall => write!(f, "decompressor stalled before completion"), + Self::Dimensions(w, h) => write!(f, "invalid schematic dimensions ({w} * {h})"), + Self::TableSize(cnt) => write!(f, "invalid block table size ({cnt})"), + Self::NoSuchBlock(name) => write!(f, "unknown block {name:?}"), + Self::BlockCount(cnt) => write!(f, "invalid total block count ({cnt})"), + Self::BlockIndex(idx, cnt) => write!(f, "invalid block index ({idx} / {cnt})"), + Self::BlockConfig(e) => e.fmt(f), + Self::BlockState(e) => e.fmt(f), + Self::Placement(e) => e.fmt(f), } } } @@ -1011,11 +1011,11 @@ impl Error for ReadError { match self { - ReadError::Read(e) => Some(e), - ReadError::Decompress(e) => Some(e), - ReadError::BlockConfig(e) => Some(e), - ReadError::BlockState(e) => Some(e), - ReadError::Placement(e) => Some(e), + Self::Read(e) => Some(e), + Self::Decompress(e) => Some(e), + Self::BlockConfig(e) => Some(e), + Self::BlockState(e) => Some(e), + Self::Placement(e) => Some(e), _ => None, } } @@ -1072,14 +1072,14 @@ impl fmt::Display for WriteError { match self { - WriteError::Write(..) => write!(f, "failed to write data to buffer"), - WriteError::TagCount(cnt) => write!(f, "invalid tag count ({cnt})"), - WriteError::TableSize(cnt) => write!(f, "invalid block table size ({cnt})"), - WriteError::StateSerialize(e) => e.fmt(f), - WriteError::BlockState(..) => write!(f, "failed to write block state"), - WriteError::Compress(e) => e.fmt(f), - WriteError::CompressEof(remain) => write!(f, "compression overflow with {remain} bytes of input remaining"), - WriteError::CompressStall => write!(f, "compressor stalled before completion"), + Self::Write(..) => write!(f, "failed to write data to buffer"), + Self::TagCount(cnt) => write!(f, "invalid tag count ({cnt})"), + Self::TableSize(cnt) => write!(f, "invalid block table size ({cnt})"), + Self::StateSerialize(e) => e.fmt(f), + Self::BlockState(..) => write!(f, "failed to write block state"), + Self::Compress(e) => e.fmt(f), + Self::CompressEof(remain) => write!(f, "compression overflow with {remain} bytes of input remaining"), + Self::CompressStall => write!(f, "compressor stalled before completion"), } } } @@ -1090,9 +1090,9 @@ impl Error for WriteError { match self { - WriteError::Write(e) => Some(e), - WriteError::StateSerialize(e) => Some(e), - WriteError::Compress(e) => Some(e), + Self::Write(e) => Some(e), + Self::StateSerialize(e) => Some(e), + Self::Compress(e) => Some(e), _ => None, } } @@ -1154,8 +1154,8 @@ impl fmt::Display for R64Error { match self { - R64Error::Base64(e) => e.fmt(f), - R64Error::Content(e) => e.fmt(f), + Self::Base64(e) => e.fmt(f), + Self::Content(e) => e.fmt(f), } } } @@ -1166,8 +1166,8 @@ impl Error for R64Error { match self { - R64Error::Base64(e) => Some(e), - R64Error::Content(e) => Some(e), + Self::Base64(e) => Some(e), + Self::Content(e) => Some(e), } } } @@ -1201,8 +1201,8 @@ impl fmt::Display for W64Error { match self { - W64Error::Base64(e) => e.fmt(f), - W64Error::Content(e) => e.fmt(f), + Self::Base64(e) => e.fmt(f), + Self::Content(e) => e.fmt(f), } } } @@ -1213,8 +1213,8 @@ impl Error for W64Error { match self { - W64Error::Base64(e) => Some(e), - W64Error::Content(e) => Some(e), + Self::Base64(e) => Some(e), + Self::Content(e) => Some(e), } } } diff --git a/src/exe/args.rs b/src/exe/args.rs index e2ba8ef..1c83c63 100644 --- a/src/exe/args.rs +++ b/src/exe/args.rs @@ -28,8 +28,8 @@ impl<E: error::Error + 'static> fmt::Display for Error<E> { match self { - Error::Handler{pos, val} => write!(f, "{val} (at #{pos})"), - Error::EmptyName{pos} => write!(f, "malformed argument (at #{pos})"), + Self::Handler{pos, val} => write!(f, "{val} (at #{pos})"), + Self::EmptyName{pos} => write!(f, "malformed argument (at #{pos})"), } } } @@ -40,7 +40,7 @@ impl<E: error::Error + 'static> error::Error for Error<E> { match self { - Error::Handler{pos: _, val} => Some(val), + Self::Handler{pos: _, val} => Some(val), _ => None, } } @@ -122,7 +122,7 @@ impl ArgCount { match self { - ArgCount::Optional(..) | ArgCount::Required(..) => true, + Self::Optional(..) | ArgCount::Required(..) => true, _ => false } } @@ -131,7 +131,7 @@ impl ArgCount { match self { - ArgCount::Required(..) => true, + Self::Required(..) => true, _ => false } } @@ -140,7 +140,7 @@ impl ArgCount { match self { - ArgCount::Optional(max) | ArgCount::Required(max) => Some(*max), + Self::Optional(max) | ArgCount::Required(max) => Some(*max), _ => None, } } @@ -222,7 +222,7 @@ impl OptionValue { match self { - OptionValue::Absent => true, + Self::Absent => true, _ => false, } } @@ -231,7 +231,7 @@ impl OptionValue { match self { - OptionValue::Present | OptionValue::Value(..) | OptionValue::Values(..) => true, + Self::Present | Self::Value(..) | Self::Values(..) => true, _ => false, } } @@ -240,8 +240,8 @@ impl OptionValue { match self { - OptionValue::Value(..) => true, - OptionValue::Values(..) => true, + Self::Value(..) => true, + Self::Values(..) => true, _ => false, } } @@ -250,7 +250,7 @@ impl OptionValue { match self { - OptionValue::Value(v) => Some(v), + Self::Value(v) => Some(v), _ => None, } } @@ -259,8 +259,8 @@ impl OptionValue { match self { - OptionValue::Value(v) => Some(from_ref(v)), - OptionValue::Values(v) => Some(v.as_ref()), + Self::Value(v) => Some(from_ref(v)), + Self::Values(v) => Some(v.as_ref()), _ => None, } } @@ -498,11 +498,11 @@ impl fmt::Display for OptionError { match self { - OptionError::NoSuchShort(short) => write!(f, "invalid argument \"-{short}\""), - OptionError::NoSuchLong(long) => write!(f, "invalid argument \"--{long}\""), - OptionError::ValueForbidden(opt) => write!(f, "argument {opt} has no value"), - OptionError::ValueRequired(opt) => write!(f, "argument {opt} requires a value"), - OptionError::TooMany(opt) => + Self::NoSuchShort(short) => write!(f, "invalid argument \"-{short}\""), + Self::NoSuchLong(long) => write!(f, "invalid argument \"--{long}\""), + Self::ValueForbidden(opt) => write!(f, "argument {opt} has no value"), + Self::ValueRequired(opt) => write!(f, "argument {opt} requires a value"), + Self::TooMany(opt) => { if let Some(max) = opt.count.get_max_count() {write!(f, "too many {opt} (max {max})")} else {write!(f, "duplicate argument {opt}")} |