1
- use std:: {
2
- cell:: RefCell ,
3
- rc:: Rc
4
- } ;
5
- use std:: io:: Write ;
6
1
use expression:: {
2
+ eval:: Object ,
7
3
Context ,
8
- DataSource ,
9
- Row ,
10
- eval:: Object
4
+ DataSource
11
5
} ;
6
+ use std:: io:: Write ;
12
7
13
- /// This struct implements a friendly way to represent a row of a table.
14
- /// The `Row` trait marks this structure with the necessary methods to be useful
15
- ///
16
- /// This example uses interior mutability because `DataSource` trait works with owned types such as `Rc`s.
17
- #[ derive( Clone ) ]
18
- struct ExampleRow {
19
- inner : Rc < RefCell < RowInner > >
20
- }
21
-
22
- struct RowInner {
23
- col1 : String ,
24
- col2 : f64
25
- }
26
-
27
- impl Row for ExampleRow {
28
- fn fields ( & self ) -> impl Iterator < Item = impl AsRef < str > > + Clone {
29
- vec ! [ "col1" , "col2" ] . into_iter ( )
30
- }
31
-
32
- fn get ( & self , field : & str ) -> Option < Object > {
33
- let inner = self . inner . borrow ( ) ;
34
-
35
- match field {
36
- "col1" => Some ( Object :: String ( inner. col1 . clone ( ) ) ) ,
37
- "col2" => Some ( Object :: Number ( inner. col2 ) ) ,
38
- _ => None
39
- }
40
- }
41
- }
42
-
43
- struct ExampleProvider < Rows : Row > {
44
- columns : Vec < String > ,
45
- rows : Vec < Rows > ,
8
+ struct ExampleProvider {
9
+ values : Vec < String >
46
10
}
47
11
48
- impl < Rows : Row + Clone > DataSource for ExampleProvider < Rows > {
49
- type Rows = Rows ;
50
-
51
- fn list_columns ( & self ) -> impl Iterator < Item =impl AsRef < str > > {
52
- self . columns . clone ( ) . into_iter ( )
53
- }
54
-
55
- fn rows ( & self ) -> impl Iterator < Item =Self :: Rows > {
56
- self . rows . iter ( )
57
- . cloned ( )
58
- }
59
-
60
- fn row ( & self , row : usize ) -> Option < Self :: Rows > {
61
- self . rows . get ( row)
62
- . cloned ( )
63
- }
12
+ impl DataSource for ExampleProvider {
13
+ fn query ( & self , query : impl AsRef < str > ) -> Option < Object > {
14
+ let index = query. as_ref ( ) . parse :: < usize > ( ) . ok ( ) ?;
64
15
65
- fn num_rows ( & self ) -> usize {
66
- self . rows . len ( )
16
+ self . values . get ( index )
17
+ . map ( |i| Object :: String ( i . clone ( ) ) )
67
18
}
68
19
}
69
20
70
- pub fn main ( ) {
71
- let cx = Context :: new ( ExampleProvider :: < ExampleRow > {
72
- columns : vec ! [
73
- "col1" . to_owned( ) ,
74
- "col2" . to_owned( ) ,
75
- ] ,
76
- rows : vec ! [ ]
21
+ pub fn main ( ) -> std:: io:: Result < ( ) > {
22
+ let cx = Context :: new ( ExampleProvider {
23
+ values : Vec :: new ( )
77
24
} ) ;
78
25
79
26
let parser = cx. parse_context ( ) ;
80
27
81
- let program = std:: env:: args ( ) . nth ( 1 ) . unwrap ( ) ;
28
+ let mut buffer = String :: new ( ) ;
29
+
30
+ eprint ! ( "> " ) ;
31
+ std:: io:: stderr ( ) . flush ( ) ?;
82
32
83
- print ! ( "{}: " , & program ) ;
84
- std :: io :: stdout ( ) . flush ( ) . unwrap ( ) ;
33
+ while let Ok ( len ) = std :: io :: stdin ( ) . read_line ( & mut buffer ) {
34
+ let program = buffer [ 0 ..len ] . trim ( ) ;
85
35
86
- match parser. parse ( program) {
87
- Ok ( result) => println ! ( "{:#?}" , result) ,
88
- Err ( err) => println ! ( "Error: {:?}" , err)
36
+ match parser. parse ( program) {
37
+ Ok ( result) => println ! ( "{:#?}" , result) ,
38
+ Err ( err) => println ! ( "Error: {:?}" , err)
39
+ }
40
+
41
+ eprint ! ( "> " ) ;
42
+ std:: io:: stderr ( ) . flush ( ) ?;
43
+ buffer. clear ( ) ;
89
44
}
45
+
46
+ Ok ( ( ) )
90
47
}
0 commit comments