Skip to content

Commit 59e3a22

Browse files
committed
add
1 parent 630c74d commit 59e3a22

File tree

17 files changed

+3898
-2
lines changed

17 files changed

+3898
-2
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "rbs"
3+
version = "4.5.27"
4+
edition = "2021"
5+
description = "Serialization framework for ORM"
6+
readme = "Readme.md"
7+
authors = ["ce <zhuxiujia@qq.com>"]
8+
license = "Apache-2.0"
9+
categories = ["database"]
10+
keywords = ["database", "orm", "mysql", "postgres", "sqlite"]
11+
documentation = "https://rbatis.github.io/rbatis.io"
12+
repository = "https://github.com/rbatis/rbatis"
13+
homepage = "https://rbatis.github.io/rbatis.io"
14+
15+
16+
[features]
17+
default = []
18+
#debug_mode will can see MapDeserializer key
19+
debug_mode = []
20+
21+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
22+
[dependencies]
23+
serde = { version = "1.0", features = ["derive"] }
24+
indexmap = "2.5"
25+
26+
27+

README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

Readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# rbs
2+
3+
* rbs is rbatis's impl serde serialize trait crates.
4+
* The rbs serialization framework is used to serialize parameters and deserialize sql result sets, and provides the value structure as py_ Sql and html_ The intermediate object representation of the expression in sql.
5+
6+
## use example
7+
```rust
8+
use std::collections::HashMap;
9+
fn main(){
10+
#[derive(serde::Serialize, serde::Deserialize, Debug)]
11+
pub struct A {
12+
pub name: String,
13+
}
14+
let a = A {
15+
name: "sss".to_string(),
16+
};
17+
let v = rbs::to_value(a).unwrap();
18+
println!("v: {}",v);
19+
let s: A = rbs::from_value(v).unwrap();
20+
println!("s:{:?}", s);
21+
}
22+
```

src/error.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use serde::{ser, Deserialize, Serialize};
2+
use std::fmt::{Display, Formatter};
3+
use std::num::{ParseFloatError, ParseIntError, TryFromIntError};
4+
use std::str::Utf8Error;
5+
6+
#[derive(Debug, Serialize, Deserialize)]
7+
pub enum Error {
8+
E(String),
9+
}
10+
11+
impl Error {
12+
pub fn append(self, arg: &str) -> Error {
13+
match self {
14+
Error::E(mut e) => {
15+
e.push_str(arg);
16+
Error::E(e)
17+
}
18+
}
19+
}
20+
}
21+
22+
impl Error {
23+
#[allow(dead_code)]
24+
#[inline]
25+
pub fn protocol(err: impl Display) -> Self {
26+
Error::from(format!("ProtocolError {}", err))
27+
}
28+
}
29+
30+
impl Display for Error {
31+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32+
match self {
33+
Error::E(e) => std::fmt::Display::fmt(&e, f),
34+
}
35+
}
36+
}
37+
38+
impl std::error::Error for Error {}
39+
40+
impl ser::Error for Error {
41+
fn custom<T: Display>(msg: T) -> Self {
42+
Error::E(format!("{}", msg))
43+
}
44+
}
45+
46+
impl serde::de::Error for Error {
47+
#[cold]
48+
fn custom<T: Display>(msg: T) -> Self {
49+
Error::E(format!("{}", msg))
50+
}
51+
}
52+
53+
impl From<std::io::Error> for Error {
54+
fn from(arg: std::io::Error) -> Self {
55+
Error::from(arg.to_string())
56+
}
57+
}
58+
59+
impl From<std::str::Utf8Error> for Error {
60+
fn from(e: Utf8Error) -> Self {
61+
Error::from(e.to_string())
62+
}
63+
}
64+
65+
impl From<&str> for Error {
66+
fn from(arg: &str) -> Self {
67+
Error::from(arg.to_string())
68+
}
69+
}
70+
71+
impl From<String> for Error {
72+
fn from(arg: String) -> Self {
73+
Error::E(arg)
74+
}
75+
}
76+
77+
impl From<ParseIntError> for Error {
78+
fn from(arg: ParseIntError) -> Self {
79+
Error::from(arg.to_string())
80+
}
81+
}
82+
83+
impl From<ParseFloatError> for Error {
84+
fn from(arg: ParseFloatError) -> Self {
85+
Error::from(arg.to_string())
86+
}
87+
}
88+
89+
impl From<TryFromIntError> for Error {
90+
fn from(e: TryFromIntError) -> Self {
91+
Error::from(e.to_string())
92+
}
93+
}
94+
95+
96+
// Format an error message as a `Protocol` error
97+
#[macro_export]
98+
macro_rules! err_protocol {
99+
($expr:expr) => {
100+
$crate::Error::E($expr.into())
101+
};
102+
103+
($fmt:expr, $($arg:tt)*) => {
104+
$crate::Error::E(format!($fmt, $($arg)*))
105+
};
106+
}

src/index.rs

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
use crate::Value;
2+
use std::ops::{Index, IndexMut};
3+
4+
impl Index<usize> for Value {
5+
type Output = Value;
6+
7+
fn index(&self, index: usize) -> &Value {
8+
match self {
9+
Value::Array(arr) => &arr[index],
10+
Value::Ext(_, ext) => {
11+
return ext.index(index);
12+
}
13+
_ => &Value::Null,
14+
}
15+
}
16+
}
17+
18+
impl IndexMut<usize> for Value {
19+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
20+
match self {
21+
Value::Array(arr) => &mut arr[index],
22+
Value::Ext(_, ext) => {
23+
return ext.index_mut(index);
24+
}
25+
_ => {
26+
panic!("not an array!")
27+
}
28+
}
29+
}
30+
}
31+
32+
impl Index<&str> for Value {
33+
type Output = Value;
34+
fn index(&self, index: &str) -> &Self::Output {
35+
match self {
36+
Value::Map(m) => {
37+
m.index(index)
38+
}
39+
Value::Ext(_, ext) => {
40+
ext.index(index)
41+
}
42+
_ => {
43+
&Value::Null
44+
}
45+
}
46+
}
47+
}
48+
49+
impl IndexMut<&str> for Value {
50+
fn index_mut(&mut self, index: &str) -> &mut Self::Output {
51+
match self {
52+
Value::Map(m) => m.index_mut(index),
53+
Value::Ext(_, ext) => {
54+
return ext.index_mut(index);
55+
}
56+
_ => {
57+
panic!("not map type")
58+
}
59+
}
60+
}
61+
}
62+
63+
64+
impl Index<Value> for Value {
65+
type Output = Value;
66+
67+
fn index(&self, index: Value) -> &Self::Output {
68+
return match self {
69+
Value::Array(arr) => {
70+
let idx = index.as_u64().unwrap_or_default() as usize;
71+
arr.index(idx)
72+
}
73+
Value::Map(map) => {
74+
let s = index.as_str().unwrap_or_default();
75+
map.index(s)
76+
}
77+
Value::Ext(_, ext) => {
78+
ext.index(index)
79+
}
80+
_ => {
81+
&Value::Null
82+
}
83+
};
84+
}
85+
}
86+
87+
88+
impl Index<&Value> for Value {
89+
type Output = Value;
90+
91+
fn index(&self, index: &Value) -> &Self::Output {
92+
return match self {
93+
Value::Array(arr) => {
94+
let idx = index.as_u64().unwrap_or_default() as usize;
95+
arr.index(idx)
96+
}
97+
Value::Map(map) => {
98+
let s = index.as_str().unwrap_or_default();
99+
map.index(s)
100+
}
101+
Value::Ext(_, ext) => {
102+
ext.index(index)
103+
}
104+
_ => {
105+
&Value::Null
106+
}
107+
};
108+
}
109+
}
110+
111+
112+
impl IndexMut<Value> for Value {
113+
fn index_mut(&mut self, index: Value) -> &mut Self::Output {
114+
match self {
115+
Value::Array(arr) => {
116+
let idx = index.as_u64().unwrap_or_default() as usize;
117+
arr.index_mut(idx)
118+
}
119+
Value::Map(map) => {
120+
let s = index.as_str().unwrap_or_default();
121+
map.index_mut(s)
122+
}
123+
Value::Ext(_, ext) => {
124+
ext.index_mut(index)
125+
}
126+
_ => {
127+
panic!("not map/array type")
128+
}
129+
}
130+
}
131+
}
132+
133+
134+
impl IndexMut<&Value> for Value {
135+
fn index_mut(&mut self, index: &Value) -> &mut Self::Output {
136+
match self {
137+
Value::Array(arr) => {
138+
let idx = index.as_u64().unwrap_or_default() as usize;
139+
arr.index_mut(idx)
140+
}
141+
Value::Map(map) => {
142+
let s = index.as_str().unwrap_or_default();
143+
map.index_mut(s)
144+
}
145+
Value::Ext(_, ext) => {
146+
ext.index_mut(index)
147+
}
148+
_ => {
149+
panic!("not map/array type")
150+
}
151+
}
152+
}
153+
}
154+
155+
impl Value {
156+
pub fn insert(&mut self, key: Value, value: Value) -> Option<Value> {
157+
match self {
158+
Value::Null => None,
159+
Value::Bool(_) => None,
160+
Value::I32(_) => None,
161+
Value::I64(_) => None,
162+
Value::U32(_) => None,
163+
Value::U64(_) => None,
164+
Value::F32(_) => None,
165+
Value::F64(_) => None,
166+
Value::String(_) => None,
167+
Value::Binary(_) => None,
168+
Value::Array(arr) => {
169+
arr.insert(key.as_u64().unwrap_or_default() as usize, value);
170+
None
171+
}
172+
Value::Map(m) => m.insert(key, value),
173+
Value::Ext(_, m) => m.insert(key, value),
174+
}
175+
}
176+
177+
pub fn remove(&mut self, key: &Value) -> Value {
178+
match self {
179+
Value::Null => Value::Null,
180+
Value::Bool(_) => Value::Null,
181+
Value::I32(_) => Value::Null,
182+
Value::I64(_) => Value::Null,
183+
Value::U32(_) => Value::Null,
184+
Value::U64(_) => Value::Null,
185+
Value::F32(_) => Value::Null,
186+
Value::F64(_) => Value::Null,
187+
Value::String(_) => Value::Null,
188+
Value::Binary(_) => Value::Null,
189+
Value::Array(arr) => {
190+
let index = key.as_u64().unwrap_or_default() as usize;
191+
if index >= arr.len() {
192+
return Value::Null;
193+
}
194+
arr.remove(index)
195+
}
196+
Value::Map(m) => m.remove(key),
197+
Value::Ext(_, e) => e.remove(key),
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)