Reformat using rustfmt

This commit is contained in:
Thinkofdeath 2015-10-07 19:36:59 +01:00
parent dc810c15dd
commit 35306c62e1
8 changed files with 160 additions and 159 deletions

View File

@ -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<bool>,
pub obfuscated: Option<bool>,
pub color: Option<Color>,
// 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();
}
},
}
}
}
}

View File

@ -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<Stack> {
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::<BigEndian>()) as isize,
@ -58,7 +58,7 @@ impl Serializable for Option<Stack> {
try!(buf.write_u8(val.count as u8));
try!(buf.write_i16::<BigEndian>(val.damage as i16));
try!(val.tag.write_to(buf));
},
}
None => try!(buf.write_i16::<BigEndian>(-1)),
}
Result::Ok(())

View File

@ -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<Tag, io::Error> {
match id {
0 => unreachable!(),
1 => Ok(Tag::Byte(try!(buf.read_i8()))),
2 => Ok(Tag::Short(try!(buf.read_i16::<BigEndian>()))),
3 => Ok(Tag::Int(try!(buf.read_i32::<BigEndian>()))),
4 => Ok(Tag::Long(try!(buf.read_i64::<BigEndian>()))),
5 => Ok(Tag::Float(try!(buf.read_f32::<BigEndian>()))),
6 => Ok(Tag::Double(try!(buf.read_f64::<BigEndian>()))),
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::<BigEndian>()));
}
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::<BigEndian>()))),
3 => Ok(Tag::Int(try!(buf.read_i32::<BigEndian>()))),
4 => Ok(Tag::Long(try!(buf.read_i64::<BigEndian>()))),
5 => Ok(Tag::Float(try!(buf.read_f32::<BigEndian>()))),
6 => Ok(Tag::Double(try!(buf.read_f64::<BigEndian>()))),
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::<BigEndian>()));
}
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::<BigEndian>(val)),
Tag::Int(val) => try!(buf.write_i32::<BigEndian>(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::<BigEndian>(0));
} else {
try!(buf.write_u8(val[0].internal_id()));
try!(buf.write_i32::<BigEndian>(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::<BigEndian>(0));
} else {
try!(buf.write_u8(val[0].internal_id()));
try!(buf.write_i32::<BigEndian>(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)

View File

@ -15,14 +15,14 @@
pub struct Map {
bits: Vec<u64>,
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;

View File

@ -13,19 +13,19 @@
// limitations under the License.
pub struct Set {
data : Vec<u64>
data: Vec<u64>,
}
#[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
}
}

View File

@ -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 {

View File

@ -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<T: MetaValue> {
impl <T: MetaValue> MetadataKey<T> {
#[allow(dead_code)]
// TODO: Make const later when possible
/*const*/ fn new(index: i32) -> MetadataKey<T> {
/*const*/ fn new(index: i32) -> MetadataKey<T> {
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::<item::Stack>::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::<Option<super::Position>>(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::<Option<protocol::UUID>>(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<String> =
MetadataKey {
index: 0,
ty: PhantomData,
};
index: 0,
ty: PhantomData,
};
#[test]
fn basic() {

View File

@ -18,4 +18,4 @@ pub use self::blockpos::*;
mod metadata;
pub use self::metadata::*;
pub mod bit;
pub mod bit;