1
1
// Sync available crate categories from `src/categories.toml`.
2
2
// Runs when the server is started.
3
3
4
- use crate :: {
5
- db,
6
- util:: errors:: { internal, AppResult , ChainError } ,
7
- } ;
4
+ use crate :: { db, util:: Error } ;
8
5
9
6
use diesel:: prelude:: * ;
10
7
@@ -37,9 +34,12 @@ impl Category {
37
34
}
38
35
}
39
36
40
- fn required_string_from_toml < ' a > ( toml : & ' a toml:: value:: Table , key : & str ) -> AppResult < & ' a str > {
41
- toml. get ( key) . and_then ( toml:: Value :: as_str) . chain_error ( || {
42
- internal ( & format_args ! (
37
+ fn required_string_from_toml < ' a > (
38
+ toml : & ' a toml:: value:: Table ,
39
+ key : & str ,
40
+ ) -> Result < & ' a str , Error > {
41
+ toml. get ( key) . and_then ( toml:: Value :: as_str) . ok_or_else ( || {
42
+ Error :: from ( format ! (
43
43
"Expected category TOML attribute '{}' to be a String" ,
44
44
key
45
45
) )
@@ -53,13 +53,13 @@ fn optional_string_from_toml<'a>(toml: &'a toml::value::Table, key: &str) -> &'a
53
53
fn categories_from_toml (
54
54
categories : & toml:: value:: Table ,
55
55
parent : Option < & Category > ,
56
- ) -> AppResult < Vec < Category > > {
56
+ ) -> Result < Vec < Category > , Error > {
57
57
let mut result = vec ! [ ] ;
58
58
59
59
for ( slug, details) in categories {
60
60
let details = details
61
61
. as_table ( )
62
- . chain_error ( || internal ( & format_args ! ( "category {} was not a TOML table" , slug) ) ) ?;
62
+ . ok_or_else ( || Error :: from ( format ! ( "category {} was not a TOML table" , slug) ) ) ?;
63
63
64
64
let category = Category :: from_parent (
65
65
slug,
@@ -69,12 +69,9 @@ fn categories_from_toml(
69
69
) ;
70
70
71
71
if let Some ( categories) = details. get ( "categories" ) {
72
- let categories = categories. as_table ( ) . chain_error ( || {
73
- internal ( & format_args ! (
74
- "child categories of {} were not a table" ,
75
- slug
76
- ) )
77
- } ) ?;
72
+ let categories = categories
73
+ . as_table ( )
74
+ . ok_or_else ( || format ! ( "child categories of {} were not a table" , slug) ) ?;
78
75
79
76
result. extend ( categories_from_toml ( categories, Some ( & category) ) ?) ;
80
77
}
@@ -85,12 +82,12 @@ fn categories_from_toml(
85
82
Ok ( result)
86
83
}
87
84
88
- pub fn sync ( toml_str : & str ) -> AppResult < ( ) > {
85
+ pub fn sync ( toml_str : & str ) -> Result < ( ) , Error > {
89
86
let conn = db:: connect_now ( ) . unwrap ( ) ;
90
87
sync_with_connection ( toml_str, & conn)
91
88
}
92
89
93
- pub fn sync_with_connection ( toml_str : & str , conn : & PgConnection ) -> AppResult < ( ) > {
90
+ pub fn sync_with_connection ( toml_str : & str , conn : & PgConnection ) -> Result < ( ) , Error > {
94
91
use crate :: schema:: categories:: dsl:: * ;
95
92
use diesel:: dsl:: all;
96
93
use diesel:: pg:: upsert:: excluded;
0 commit comments