Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.37 KB | None | 0 0
  1. impl<'lua> ToLua<'lua> for MessageListEntry {
  2.     fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>, Error> {
  3.  
  4.         Ok(match self {
  5.             MessageListEntry::I8(int) => Value::Integer(int as i64),
  6.             MessageListEntry::String(text) => Value::String(lua.create_string(&text).unwrap()),
  7.             MessageListEntry::Coord((x,y)) => {
  8.                 let mut table = lua.create_table().unwrap();
  9.                 table.set("x", x).unwrap();
  10.                 table.set("y", y).unwrap();
  11.                 Value::Table(table)
  12.             },
  13.             MessageListEntry::U8(int) => Value::Integer(int as i64),
  14.             MessageListEntry::U16(int) => Value::Integer(int as i64),
  15.             MessageListEntry::Color((r, g, b, a)) => {
  16.                 let mut table = lua.create_table().unwrap();
  17.                 table.set("r", r).unwrap();
  18.                 table.set("g", g).unwrap();
  19.                 table.set("b", b).unwrap();
  20.                 table.set("a", a).unwrap();
  21.                 Value::Table(table)
  22.             },
  23.             MessageListEntry::List(list) => return list.to_lua(lua),
  24.             MessageListEntry::I32(int) => Value::Integer(int as i64),
  25.             MessageListEntry::I16(int) => Value::Integer(int as i64),
  26.             MessageListEntry::Null => Value::Nil,
  27.             MessageListEntry::I64(int) => Value::Integer(int as i64),
  28.             MessageListEntry::Bytes(bytes) => {
  29.                 let mut table = lua.create_table().unwrap();
  30.                 let mut counter = 1;
  31.                 for byte in bytes {
  32.                     table.set(counter, byte).unwrap();
  33.                     counter += 1;
  34.                 }
  35.                 Value::Table(table)
  36.             },
  37.             MessageListEntry::F32(float) => Value::Number(float as f64),
  38.             MessageListEntry::F64(float) => Value::Number(float as f64),
  39.             MessageListEntry::CoordF32((x, y)) => {
  40.                 let mut table = lua.create_table().unwrap();
  41.                 table.set("x", x).unwrap();
  42.                 table.set("y", y).unwrap();
  43.                 Value::Table(table)
  44.             },
  45.             MessageListEntry::CoordF64((x, y)) => {
  46.                 let mut table = lua.create_table().unwrap();
  47.                 table.set("x", x).unwrap();
  48.                 table.set("y", y).unwrap();
  49.                 Value::Table(table)
  50.             }
  51.         })
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement