Skip to content

Commit 8eb4ec2

Browse files
committed
Convert categories boot sync to concrete error type
1 parent 1c40f7a commit 8eb4ec2

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/boot/categories.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Sync available crate categories from `src/categories.toml`.
22
// Runs when the server is started.
33

4-
use crate::{
5-
db,
6-
util::errors::{internal, AppResult, ChainError},
7-
};
4+
use crate::{db, util::Error};
85

96
use diesel::prelude::*;
107

@@ -37,9 +34,12 @@ impl Category {
3734
}
3835
}
3936

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!(
4343
"Expected category TOML attribute '{}' to be a String",
4444
key
4545
))
@@ -53,13 +53,13 @@ fn optional_string_from_toml<'a>(toml: &'a toml::value::Table, key: &str) -> &'a
5353
fn categories_from_toml(
5454
categories: &toml::value::Table,
5555
parent: Option<&Category>,
56-
) -> AppResult<Vec<Category>> {
56+
) -> Result<Vec<Category>, Error> {
5757
let mut result = vec![];
5858

5959
for (slug, details) in categories {
6060
let details = details
6161
.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)))?;
6363

6464
let category = Category::from_parent(
6565
slug,
@@ -69,12 +69,9 @@ fn categories_from_toml(
6969
);
7070

7171
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))?;
7875

7976
result.extend(categories_from_toml(categories, Some(&category))?);
8077
}
@@ -85,12 +82,12 @@ fn categories_from_toml(
8582
Ok(result)
8683
}
8784

88-
pub fn sync(toml_str: &str) -> AppResult<()> {
85+
pub fn sync(toml_str: &str) -> Result<(), Error> {
8986
let conn = db::connect_now().unwrap();
9087
sync_with_connection(toml_str, &conn)
9188
}
9289

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> {
9491
use crate::schema::categories::dsl::*;
9592
use diesel::dsl::all;
9693
use diesel::pg::upsert::excluded;

0 commit comments

Comments
 (0)