Skip to content

Commit a5e6e57

Browse files
committed
chore: Use a common method in parsers to check root is a table
Signed-off-by: Brennan Kinney <[email protected]>
1 parent 8e9fc52 commit a5e6e57

File tree

6 files changed

+36
-40
lines changed

6 files changed

+36
-40
lines changed

src/file/format/json.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::error::Error;
22

3+
use crate::format;
34
use crate::map::Map;
45
use crate::value::{Value, ValueKind};
56

@@ -8,13 +9,8 @@ pub fn parse(
89
text: &str,
910
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
1011
// Parse a JSON object value from the text
11-
// TODO: Have a proper error fire if the root of a file is ever not a Table
1212
let value = from_json_value(uri, &serde_json::from_str(text)?);
13-
match value.kind {
14-
ValueKind::Table(map) => Ok(map),
15-
16-
_ => Ok(Map::new()),
17-
}
13+
format::extract_root_table(uri, value)
1814
}
1915

2016
fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value {

src/file/format/json5.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::error::Error;
22

3-
use crate::error::{ConfigError, Unexpected};
3+
use crate::format;
44
use crate::map::Map;
55
use crate::value::{Value, ValueKind};
66

@@ -20,20 +20,8 @@ pub fn parse(
2020
uri: Option<&String>,
2121
text: &str,
2222
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
23-
match json5_rs::from_str::<Val>(text)? {
24-
Val::String(ref value) => Err(Unexpected::Str(value.clone())),
25-
Val::Integer(value) => Err(Unexpected::I64(value)),
26-
Val::Float(value) => Err(Unexpected::Float(value)),
27-
Val::Boolean(value) => Err(Unexpected::Bool(value)),
28-
Val::Array(_) => Err(Unexpected::Seq),
29-
Val::Null => Err(Unexpected::Unit),
30-
Val::Object(o) => match from_json5_value(uri, Val::Object(o)).kind {
31-
ValueKind::Table(map) => Ok(map),
32-
_ => Ok(Map::new()),
33-
},
34-
}
35-
.map_err(|err| ConfigError::invalid_root(uri, err))
36-
.map_err(|err| Box::new(err) as Box<dyn Error + Send + Sync>)
23+
let value = from_json5_value(uri, json5_rs::from_str::<Val>(text)?);
24+
format::extract_root_table(uri, value)
3725
}
3826

3927
fn from_json5_value(uri: Option<&String>, value: Val) -> Value {

src/file/format/ron.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::error::Error;
22

3+
use crate::format;
34
use crate::map::Map;
45
use crate::value::{Value, ValueKind};
56

@@ -8,11 +9,7 @@ pub fn parse(
89
text: &str,
910
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
1011
let value = from_ron_value(uri, ron::from_str(text)?)?;
11-
match value.kind {
12-
ValueKind::Table(map) => Ok(map),
13-
14-
_ => Ok(Map::new()),
15-
}
12+
format::extract_root_table(uri, value)
1613
}
1714

1815
fn from_ron_value(

src/file/format/toml.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
use std::error::Error;
22

3+
use crate::format;
34
use crate::map::Map;
4-
use crate::value::{Value, ValueKind};
5+
use crate::value::Value;
56

67
pub fn parse(
78
uri: Option<&String>,
89
text: &str,
910
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
1011
// Parse a TOML value from the provided text
11-
// TODO: Have a proper error fire if the root of a file is ever not a Table
1212
let value = from_toml_value(uri, &toml::from_str(text)?);
13-
match value.kind {
14-
ValueKind::Table(map) => Ok(map),
15-
16-
_ => Ok(Map::new()),
17-
}
13+
format::extract_root_table(uri, value)
1814
}
1915

2016
fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value {

src/file/format/yaml.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::mem;
44

55
use yaml_rust as yaml;
66

7+
use crate::format;
78
use crate::map::Map;
89
use crate::value::{Value, ValueKind};
910

@@ -21,13 +22,8 @@ pub fn parse(
2122
}
2223
};
2324

24-
// TODO: Have a proper error fire if the root of a file is ever not a Table
2525
let value = from_yaml_value(uri, &root)?;
26-
match value.kind {
27-
ValueKind::Table(map) => Ok(map),
28-
29-
_ => Ok(Map::new()),
30-
}
26+
format::extract_root_table(uri, value)
3127
}
3228

3329
fn from_yaml_value(

src/format.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::error::Error;
22

3-
use crate::{map::Map, value::Value};
3+
use crate::error::{ConfigError, Unexpected};
4+
use crate::map::Map;
5+
use crate::value::{Value, ValueKind};
46

57
/// Describes a format of configuration source data
68
///
@@ -21,3 +23,24 @@ pub trait Format {
2123
text: &str,
2224
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>>;
2325
}
26+
27+
// Have a proper error fire if the root of a file is ever not a Table
28+
pub fn extract_root_table(
29+
uri: Option<&String>,
30+
value: Value,
31+
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
32+
match value.kind {
33+
ValueKind::Table(map) => Ok(map),
34+
ValueKind::Nil => Err(Unexpected::Unit),
35+
ValueKind::Array(_value) => Err(Unexpected::Seq),
36+
ValueKind::Boolean(value) => Err(Unexpected::Bool(value)),
37+
ValueKind::I64(value) => Err(Unexpected::I64(value)),
38+
ValueKind::I128(value) => Err(Unexpected::I128(value)),
39+
ValueKind::U64(value) => Err(Unexpected::U64(value)),
40+
ValueKind::U128(value) => Err(Unexpected::U128(value)),
41+
ValueKind::Float(value) => Err(Unexpected::Float(value)),
42+
ValueKind::String(value) => Err(Unexpected::Str(value)),
43+
}
44+
.map_err(|err| ConfigError::invalid_root(uri, err))
45+
.map_err(|err| Box::new(err) as Box<dyn Error + Send + Sync>)
46+
}

0 commit comments

Comments
 (0)