File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1
1
pub ( crate ) mod reverse;
2
+ pub ( crate ) mod join;
Original file line number Diff line number Diff line change
1
+ use crate :: ifn:: IFn ;
2
+ use crate :: value:: { ToValue , Value } ;
3
+ use std:: rc:: Rc ;
4
+
5
+ use crate :: error_message;
6
+ use crate :: type_tag:: TypeTag ;
7
+
8
+ /// clojure.string/reverse ; reverses a string
9
+ /// (defn print-string [string] .. prints single string .. )
10
+ #[ derive( Debug , Clone ) ]
11
+ pub struct JoinFn { }
12
+ impl ToValue for JoinFn {
13
+ fn to_value ( & self ) -> Value {
14
+ Value :: IFn ( Rc :: new ( self . clone ( ) ) )
15
+ }
16
+ }
17
+ impl IFn for JoinFn {
18
+ fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
19
+ if args. len ( ) == 1 {
20
+ match args. get ( 0 ) . unwrap ( ) . to_value ( ) {
21
+ Value :: Iseq ( s) => Value :: String ( s. chars ( ) . rev ( ) . collect ( ) ) ,
22
+ _a => error_message:: type_mismatch ( TypeTag :: String , & _a. to_value ( ) )
23
+ }
24
+
25
+ } else {
26
+ return error_message:: wrong_arg_count ( 2 , args. len ( ) ) ;
27
+
28
+ }
29
+ }
30
+ }
31
+
32
+ #[ cfg( test) ]
33
+ mod tests {
34
+ mod reverse_tests {
35
+ use crate :: value:: Value ;
36
+ use std:: rc:: Rc ;
37
+ use crate :: clojure_string:: reverse:: JoinFn ;
38
+ use crate :: ifn:: IFn ;
39
+
40
+ #[ test]
41
+ fn reverse_string ( ) {
42
+ let reverse = JoinFn { } ;
43
+ let s = "hello" ;
44
+ let args = vec ! [ Rc :: new( Value :: String ( String :: from( s) ) ) ] ;
45
+ assert_eq ! ( Value :: String ( String :: from( "olleh" ) ) , reverse. invoke( args) ) ;
46
+ }
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments