@@ -13,23 +13,23 @@ impl TodoList {
13
13
let data = std:: fs:: read_to_string ( path)
14
14
. wrap_err_with ( || format ! ( "Could not read {}" , path. display( ) ) ) ?;
15
15
16
- serde_yaml :: from_str ( & data)
16
+ Self :: parse_yaml ( & data)
17
17
. wrap_err_with ( || format ! ( "Could not parse {}" , path. display( ) ) )
18
18
}
19
19
#[ cfg( feature = "json" ) ]
20
20
Some ( "json" ) => {
21
21
let data = std:: fs:: read_to_string ( path)
22
22
. wrap_err_with ( || format ! ( "Could not read {}" , path. display( ) ) ) ?;
23
23
24
- serde_json :: from_str ( & data)
24
+ Self :: parse_json ( & data)
25
25
. wrap_err_with ( || format ! ( "Could not parse {}" , path. display( ) ) )
26
26
}
27
27
#[ cfg( feature = "toml" ) ]
28
28
Some ( "toml" ) => {
29
29
let data = std:: fs:: read_to_string ( path)
30
30
. wrap_err_with ( || format ! ( "Could not read {}" , path. display( ) ) ) ?;
31
31
32
- toml :: from_str ( & data)
32
+ Self :: parse_toml ( & data)
33
33
. wrap_err_with ( || format ! ( "Could not parse {}" , path. display( ) ) )
34
34
}
35
35
Some ( other) => Err ( eyre:: eyre!( "Unknown extension: {:?}" , other) ) ,
@@ -41,21 +41,24 @@ impl TodoList {
41
41
match path. extension ( ) . and_then ( std:: ffi:: OsStr :: to_str) {
42
42
#[ cfg( feature = "yaml" ) ]
43
43
Some ( "yaml" ) | Some ( "yml" ) => {
44
- let raw = serde_yaml:: to_string ( self )
44
+ let raw = self
45
+ . to_yaml ( )
45
46
. wrap_err_with ( || format ! ( "Could not parse {}" , path. display( ) ) ) ?;
46
47
std:: fs:: write ( path, & raw )
47
48
. wrap_err_with ( || format ! ( "Could not write {}" , path. display( ) ) )
48
49
}
49
50
#[ cfg( feature = "json" ) ]
50
51
Some ( "json" ) => {
51
- let raw = serde_json:: to_string ( self )
52
+ let raw = self
53
+ . to_json ( )
52
54
. wrap_err_with ( || format ! ( "Could not parse {}" , path. display( ) ) ) ?;
53
55
std:: fs:: write ( path, & raw )
54
56
. wrap_err_with ( || format ! ( "Could not write {}" , path. display( ) ) )
55
57
}
56
58
#[ cfg( feature = "toml" ) ]
57
59
Some ( "toml" ) => {
58
- let raw = toml:: to_string ( self )
60
+ let raw = self
61
+ . to_toml ( )
59
62
. wrap_err_with ( || format ! ( "Could not parse {}" , path. display( ) ) ) ?;
60
63
std:: fs:: write ( path, & raw )
61
64
. wrap_err_with ( || format ! ( "Could not write {}" , path. display( ) ) )
@@ -65,6 +68,38 @@ impl TodoList {
65
68
}
66
69
}
67
70
71
+ #[ cfg( feature = "yaml" ) ]
72
+ pub fn parse_yaml ( data : & str ) -> eyre:: Result < Self > {
73
+ serde_yaml:: from_str ( data) . map_err ( |err| err. into ( ) )
74
+ }
75
+
76
+ #[ cfg( feature = "json" ) ]
77
+ pub fn parse_json ( data : & str ) -> eyre:: Result < Self > {
78
+ serde_json:: from_str ( data) . map_err ( |err| err. into ( ) )
79
+ }
80
+
81
+ #[ cfg( feature = "toml" ) ]
82
+ pub fn parse_toml ( data : & str ) -> eyre:: Result < Self > {
83
+ toml:: from_str ( data) . map_err ( |err| err. into ( ) )
84
+ }
85
+
86
+ #[ cfg( feature = "yaml" ) ]
87
+ pub fn to_yaml ( & self ) -> eyre:: Result < String > {
88
+ serde_yaml:: to_string ( self ) . map_err ( |err| err. into ( ) )
89
+ }
90
+
91
+ #[ cfg( feature = "json" ) ]
92
+ pub fn to_json ( & self ) -> eyre:: Result < String > {
93
+ serde_json:: to_string ( self ) . map_err ( |err| err. into ( ) )
94
+ }
95
+
96
+ #[ cfg( feature = "toml" ) ]
97
+ pub fn to_toml ( & self ) -> eyre:: Result < String > {
98
+ toml:: to_string ( self ) . map_err ( |err| err. into ( ) )
99
+ }
100
+ }
101
+
102
+ impl TodoList {
68
103
pub fn run ( self , cwd : & std:: path:: Path ) -> eyre:: Result < ( ) > {
69
104
let repo = if self . init {
70
105
git2:: Repository :: init ( cwd) ?
0 commit comments