diff --git a/protocol/src/format.rs b/protocol/src/format.rs index 50dc3f9..49c12ba 100644 --- a/protocol/src/format.rs +++ b/protocol/src/format.rs @@ -18,19 +18,25 @@ use std::mem; #[derive(Debug, Clone)] pub enum Component { - Text(TextComponent) + Text(TextComponent), } impl Component { pub fn from_value(v: &serde_json::Value) -> Self { let mut modifier = Modifier::from_value(v); if let Some(val) = v.as_string() { - Component::Text(TextComponent{text: val.to_owned(), modifier: modifier}) - } else if v.find("text").is_some(){ + Component::Text(TextComponent { + text: val.to_owned(), + modifier: modifier, + }) + } else if v.find("text").is_some() { Component::Text(TextComponent::from_value(v, modifier)) } else { modifier.color = Some(Color::RGB(255, 0, 0)); - Component::Text(TextComponent{text: "UNHANDLED".to_owned(), modifier: modifier}) + Component::Text(TextComponent { + text: "UNHANDLED".to_owned(), + modifier: modifier, + }) } } @@ -49,7 +55,10 @@ impl fmt::Display for Component { impl Default for Component { fn default() -> Self { - Component::Text(TextComponent{text: "".to_owned(), modifier: Default::default()}) + Component::Text(TextComponent { + text: "".to_owned(), + modifier: Default::default(), + }) } } @@ -62,12 +71,10 @@ pub struct Modifier { pub strikethrough: Option, pub obfuscated: Option, pub color: Option, - - // click_event - // hover_event - // insertion } +// TODO: Missing events click/hover/insert + impl Modifier { pub fn from_value(v: &serde_json::Value) -> Self { let mut m = Modifier { @@ -76,7 +83,9 @@ impl Modifier { underlined: v.find("underlined").map_or(Option::None, |v| v.as_boolean()), strikethrough: v.find("strikethrough").map_or(Option::None, |v| v.as_boolean()), obfuscated: v.find("obfuscated").map_or(Option::None, |v| v.as_boolean()), - color: v.find("color").map_or(Option::None, |v| v.as_string()).map(|v| Color::from_string(&v.to_owned())), + color: v.find("color") + .map_or(Option::None, |v| v.as_string()) + .map(|v| Color::from_string(&v.to_owned())), extra: Option::None, }; if let Some(extra) = v.find("extra") { @@ -106,9 +115,7 @@ impl TextComponent { pub fn new(val: &str) -> TextComponent { TextComponent { text: val.to_owned(), - modifier: Modifier { - .. Default::default() - } + modifier: Modifier { ..Default::default() }, } } @@ -154,7 +161,7 @@ pub enum Color { LightPurple, Yellow, White, - RGB(u8, u8, u8) + RGB(u8, u8, u8), } impl fmt::Display for Color { @@ -195,11 +202,7 @@ impl Color { Ok(b) => b, Err(_) => return Color::White, }; - Color::RGB( - r, - g, - b - ) + Color::RGB(r, g, b) } _ => Color::White, } @@ -229,22 +232,22 @@ impl Color { pub fn to_rgb(&self) -> (u8, u8, u8) { match *self { - Color::Black =>(0, 0, 0), - Color::DarkBlue =>(0, 0, 170), - Color::DarkGreen =>(0, 170, 0), - Color::DarkAqua =>(0, 170, 170), - Color::DarkRed =>(170, 0, 0), - Color::DarkPurple =>(170, 0, 170), - Color::Gold =>(255, 170, 0), - Color::Gray =>(170, 170, 170), - Color::DarkGray =>(85, 85, 85), - Color::Blue =>(85, 85, 255), - Color::Green =>(85, 255, 85), - Color::Aqua =>(85, 255, 255), - Color::Red =>(255, 85, 85), - Color::LightPurple =>(255, 85, 255), - Color::Yellow =>(255, 255, 85), - Color::White =>(255, 255, 255), + Color::Black => (0, 0, 0), + Color::DarkBlue => (0, 0, 170), + Color::DarkGreen => (0, 170, 0), + Color::DarkAqua => (0, 170, 170), + Color::DarkRed => (170, 0, 0), + Color::DarkPurple => (170, 0, 170), + Color::Gold => (255, 170, 0), + Color::Gray => (170, 170, 170), + Color::DarkGray => (85, 85, 85), + Color::Blue => (85, 85, 255), + Color::Green => (85, 255, 85), + Color::Aqua => (85, 255, 255), + Color::Red => (255, 85, 85), + Color::LightPurple => (255, 85, 255), + Color::Yellow => (255, 255, 85), + Color::White => (255, 255, 255), Color::RGB(r, g, b) => (r, g, b), } } @@ -264,12 +267,12 @@ fn test_color_from() { } let test = Color::from_string(&"red".to_owned()); match test { - Color::Red => {}, + Color::Red => {} _ => panic!("Wrong type"), } let test = Color::from_string(&"dark_blue".to_owned()); match test { - Color::DarkBlue => {}, + Color::DarkBlue => {} _ => panic!("Wrong type"), } } @@ -297,10 +300,11 @@ pub fn convert_legacy(c: &mut Component) { None => break, }; let color_char = next.1.to_lowercase().next().unwrap(); - current.text = txt.text[last .. i].to_owned(); + current.text = txt.text[last..i].to_owned(); last = next.0 + 1; - let mut modifier = if (color_char >= 'a' && color_char <= 'f') || (color_char >= '0' && color_char <= '9') { + let mut modifier = if (color_char >= 'a' && color_char <= 'f') || + (color_char >= '0' && color_char <= '9') { Default::default() } else { current.modifier.clone() @@ -331,7 +335,7 @@ pub fn convert_legacy(c: &mut Component) { 'm' => modifier.strikethrough = Some(true), 'n' => modifier.underlined = Some(true), 'o' => modifier.italic = Some(true), - 'r' => {}, + 'r' => {} _ => unimplemented!(), } @@ -352,6 +356,6 @@ pub fn convert_legacy(c: &mut Component) { } txt.text = "".to_owned(); } - }, + } } -} \ No newline at end of file +} diff --git a/protocol/src/item.rs b/protocol/src/item.rs index 5f143ad..a6ab829 100644 --- a/protocol/src/item.rs +++ b/protocol/src/item.rs @@ -13,7 +13,7 @@ // limitations under the License. use nbt; -use protocol::{Serializable}; +use protocol::Serializable; use std::io; use std::io::{Read, Write}; use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt}; @@ -44,7 +44,7 @@ impl Serializable for Option { if id == -1 { return Ok(None); } - Ok(Some(Stack{ + Ok(Some(Stack { id: id as isize, count: try!(buf.read_u8()) as isize, damage: try!(buf.read_i16::()) as isize, @@ -58,7 +58,7 @@ impl Serializable for Option { try!(buf.write_u8(val.count as u8)); try!(buf.write_i16::(val.damage as i16)); try!(val.tag.write_to(buf)); - }, + } None => try!(buf.write_i16::(-1)), } Result::Ok(()) diff --git a/protocol/src/nbt/mod.rs b/protocol/src/nbt/mod.rs index 4100448..32d4a64 100644 --- a/protocol/src/nbt/mod.rs +++ b/protocol/src/nbt/mod.rs @@ -16,7 +16,7 @@ use std::collections::HashMap; use std::io; use std::io::{Read, Write}; -use super::protocol::{Serializable}; +use super::protocol::Serializable; use super::protocol; use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt}; @@ -173,52 +173,53 @@ impl Tag { } fn read_type(id: u8, buf: &mut io::Read) -> Result { - match id { - 0 => unreachable!(), - 1 => Ok(Tag::Byte(try!(buf.read_i8()))), - 2 => Ok(Tag::Short(try!(buf.read_i16::()))), - 3 => Ok(Tag::Int(try!(buf.read_i32::()))), - 4 => Ok(Tag::Long(try!(buf.read_i64::()))), - 5 => Ok(Tag::Float(try!(buf.read_f32::()))), - 6 => Ok(Tag::Double(try!(buf.read_f64::()))), - 7 => Ok(Tag::ByteArray({ - let len : i32 = try!(Serializable::read_from(buf)); - let mut data = Vec::with_capacity(len as usize); - try!(buf.take(len as u64).read_to_end(&mut data)); - data - })), - 8 => Ok(Tag::String(try!(read_string(buf)))), - 9 => { - let mut l = Vec::new(); - let ty = try!(buf.read_u8()); - let len : i32 = try!(Serializable::read_from(buf)); - for _ in 0 .. len { - l.push(try!(Tag::read_type(ty, buf))); - } - Ok(Tag::List(l)) - }, - 10 => { - let mut c = Tag::new_compound(); - loop { - let ty = try!(buf.read_u8()); - if ty == 0 { - break; - } - let name: String = try!(read_string(buf)); - c.put(&name[..], try!(Tag::read_type(ty, buf))); - } - Ok(c) - }, - 11 => Ok(Tag::IntArray({ - let len : i32 = try!(Serializable::read_from(buf)); - let mut data = Vec::with_capacity(len as usize); - for _ in 0 .. len { - data.push(try!(buf.read_i32::())); - } - data - })), - _ => Err(io::Error::new(io::ErrorKind::InvalidData, protocol::Error::Err("invalid tag".to_owned()))), + match id { + 0 => unreachable!(), + 1 => Ok(Tag::Byte(try!(buf.read_i8()))), + 2 => Ok(Tag::Short(try!(buf.read_i16::()))), + 3 => Ok(Tag::Int(try!(buf.read_i32::()))), + 4 => Ok(Tag::Long(try!(buf.read_i64::()))), + 5 => Ok(Tag::Float(try!(buf.read_f32::()))), + 6 => Ok(Tag::Double(try!(buf.read_f64::()))), + 7 => Ok(Tag::ByteArray({ + let len: i32 = try!(Serializable::read_from(buf)); + let mut data = Vec::with_capacity(len as usize); + try!(buf.take(len as u64).read_to_end(&mut data)); + data + })), + 8 => Ok(Tag::String(try!(read_string(buf)))), + 9 => { + let mut l = Vec::new(); + let ty = try!(buf.read_u8()); + let len: i32 = try!(Serializable::read_from(buf)); + for _ in 0..len { + l.push(try!(Tag::read_type(ty, buf))); + } + Ok(Tag::List(l)) } + 10 => { + let mut c = Tag::new_compound(); + loop { + let ty = try!(buf.read_u8()); + if ty == 0 { + break; + } + let name: String = try!(read_string(buf)); + c.put(&name[..], try!(Tag::read_type(ty, buf))); + } + Ok(c) + } + 11 => Ok(Tag::IntArray({ + let len: i32 = try!(Serializable::read_from(buf)); + let mut data = Vec::with_capacity(len as usize); + for _ in 0..len { + data.push(try!(buf.read_i32::())); + } + data + })), + _ => Err(io::Error::new(io::ErrorKind::InvalidData, + protocol::Error::Err("invalid tag".to_owned()))), + } } } @@ -229,7 +230,7 @@ impl Serializable for Tag { fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { match *self { - Tag::End => {}, + Tag::End => {} Tag::Byte(val) => try!(buf.write_i8(val)), Tag::Short(val) => try!(buf.write_i16::(val)), Tag::Int(val) => try!(buf.write_i32::(val)), @@ -239,20 +240,20 @@ impl Serializable for Tag { Tag::ByteArray(ref val) => { try!((val.len() as i32).write_to(buf)); try!(buf.write_all(val)); - }, + } Tag::String(ref val) => try!(write_string(buf, val)), Tag::List(ref val) => { - if val.is_empty() { - try!(buf.write_u8(0)); - try!(buf.write_i32::(0)); - } else { - try!(buf.write_u8(val[0].internal_id())); - try!(buf.write_i32::(val.len() as i32)); - for e in val { - try!(e.write_to(buf)); - } + if val.is_empty() { + try!(buf.write_u8(0)); + try!(buf.write_i32::(0)); + } else { + try!(buf.write_u8(val[0].internal_id())); + try!(buf.write_i32::(val.len() as i32)); + for e in val { + try!(e.write_to(buf)); } - }, + } + } Tag::Compound(ref val) => { for (k, v) in val { try!(v.internal_id().write_to(buf)); @@ -260,19 +261,19 @@ impl Serializable for Tag { try!(v.write_to(buf)); } try!(buf.write_u8(0)); - }, + } Tag::IntArray(ref val) => { try!((val.len() as i32).write_to(buf)); for v in val { try!(v.write_to(buf)); } - }, + } } Result::Ok(()) } } -pub fn write_string(buf: &mut io::Write, s: &String)-> io::Result<()> { +pub fn write_string(buf: &mut io::Write, s: &String) -> io::Result<()> { let data = s.as_bytes(); try!((data.len() as i16).write_to(buf)); buf.write_all(data) diff --git a/protocol/src/types/bit/map.rs b/protocol/src/types/bit/map.rs index b1b9e05..4814f2a 100644 --- a/protocol/src/types/bit/map.rs +++ b/protocol/src/types/bit/map.rs @@ -15,14 +15,14 @@ pub struct Map { bits: Vec, bit_size: usize, - length: usize + length: usize, } #[test] fn test_map() { let mut map = Map::new(4096, 4); - for i in 0 .. 4096 { - for j in 0 .. 16 { + for i in 0..4096 { + for j in 0..16 { map.set(i, j); if map.get(i) != j { panic!("Fail"); @@ -33,11 +33,11 @@ fn test_map() { #[test] fn test_map_odd() { - for size in 1 .. 16 { - let mut map = Map::new(64*3, size); + for size in 1..16 { + let mut map = Map::new(64 * 3, size); let max = (1 << size) - 1; - for i in 0 .. 64*3 { - for j in 0 .. max { + for i in 0..64 * 3 { + for j in 0..max { map.set(i, j); if map.get(i) != j { panic!("Index: {} wanted {} and got {}", i, j, map.get(i)); @@ -52,9 +52,9 @@ impl Map { let mut map = Map { bit_size: size, length: len, - bits: Vec::with_capacity((len*size)/64) + bits: Vec::with_capacity((len * size) / 64), }; - for _ in 0 .. len { + for _ in 0..len { map.bits.push(0) } map @@ -62,7 +62,7 @@ impl Map { pub fn resize(self, size: usize) -> Map { let mut n = Map::new(self.length, size); - for i in 0 .. self.length { + for i in 0..self.length { n.set(i, self.get(i)); } n @@ -73,7 +73,7 @@ impl Map { let pos = i / 64; let mask = (1 << self.bit_size) - 1; let ii = i % 64; - self.bits[pos] = (self.bits[pos] & !(mask << ii )) | ((val << ii) as u64); + self.bits[pos] = (self.bits[pos] & !(mask << ii)) | ((val << ii) as u64); let pos2 = (i + self.bit_size - 1) / 64; if pos2 != pos { let used = 64 - ii; diff --git a/protocol/src/types/bit/set.rs b/protocol/src/types/bit/set.rs index e74f6c5..123b0f9 100644 --- a/protocol/src/types/bit/set.rs +++ b/protocol/src/types/bit/set.rs @@ -13,19 +13,19 @@ // limitations under the License. pub struct Set { - data : Vec + data: Vec, } #[test] fn test_set() { let mut set = Set::new(200); - for i in 0 .. 200 { + for i in 0..200 { if i % 3 == 0 { set.set(i, true) } } - for i in 0 .. 200 { - if set.get(i) != (i%3 == 0) { + for i in 0..200 { + if set.get(i) != (i % 3 == 0) { panic!("Fail") } } @@ -33,10 +33,8 @@ fn test_set() { impl Set { pub fn new(size: usize) -> Set { - let mut set = Set { - data: Vec::with_capacity(size) - }; - for _ in 0 .. size { + let mut set = Set { data: Vec::with_capacity(size) }; + for _ in 0..size { set.data.push(0) } set @@ -44,13 +42,13 @@ impl Set { pub fn set(&mut self, i: usize, v: bool) { if v { - self.data[i>>6] |= 1 << (i & 0x3F) + self.data[i >> 6] |= 1 << (i & 0x3F) } else { - self.data[i>>6] &= !(1 << (i & 0x3F)) + self.data[i >> 6] &= !(1 << (i & 0x3F)) } } pub fn get(&mut self, i: usize) -> bool { - (self.data[i>>6] & (1 << (i & 0x3F))) != 0 + (self.data[i >> 6] & (1 << (i & 0x3F))) != 0 } } diff --git a/protocol/src/types/blockpos.rs b/protocol/src/types/blockpos.rs index cc38604..8455811 100644 --- a/protocol/src/types/blockpos.rs +++ b/protocol/src/types/blockpos.rs @@ -15,7 +15,7 @@ extern crate byteorder; use std::fmt; -use protocol::{Serializable}; +use protocol::Serializable; use std::io; use std::io::{Read, Write}; use self::byteorder::{BigEndian, WriteBytesExt, ReadBytesExt}; @@ -26,11 +26,8 @@ pub struct Position(u64); impl Position { #[allow(dead_code)] fn new(x: i32, y: i32, z: i32) -> Position { - Position( - (((x as u64) & 0x3FFFFFF) << 38) | - (((y as u64) & 0xFFF) << 26) | - ((z as u64) & 0x3FFFFFF) - ) + Position((((x as u64) & 0x3FFFFFF) << 38) | (((y as u64) & 0xFFF) << 26) | + ((z as u64) & 0x3FFFFFF)) } fn get_x(&self) -> i32 { diff --git a/protocol/src/types/metadata.rs b/protocol/src/types/metadata.rs index f4281b6..b7443f3 100644 --- a/protocol/src/types/metadata.rs +++ b/protocol/src/types/metadata.rs @@ -18,7 +18,7 @@ use std::io; use std::io::{Read, Write}; use std::fmt; use protocol; -use protocol::{Serializable}; +use protocol::Serializable; use format; use item; @@ -30,7 +30,7 @@ pub struct MetadataKey { impl MetadataKey { #[allow(dead_code)] // TODO: Make const later when possible - /*const*/ fn new(index: i32) -> MetadataKey { + /*const*/ fn new(index: i32) -> MetadataKey { MetadataKey { index: index, ty: PhantomData, @@ -78,11 +78,10 @@ impl Serializable for Metadata { 4 => m.put_raw(index, try!(format::Component::read_from(buf))), 5 => m.put_raw(index, try!(Option::::read_from(buf))), 6 => m.put_raw(index, try!(bool::read_from(buf))), - 7 => m.put_raw(index, [ - try!(f32::read_from(buf)), - try!(f32::read_from(buf)), - try!(f32::read_from(buf)) - ]), + 7 => m.put_raw(index, + [try!(f32::read_from(buf)), + try!(f32::read_from(buf)), + try!(f32::read_from(buf))]), 8 => m.put_raw(index, try!(super::Position::read_from(buf))), 9 => { if try!(bool::read_from(buf)) { @@ -90,7 +89,7 @@ impl Serializable for Metadata { } else { m.put_raw::>(index, None); } - }, + } 10 => m.put_raw(index, try!(protocol::VarInt::read_from(buf))), 11 => { if try!(bool::read_from(buf)) { @@ -98,9 +97,11 @@ impl Serializable for Metadata { } else { m.put_raw::>(index, None); } - }, + } 12 => m.put_raw(index, try!(protocol::VarInt::read_from(buf)).0 as u16), - _ => return Err(io::Error::new(io::ErrorKind::InvalidInput, protocol::Error::Err("unknown metadata type".to_owned()))), + _ => return Err(io::Error::new(io::ErrorKind::InvalidInput, + protocol::Error::Err("unknown metadata type" + .to_owned()))), } } Ok(m) @@ -113,46 +114,46 @@ impl Serializable for Metadata { Value::Byte(ref val) => { try!(u8::write_to(&0, buf)); try!(val.write_to(buf)); - }, + } Value::Int(ref val) => { try!(u8::write_to(&1, buf)); try!(protocol::VarInt(*val).write_to(buf)); - }, + } Value::Float(ref val) => { try!(u8::write_to(&2, buf)); try!(val.write_to(buf)); - }, + } Value::String(ref val) => { try!(u8::write_to(&3, buf)); try!(val.write_to(buf)); - }, + } Value::FormatComponent(ref val) => { try!(u8::write_to(&4, buf)); try!(val.write_to(buf)); - }, + } Value::OptionalItemStack(ref val) => { try!(u8::write_to(&5, buf)); try!(val.write_to(buf)); - }, + } Value::Bool(ref val) => { try!(u8::write_to(&6, buf)); try!(val.write_to(buf)); - }, + } Value::Vector(ref val) => { try!(u8::write_to(&7, buf)); try!(val[0].write_to(buf)); try!(val[1].write_to(buf)); try!(val[2].write_to(buf)); - }, + } Value::Position(ref val) => { try!(u8::write_to(&8, buf)); try!(val.write_to(buf)); - }, + } Value::OptionalPosition(ref val) => { try!(u8::write_to(&9, buf)); try!(val.is_some().write_to(buf)); try!(val.write_to(buf)); - }, + } Value::Direction(ref val) => { try!(u8::write_to(&10, buf)); try!(val.write_to(buf)); @@ -161,7 +162,7 @@ impl Serializable for Metadata { try!(u8::write_to(&11, buf)); try!(val.is_some().write_to(buf)); try!(val.write_to(buf)); - }, + } Value::Block(ref val) => { try!(u8::write_to(&11, buf)); try!(protocol::VarInt(*val as i32).write_to(buf)); @@ -374,9 +375,9 @@ mod test { const TEST: MetadataKey = MetadataKey { - index: 0, - ty: PhantomData, - }; + index: 0, + ty: PhantomData, + }; #[test] fn basic() { diff --git a/protocol/src/types/mod.rs b/protocol/src/types/mod.rs index 034015f..ca88128 100644 --- a/protocol/src/types/mod.rs +++ b/protocol/src/types/mod.rs @@ -18,4 +18,4 @@ pub use self::blockpos::*; mod metadata; pub use self::metadata::*; -pub mod bit; \ No newline at end of file +pub mod bit;