1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
use anyhow::*; use data_rw::{Data, DataOwnedReader}; use serde::{Deserialize, Serialize}; use std::io; use std::io::ErrorKind; use std::ops::{Index, IndexMut}; #[derive(Debug)] pub struct RetResult { pub is_error: bool, pub error_id: i32, pub msg: String, pub arguments: Vec<DataOwnedReader>, } impl RetResult { #[inline] pub fn new(is_error: bool, error_id: i32, msg: String, args: Vec<DataOwnedReader>) -> RetResult { RetResult { is_error, error_id, msg, arguments: args, } } #[inline] pub fn success() -> RetResult { RetResult { is_error: false, error_id: 0, msg: "Success".to_string(), arguments: Vec::new(), } } #[inline] pub fn error(error_id: i32, msg: String) -> RetResult { RetResult { is_error: true, error_id, msg, arguments: Vec::new(), } } #[inline] pub fn add_arg_buff<T: Serialize>(&mut self, p: T) { match Data::pack_from(p) { Ok(data) => { self.arguments.push(DataOwnedReader::new(data.into())); } Err(er) => { log::error!("Data serialize error:{} {}", er, line!()) } } } #[inline] pub fn from(mut dr: DataOwnedReader) -> Result<RetResult> { if dr.read_fixed::<bool>()? { Ok(RetResult::new( true, dr.read_fixed::<i32>()?, dr.read_fixed_str()?.to_string(), Vec::new(), )) } else { let len = dr.read_fixed::<i32>()?; let mut buffs = Vec::with_capacity(len as usize); for _ in 0..len { buffs.push(DataOwnedReader::new(dr.read_fixed_buf()?.to_vec())); } Ok(RetResult::new(false, 0, "success".into(), buffs)) } } #[inline] pub fn len(&self) -> usize { self.arguments.len() } #[inline] pub fn is_empty(&self) -> bool { self.arguments.is_empty() } #[inline] pub fn check(self) -> Result<RetResult> { if self.is_error { bail!("{}:{}", self.error_id, self.msg) } else { Ok(self) } } #[inline] pub fn get(&mut self, index: usize) -> io::Result<&mut DataOwnedReader> { if index >= self.len() { return Err(io::Error::new(ErrorKind::Other, "index >= len")); } Ok(&mut self.arguments[index]) } #[inline] pub fn deserialize<'a, T: Deserialize<'a> + 'static>(&'a mut self) -> Result<T> { if self.is_empty() { return Err(io::Error::new(ErrorKind::Other, "index >= len").into()); } self.arguments[0].pack_to() } } impl Index<usize> for RetResult { type Output = DataOwnedReader; #[inline] fn index(&self, index: usize) -> &Self::Output { &self.arguments[index] } } impl IndexMut<usize> for RetResult { #[inline] fn index_mut(&mut self, index: usize) -> &mut Self::Output { &mut self.arguments[index] } }