Tabs to spaces

This commit is contained in:
Thinkofdeath 2015-09-10 11:58:42 +01:00
parent fe6a56ce3a
commit 25a033268c
1 changed files with 201 additions and 201 deletions

View File

@ -4,251 +4,251 @@ use std::fmt;
#[derive(Debug)] #[derive(Debug)]
pub enum Component { pub enum Component {
Text(TextComponent), Text(TextComponent),
None None
} }
impl Component { impl Component {
pub fn from_value(v: &serde_json::Value) -> Self { pub fn from_value(v: &serde_json::Value) -> Self {
let modifier = Modifier::from_value(v); let modifier = Modifier::from_value(v);
if let Some(val) = v.as_string() { if let Some(val) = v.as_string() {
Component::Text(TextComponent{text: val.to_owned(), modifier: modifier}) Component::Text(TextComponent{text: val.to_owned(), modifier: modifier})
} else if v.find("text").is_some(){ } else if v.find("text").is_some(){
Component::Text(TextComponent::from_value(v, modifier)) Component::Text(TextComponent::from_value(v, modifier))
} else { } else {
Component::None Component::None
} }
} }
pub fn to_value(&self) -> serde_json::Value { pub fn to_value(&self) -> serde_json::Value {
unimplemented!() unimplemented!()
} }
} }
impl fmt::Display for Component { impl fmt::Display for Component {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Component::Text(ref txt) => write!(f, "{}", txt), Component::Text(ref txt) => write!(f, "{}", txt),
Component::None => Result::Ok(()), Component::None => Result::Ok(()),
} }
} }
} }
impl Default for Component { impl Default for Component {
fn default() -> Self { fn default() -> Self {
Component::None Component::None
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Modifier { pub struct Modifier {
pub extra: Option<Vec<Component>>, pub extra: Option<Vec<Component>>,
pub bold: Option<bool>, pub bold: Option<bool>,
pub italic: Option<bool>, pub italic: Option<bool>,
pub underlined: Option<bool>, pub underlined: Option<bool>,
pub strikethrough: Option<bool>, pub strikethrough: Option<bool>,
pub obfuscated: Option<bool>, pub obfuscated: Option<bool>,
pub color: Option<Color>, pub color: Option<Color>,
// click_event // click_event
// hover_event // hover_event
// insertion // insertion
} }
impl Modifier { impl Modifier {
pub fn from_value(v: &serde_json::Value) -> Self { pub fn from_value(v: &serde_json::Value) -> Self {
let mut m = Modifier { let mut m = Modifier {
bold: v.find("bold").map_or(Option::None, |v| v.as_boolean()), bold: v.find("bold").map_or(Option::None, |v| v.as_boolean()),
italic: v.find("italic").map_or(Option::None, |v| v.as_boolean()), italic: v.find("italic").map_or(Option::None, |v| v.as_boolean()),
underlined: v.find("underlined").map_or(Option::None, |v| v.as_boolean()), underlined: v.find("underlined").map_or(Option::None, |v| v.as_boolean()),
strikethrough: v.find("strikethrough").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()), 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, extra: Option::None,
}; };
if let Some(extra) = v.find("extra") { if let Some(extra) = v.find("extra") {
if let Some(data) = extra.as_array() { if let Some(data) = extra.as_array() {
let mut ex = Vec::new(); let mut ex = Vec::new();
for e in data { for e in data {
ex.push(Component::from_value(e)); ex.push(Component::from_value(e));
} }
m.extra = Some(ex); m.extra = Some(ex);
} }
} }
m m
} }
pub fn to_value(&self) -> serde_json::Value { pub fn to_value(&self) -> serde_json::Value {
unimplemented!() unimplemented!()
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct TextComponent { pub struct TextComponent {
pub text: String, pub text: String,
pub modifier: Modifier, pub modifier: Modifier,
} }
impl TextComponent { impl TextComponent {
pub fn from_value(v: &serde_json::Value, modifier: Modifier) -> Self { pub fn from_value(v: &serde_json::Value, modifier: Modifier) -> Self {
TextComponent { TextComponent {
text: v.find("text").unwrap().as_string().unwrap_or("").to_owned(), text: v.find("text").unwrap().as_string().unwrap_or("").to_owned(),
modifier: modifier, modifier: modifier,
} }
} }
pub fn to_value(&self) -> serde_json::Value { pub fn to_value(&self) -> serde_json::Value {
unimplemented!() unimplemented!()
} }
} }
impl fmt::Display for TextComponent { impl fmt::Display for TextComponent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}", self.text)); try!(write!(f, "{}", self.text));
if let Some(ref extra) = self.modifier.extra { if let Some(ref extra) = self.modifier.extra {
for c in extra { for c in extra {
try!(write!(f, "{}", c)); try!(write!(f, "{}", c));
} }
} }
Result::Ok(()) Result::Ok(())
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub enum Color { pub enum Color {
Black, Black,
DarkBlue, DarkBlue,
DarkGreen, DarkGreen,
DarkAqua, DarkAqua,
DarkRed, DarkRed,
DarkPurple, DarkPurple,
Gold, Gold,
Gray, Gray,
DarkGray, DarkGray,
Blue, Blue,
Green, Green,
Aqua, Aqua,
Red, Red,
LightPurple, LightPurple,
Yellow, Yellow,
White, White,
RGB(u8, u8, u8) RGB(u8, u8, u8)
} }
impl fmt::Display for Color { impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_string()) write!(f, "{}", self.to_string())
} }
} }
impl Color { impl Color {
fn from_string(val: &String) -> Self { fn from_string(val: &String) -> Self {
match val.as_ref() { match val.as_ref() {
"black" => Color::Black, "black" => Color::Black,
"dark_blue" => Color::DarkBlue, "dark_blue" => Color::DarkBlue,
"dark_green" => Color::DarkGreen, "dark_green" => Color::DarkGreen,
"dark_aqua" => Color::DarkAqua, "dark_aqua" => Color::DarkAqua,
"dark_red" => Color::DarkRed, "dark_red" => Color::DarkRed,
"dark_purple" => Color::DarkPurple, "dark_purple" => Color::DarkPurple,
"gold" => Color::Gold, "gold" => Color::Gold,
"gray" => Color::Gray, "gray" => Color::Gray,
"dark_gray" => Color::DarkGray, "dark_gray" => Color::DarkGray,
"blue" => Color::Blue, "blue" => Color::Blue,
"green" => Color::Green, "green" => Color::Green,
"aqua" => Color::Aqua, "aqua" => Color::Aqua,
"red" => Color::Red, "red" => Color::Red,
"light_purple" => Color::LightPurple, "light_purple" => Color::LightPurple,
"yellow" => Color::Yellow, "yellow" => Color::Yellow,
"white" => Color::White, "white" => Color::White,
val if val.len() == 7 && val.as_bytes()[0] == b'#' => { val if val.len() == 7 && val.as_bytes()[0] == b'#' => {
let r = match u8::from_str_radix(&val[1..3], 16) { let r = match u8::from_str_radix(&val[1..3], 16) {
Ok(r) => r, Ok(r) => r,
Err(_) => return Color::White, Err(_) => return Color::White,
}; };
let g = match u8::from_str_radix(&val[3..5], 16) { let g = match u8::from_str_radix(&val[3..5], 16) {
Ok(g) => g, Ok(g) => g,
Err(_) => return Color::White, Err(_) => return Color::White,
}; };
let b = match u8::from_str_radix(&val[5..7], 16) { let b = match u8::from_str_radix(&val[5..7], 16) {
Ok(b) => b, Ok(b) => b,
Err(_) => return Color::White, Err(_) => return Color::White,
}; };
Color::RGB( Color::RGB(
r, r,
g, g,
b b
) )
} }
_ => Color::White, _ => Color::White,
} }
} }
fn to_string(&self) -> String { fn to_string(&self) -> String {
match *self { match *self {
Color::Black => "black".to_owned(), Color::Black => "black".to_owned(),
Color::DarkBlue => "dark_blue".to_owned(), Color::DarkBlue => "dark_blue".to_owned(),
Color::DarkGreen => "dark_green".to_owned(), Color::DarkGreen => "dark_green".to_owned(),
Color::DarkAqua => "dark_aqua".to_owned(), Color::DarkAqua => "dark_aqua".to_owned(),
Color::DarkRed => "dark_red".to_owned(), Color::DarkRed => "dark_red".to_owned(),
Color::DarkPurple => "dark_purple".to_owned(), Color::DarkPurple => "dark_purple".to_owned(),
Color::Gold => "gold".to_owned(), Color::Gold => "gold".to_owned(),
Color::Gray => "gray".to_owned(), Color::Gray => "gray".to_owned(),
Color::DarkGray => "dark_gray".to_owned(), Color::DarkGray => "dark_gray".to_owned(),
Color::Blue => "blue".to_owned(), Color::Blue => "blue".to_owned(),
Color::Green => "green".to_owned(), Color::Green => "green".to_owned(),
Color::Aqua => "aqua".to_owned(), Color::Aqua => "aqua".to_owned(),
Color::Red => "red".to_owned(), Color::Red => "red".to_owned(),
Color::LightPurple => "light_purple".to_owned(), Color::LightPurple => "light_purple".to_owned(),
Color::Yellow => "yellow".to_owned(), Color::Yellow => "yellow".to_owned(),
Color::White => "white".to_owned(), Color::White => "white".to_owned(),
Color::RGB(r, g, b) => format!("#{:02X}{:02X}{:02X}", r, g, b), Color::RGB(r, g, b) => format!("#{:02X}{:02X}{:02X}", r, g, b),
} }
} }
#[allow(dead_code)] #[allow(dead_code)]
fn to_rgb(&self) -> (u8, u8, u8) { fn to_rgb(&self) -> (u8, u8, u8) {
match *self { match *self {
Color::Black =>(0, 0, 0), Color::Black =>(0, 0, 0),
Color::DarkBlue =>(0, 0, 170), Color::DarkBlue =>(0, 0, 170),
Color::DarkGreen =>(0, 170, 0), Color::DarkGreen =>(0, 170, 0),
Color::DarkAqua =>(0, 170, 170), Color::DarkAqua =>(0, 170, 170),
Color::DarkRed =>(170, 0, 0), Color::DarkRed =>(170, 0, 0),
Color::DarkPurple =>(170, 0, 170), Color::DarkPurple =>(170, 0, 170),
Color::Gold =>(255, 170, 0), Color::Gold =>(255, 170, 0),
Color::Gray =>(170, 170, 170), Color::Gray =>(170, 170, 170),
Color::DarkGray =>(85, 85, 85), Color::DarkGray =>(85, 85, 85),
Color::Blue =>(85, 85, 255), Color::Blue =>(85, 85, 255),
Color::Green =>(85, 255, 85), Color::Green =>(85, 255, 85),
Color::Aqua =>(85, 255, 255), Color::Aqua =>(85, 255, 255),
Color::Red =>(255, 85, 85), Color::Red =>(255, 85, 85),
Color::LightPurple =>(255, 85, 255), Color::LightPurple =>(255, 85, 255),
Color::Yellow =>(255, 255, 85), Color::Yellow =>(255, 255, 85),
Color::White =>(255, 255, 255), Color::White =>(255, 255, 255),
Color::RGB(r, g, b) => (r, g, b), Color::RGB(r, g, b) => (r, g, b),
} }
} }
} }
#[test] #[test]
fn test_color_from() { fn test_color_from() {
let test = Color::from_string(&"#FF0000".to_owned()); let test = Color::from_string(&"#FF0000".to_owned());
match test { match test {
Color::RGB(r, g, b) => assert!(r == 255 && g == 0 && b == 0), Color::RGB(r, g, b) => assert!(r == 255 && g == 0 && b == 0),
_ => panic!("Wrong type"), _ => panic!("Wrong type"),
} }
let test = Color::from_string(&"#123456".to_owned()); let test = Color::from_string(&"#123456".to_owned());
match test { match test {
Color::RGB(r, g, b) => assert!(r == 0x12 && g == 0x34 && b == 0x56), Color::RGB(r, g, b) => assert!(r == 0x12 && g == 0x34 && b == 0x56),
_ => panic!("Wrong type"), _ => panic!("Wrong type"),
} }
let test = Color::from_string(&"red".to_owned()); let test = Color::from_string(&"red".to_owned());
match test { match test {
Color::Red => {}, Color::Red => {},
_ => panic!("Wrong type"), _ => panic!("Wrong type"),
} }
let test = Color::from_string(&"dark_blue".to_owned()); let test = Color::from_string(&"dark_blue".to_owned());
match test { match test {
Color::DarkBlue => {}, Color::DarkBlue => {},
_ => panic!("Wrong type"), _ => panic!("Wrong type"),
} }
} }