1
1
use abi_stable:: std_types:: { ROption , RString , RVec } ;
2
2
use anyrun_plugin:: * ;
3
3
use rink_core:: { ast, date, gnu_units, CURRENCY_FILE } ;
4
+ use serde:: Deserialize ;
5
+ use std:: fs;
6
+
7
+ #[ derive( Deserialize , Debug ) ]
8
+ struct Config {
9
+ prefix : String ,
10
+ }
11
+
12
+ impl Default for Config {
13
+ fn default ( ) -> Self {
14
+ Config {
15
+ prefix : "" . to_string ( ) ,
16
+ }
17
+ }
18
+ }
19
+
20
+ struct State {
21
+ ctx : rink_core:: Context ,
22
+ config : Config ,
23
+ }
4
24
5
25
#[ init]
6
- fn init ( _config_dir : RString ) -> rink_core :: Context {
26
+ fn init ( config_dir : RString ) -> State {
7
27
let mut ctx = rink_core:: Context :: new ( ) ;
8
28
9
29
let units = gnu_units:: parse_str ( rink_core:: DEFAULT_FILE . unwrap ( ) ) ;
@@ -29,7 +49,18 @@ fn init(_config_dir: RString) -> rink_core::Context {
29
49
} ) ;
30
50
ctx. load_dates ( dates) ;
31
51
32
- ctx
52
+ let config = match fs:: read_to_string ( format ! ( "{}/rink.ron" , config_dir) ) {
53
+ Ok ( content) => ron:: from_str ( & content) . unwrap_or_else ( |why| {
54
+ eprintln ! ( "[nix-run] Failed to parse config: {}" , why) ;
55
+ Config :: default ( )
56
+ } ) ,
57
+ Err ( why) => {
58
+ eprintln ! ( "[nix-run] No config file provided, using default: {}" , why) ;
59
+ Config :: default ( )
60
+ }
61
+ } ;
62
+
63
+ State { ctx, config }
33
64
}
34
65
35
66
#[ info]
@@ -41,8 +72,14 @@ fn info() -> PluginInfo {
41
72
}
42
73
43
74
#[ get_matches]
44
- fn get_matches ( input : RString , ctx : & mut rink_core:: Context ) -> RVec < Match > {
45
- match rink_core:: one_line ( ctx, & input) {
75
+ fn get_matches ( input : RString , state : & mut State ) -> RVec < Match > {
76
+ let input = if let Some ( input) = input. strip_prefix ( & state. config . prefix ) {
77
+ input. trim ( )
78
+ } else {
79
+ return RVec :: new ( ) ;
80
+ } ;
81
+
82
+ match rink_core:: one_line ( & mut state. ctx , & input) {
46
83
Ok ( result) => {
47
84
let ( title, desc) = parse_result ( result) ;
48
85
vec ! [ Match {
0 commit comments