Base of server list

This commit is contained in:
Thinkofdeath 2015-09-23 20:16:25 +01:00
parent 63731ca450
commit 1920e9e9e2
1 changed files with 56 additions and 47 deletions

View File

@ -15,20 +15,21 @@
use serde_json;
use std::fmt;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Component {
Text(TextComponent)
}
impl Component {
pub fn from_value(v: &serde_json::Value) -> Self {
let modifier = Modifier::from_value(v);
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::from_value(v, modifier))
} else {
Component::Text(TextComponent{text: "".to_owned(), modifier: modifier})
modifier.color = Some(Color::RGB(255, 0, 0));
Component::Text(TextComponent{text: "UNHANDLED".to_owned(), modifier: modifier})
}
}
@ -51,7 +52,7 @@ impl Default for Component {
}
}
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Modifier {
pub extra: Option<Vec<Component>>,
pub bold: Option<bool>,
@ -94,13 +95,22 @@ impl Modifier {
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct TextComponent {
pub text: String,
pub modifier: Modifier,
}
impl TextComponent {
pub fn new(val: &str) -> TextComponent {
TextComponent {
text: val.to_owned(),
modifier: Modifier {
.. Default::default()
}
}
}
pub fn from_value(v: &serde_json::Value, modifier: Modifier) -> Self {
TextComponent {
text: v.find("text").unwrap().as_string().unwrap_or("").to_owned(),
@ -125,7 +135,7 @@ impl fmt::Display for TextComponent {
}
}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum Color {
Black,
DarkBlue,
@ -194,7 +204,7 @@ impl Color {
}
}
fn to_string(&self) -> String {
pub fn to_string(&self) -> String {
match *self {
Color::Black => "black".to_owned(),
Color::DarkBlue => "dark_blue".to_owned(),
@ -216,8 +226,7 @@ impl Color {
}
}
#[allow(dead_code)]
fn to_rgb(&self) -> (u8, u8, u8) {
pub fn to_rgb(&self) -> (u8, u8, u8) {
match *self {
Color::Black =>(0, 0, 0),
Color::DarkBlue =>(0, 0, 170),