@@ -2,6 +2,83 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry};
22use std:: collections:: HashMap ;
33use std:: rc:: Rc ;
44use techscript_runtime:: { error:: RuntimeError , error:: RuntimeErrorKind , value:: RuntimeValue } ;
5+ use techscript_runtime:: RuntimeContext ;
6+
7+ fn trim ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
8+ let s = args[ 0 ] . try_into_string ( ) ?;
9+ Ok ( RuntimeValue :: Str ( s. trim ( ) . to_string ( ) ) )
10+ }
11+
12+ fn replace ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
13+ let source = args[ 0 ] . try_into_string ( ) ?;
14+ let from = args[ 1 ] . try_into_string ( ) ?;
15+ let to = args[ 2 ] . try_into_string ( ) ?;
16+ Ok ( RuntimeValue :: Str ( source. replace ( & from, & to) ) )
17+ }
18+
19+ fn split ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
20+ let s = args[ 0 ] . try_into_string ( ) ?;
21+ let pat = args[ 1 ] . try_into_string ( ) ?;
22+ let parts: Vec < RuntimeValue > = s
23+ . split ( & pat)
24+ . map ( |p| RuntimeValue :: Str ( p. to_string ( ) ) )
25+ . collect ( ) ;
26+ Ok ( RuntimeValue :: List {
27+ items : Rc :: new ( std:: cell:: RefCell :: new ( parts) ) ,
28+ is_const : false ,
29+ } )
30+ }
31+
32+ fn join ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
33+ let sep = args[ 1 ] . try_into_string ( ) ?;
34+ if let RuntimeValue :: List { items, .. } = & args[ 0 ] {
35+ let mut str_parts = Vec :: new ( ) ;
36+ for item in items. borrow ( ) . iter ( ) {
37+ str_parts. push ( item. try_into_string ( ) ?) ;
38+ }
39+ Ok ( RuntimeValue :: Str ( str_parts. join ( & sep) ) )
40+ } else {
41+ Err ( RuntimeError :: new (
42+ RuntimeErrorKind :: TypeMismatch {
43+ expected : "List" . to_string ( ) ,
44+ found : args[ 0 ] . runtime_type ( ) . to_string ( ) ,
45+ } ,
46+ None ,
47+ None ,
48+ ) )
49+ }
50+ }
51+
52+ fn to_lower ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
53+ let s = args[ 0 ] . try_into_string ( ) ?;
54+ Ok ( RuntimeValue :: Str ( s. to_lowercase ( ) ) )
55+ }
56+
57+ fn to_upper ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
58+ let s = args[ 0 ] . try_into_string ( ) ?;
59+ Ok ( RuntimeValue :: Str ( s. to_uppercase ( ) ) )
60+ }
61+
62+ fn contains ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
63+ let s = args[ 0 ] . try_into_string ( ) ?;
64+ let sub = args[ 1 ] . try_into_string ( ) ?;
65+ Ok ( RuntimeValue :: Bool ( s. contains ( & sub) ) )
66+ }
67+
68+ fn from_int ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
69+ let val = args[ 0 ] . try_into_int ( ) ?;
70+ Ok ( RuntimeValue :: Str ( val. to_string ( ) ) )
71+ }
72+
73+ fn from_float ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
74+ let val = args[ 0 ] . try_into_float ( ) ?;
75+ Ok ( RuntimeValue :: Str ( val. to_string ( ) ) )
76+ }
77+
78+ fn from_bool ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
79+ let val = args[ 0 ] . try_into_bool ( ) ?;
80+ Ok ( RuntimeValue :: Str ( val. to_string ( ) ) )
81+ }
582
683impl StdlibRegistry {
784 pub fn register_strings ( & mut self ) {
@@ -13,10 +90,7 @@ impl StdlibRegistry {
1390 Rc :: new ( StdFunction {
1491 name : "trim" . to_string ( ) ,
1592 arity : 1 ,
16- callback : |_ctx, args| {
17- let s = args[ 0 ] . try_into_string ( ) ?;
18- Ok ( RuntimeValue :: Str ( s. trim ( ) . to_string ( ) ) )
19- } ,
93+ callback : trim,
2094 } ) ,
2195 ) ;
2296
@@ -25,12 +99,7 @@ impl StdlibRegistry {
2599 Rc :: new ( StdFunction {
26100 name : "replace" . to_string ( ) ,
27101 arity : 3 ,
28- callback : |_ctx, args| {
29- let source = args[ 0 ] . try_into_string ( ) ?;
30- let from = args[ 1 ] . try_into_string ( ) ?;
31- let to = args[ 2 ] . try_into_string ( ) ?;
32- Ok ( RuntimeValue :: Str ( source. replace ( & from, & to) ) )
33- } ,
102+ callback : replace,
34103 } ) ,
35104 ) ;
36105
@@ -39,18 +108,7 @@ impl StdlibRegistry {
39108 Rc :: new ( StdFunction {
40109 name : "split" . to_string ( ) ,
41110 arity : 2 ,
42- callback : |_ctx, args| {
43- let s = args[ 0 ] . try_into_string ( ) ?;
44- let pat = args[ 1 ] . try_into_string ( ) ?;
45- let parts: Vec < RuntimeValue > = s
46- . split ( & pat)
47- . map ( |p| RuntimeValue :: Str ( p. to_string ( ) ) )
48- . collect ( ) ;
49- Ok ( RuntimeValue :: List {
50- items : Rc :: new ( std:: cell:: RefCell :: new ( parts) ) ,
51- is_const : false ,
52- } )
53- } ,
111+ callback : split,
54112 } ) ,
55113 ) ;
56114
@@ -59,25 +117,7 @@ impl StdlibRegistry {
59117 Rc :: new ( StdFunction {
60118 name : "join" . to_string ( ) ,
61119 arity : 2 ,
62- callback : |_ctx, args| {
63- let sep = args[ 1 ] . try_into_string ( ) ?;
64- if let RuntimeValue :: List { items, .. } = & args[ 0 ] {
65- let mut str_parts = Vec :: new ( ) ;
66- for item in items. borrow ( ) . iter ( ) {
67- str_parts. push ( item. try_into_string ( ) ?) ;
68- }
69- Ok ( RuntimeValue :: Str ( str_parts. join ( & sep) ) )
70- } else {
71- Err ( RuntimeError :: new (
72- RuntimeErrorKind :: TypeMismatch {
73- expected : "List" . to_string ( ) ,
74- found : args[ 0 ] . runtime_type ( ) . to_string ( ) ,
75- } ,
76- None ,
77- None ,
78- ) )
79- }
80- } ,
120+ callback : join,
81121 } ) ,
82122 ) ;
83123
@@ -86,10 +126,7 @@ impl StdlibRegistry {
86126 Rc :: new ( StdFunction {
87127 name : "to_lower" . to_string ( ) ,
88128 arity : 1 ,
89- callback : |_ctx, args| {
90- let s = args[ 0 ] . try_into_string ( ) ?;
91- Ok ( RuntimeValue :: Str ( s. to_lowercase ( ) ) )
92- } ,
129+ callback : to_lower,
93130 } ) ,
94131 ) ;
95132
@@ -98,10 +135,7 @@ impl StdlibRegistry {
98135 Rc :: new ( StdFunction {
99136 name : "to_upper" . to_string ( ) ,
100137 arity : 1 ,
101- callback : |_ctx, args| {
102- let s = args[ 0 ] . try_into_string ( ) ?;
103- Ok ( RuntimeValue :: Str ( s. to_uppercase ( ) ) )
104- } ,
138+ callback : to_upper,
105139 } ) ,
106140 ) ;
107141
@@ -110,11 +144,7 @@ impl StdlibRegistry {
110144 Rc :: new ( StdFunction {
111145 name : "contains" . to_string ( ) ,
112146 arity : 2 ,
113- callback : |_ctx, args| {
114- let s = args[ 0 ] . try_into_string ( ) ?;
115- let sub = args[ 1 ] . try_into_string ( ) ?;
116- Ok ( RuntimeValue :: Bool ( s. contains ( & sub) ) )
117- } ,
147+ callback : contains,
118148 } ) ,
119149 ) ;
120150
@@ -123,10 +153,7 @@ impl StdlibRegistry {
123153 Rc :: new ( StdFunction {
124154 name : "from_int" . to_string ( ) ,
125155 arity : 1 ,
126- callback : |_ctx, args| {
127- let val = args[ 0 ] . try_into_int ( ) ?;
128- Ok ( RuntimeValue :: Str ( val. to_string ( ) ) )
129- } ,
156+ callback : from_int,
130157 } ) ,
131158 ) ;
132159
@@ -135,10 +162,7 @@ impl StdlibRegistry {
135162 Rc :: new ( StdFunction {
136163 name : "from_float" . to_string ( ) ,
137164 arity : 1 ,
138- callback : |_ctx, args| {
139- let val = args[ 0 ] . try_into_float ( ) ?;
140- Ok ( RuntimeValue :: Str ( val. to_string ( ) ) )
141- } ,
165+ callback : from_float,
142166 } ) ,
143167 ) ;
144168
@@ -147,10 +171,7 @@ impl StdlibRegistry {
147171 Rc :: new ( StdFunction {
148172 name : "from_bool" . to_string ( ) ,
149173 arity : 1 ,
150- callback : |_ctx, args| {
151- let val = args[ 0 ] . try_into_bool ( ) ?;
152- Ok ( RuntimeValue :: Str ( val. to_string ( ) ) )
153- } ,
174+ callback : from_bool,
154175 } ) ,
155176 ) ;
156177
0 commit comments