1
- use cirru_edn:: Edn ;
1
+ use cirru_edn:: { Edn , EdnMapView } ;
2
2
use std:: collections:: HashMap ;
3
3
use std:: sync:: Arc ;
4
4
use tiny_http:: { Method , Response , Server } ;
5
5
6
6
struct HttpServerOptions {
7
7
port : u16 ,
8
- host : Box < str > ,
8
+ host : Arc < str > ,
9
9
}
10
10
11
11
struct ResponseSkeleton {
12
12
code : u16 ,
13
- headers : HashMap < Box < str > , Box < str > > ,
14
- body : Box < str > ,
13
+ headers : HashMap < Arc < str > , Arc < str > > ,
14
+ body : Arc < str > ,
15
15
}
16
16
17
17
#[ no_mangle]
18
18
pub fn abi_version ( ) -> String {
19
- String :: from ( "0.0.6 " )
19
+ String :: from ( "0.0.8 " )
20
20
}
21
21
22
22
#[ no_mangle]
@@ -54,12 +54,12 @@ pub fn serve_http(
54
54
for ( k, v) in query {
55
55
query_dict. insert ( Edn :: tag ( k) , v. into ( ) ) ;
56
56
}
57
- m. insert ( Edn :: tag ( "query" ) , Edn :: Map ( query_dict) ) ;
57
+ m. insert ( Edn :: tag ( "query" ) , Edn :: Map ( EdnMapView ( query_dict) ) ) ;
58
58
}
59
59
None => {
60
60
m. insert ( Edn :: tag ( "path" ) , url. into ( ) ) ;
61
61
m. insert ( Edn :: tag ( "querystring" ) , "" . into ( ) ) ;
62
- m. insert ( Edn :: tag ( "query" ) , Edn :: Map ( HashMap :: new ( ) ) ) ;
62
+ m. insert ( Edn :: tag ( "query" ) , Edn :: Map ( EdnMapView :: default ( ) ) ) ;
63
63
}
64
64
}
65
65
@@ -68,15 +68,15 @@ pub fn serve_http(
68
68
for pair in request. headers ( ) {
69
69
headers. insert ( Edn :: tag ( & pair. field . to_string ( ) ) , Edn :: str ( pair. value . to_string ( ) ) ) ;
70
70
}
71
- m. insert ( Edn :: tag ( "headers" ) , Edn :: Map ( headers) ) ;
71
+ m. insert ( Edn :: tag ( "headers" ) , Edn :: Map ( EdnMapView ( headers) ) ) ;
72
72
73
73
if request. method ( ) != & Method :: Get {
74
74
let mut content = String :: new ( ) ;
75
75
request. as_reader ( ) . read_to_string ( & mut content) . unwrap ( ) ;
76
- m. insert ( Edn :: tag ( "body" ) , Edn :: Str ( content. to_string ( ) . into_boxed_str ( ) ) ) ;
76
+ m. insert ( Edn :: tag ( "body" ) , Edn :: Str ( content. to_string ( ) . into ( ) ) ) ;
77
77
}
78
78
79
- let info = Edn :: Map ( m ) ;
79
+ let info = Edn :: Map ( EdnMapView ( m ) ) ;
80
80
let result = handler ( vec ! [ info] ) ?;
81
81
let res = parse_response ( & result) ?;
82
82
@@ -95,12 +95,12 @@ fn parse_options(d: &Edn) -> Result<HttpServerOptions, String> {
95
95
match d {
96
96
Edn :: Nil => Ok ( HttpServerOptions {
97
97
port : 4000 ,
98
- host : String :: from ( "0.0.0.0" ) . into_boxed_str ( ) ,
98
+ host : Arc :: from ( "0.0.0.0" ) ,
99
99
} ) ,
100
100
Edn :: Map ( m) => {
101
101
let mut options = HttpServerOptions {
102
102
port : 4000 ,
103
- host : String :: from ( "0.0.0.0" ) . into_boxed_str ( ) ,
103
+ host : Arc :: from ( "0.0.0.0" ) ,
104
104
} ;
105
105
options. port = match m. get ( & Edn :: tag ( "port" ) ) {
106
106
Some ( Edn :: Number ( port) ) => * port as u16 ,
@@ -109,7 +109,7 @@ fn parse_options(d: &Edn) -> Result<HttpServerOptions, String> {
109
109
} ;
110
110
options. host = match m. get ( & Edn :: tag ( "host" ) ) {
111
111
Some ( Edn :: Str ( host) ) => host. to_owned ( ) ,
112
- None => String :: from ( "0.0.0.0" ) . into_boxed_str ( ) ,
112
+ None => Arc :: from ( "0.0.0.0" ) ,
113
113
a => return Err ( format ! ( "invalid config for host: {:?}" , a) ) ,
114
114
} ;
115
115
Ok ( options)
@@ -124,7 +124,7 @@ fn parse_response(info: &Edn) -> Result<ResponseSkeleton, String> {
124
124
let mut res = ResponseSkeleton {
125
125
code : 200 ,
126
126
headers : HashMap :: new ( ) ,
127
- body : String :: from ( "" ) . into_boxed_str ( ) ,
127
+ body : String :: from ( "" ) . into ( ) ,
128
128
} ;
129
129
res. code = match m. get ( & Edn :: tag ( "code" ) ) {
130
130
Some ( Edn :: Number ( n) ) => * n as u16 ,
@@ -133,26 +133,26 @@ fn parse_response(info: &Edn) -> Result<ResponseSkeleton, String> {
133
133
} ;
134
134
res. body = match m. get ( & Edn :: tag ( "body" ) ) {
135
135
Some ( Edn :: Str ( s) ) => s. to_owned ( ) ,
136
- Some ( a) => a. to_string ( ) . into_boxed_str ( ) ,
137
- None => String :: from ( "" ) . into_boxed_str ( ) ,
136
+ Some ( a) => a. to_string ( ) . into ( ) ,
137
+ None => String :: from ( "" ) . into ( ) ,
138
138
} ;
139
139
res. headers = match m. get ( & Edn :: tag ( "headers" ) ) {
140
140
Some ( Edn :: Map ( m) ) => {
141
- let mut hs: HashMap < Box < str > , Box < str > > = HashMap :: new ( ) ;
142
- for ( k, v) in m {
143
- let k: Box < str > = if let Edn :: Tag ( s) = k {
144
- s . to_str ( )
141
+ let mut hs: HashMap < Arc < str > , Arc < str > > = HashMap :: new ( ) ;
142
+ for ( k, v) in & m . 0 {
143
+ let k: Arc < str > = if let Edn :: Tag ( s) = k {
144
+ Arc :: from ( s . ref_str ( ) )
145
145
} else if let Edn :: Str ( s) = k {
146
- s . to_owned ( )
146
+ Arc :: from ( & * * s )
147
147
} else {
148
148
return Err ( format ! ( "invalid head entry: {}" , k) ) ;
149
149
} ;
150
150
let value = if let Edn :: Str ( s2) = v {
151
151
s2. to_owned ( )
152
152
} else {
153
- v. to_string ( ) . into_boxed_str ( )
153
+ v. to_string ( ) . into ( )
154
154
} ;
155
- hs. insert ( k, value) ;
155
+ hs. insert ( k, Arc :: from ( & * value) ) ;
156
156
}
157
157
hs
158
158
}
0 commit comments