Skip to content

Commit d5cb582

Browse files
author
Guillaume Fraux
committed
Update TOML to 0.3
1 parent 55a8078 commit d5cb582

File tree

15 files changed

+46
-57
lines changed

15 files changed

+46
-57
lines changed

src/input/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "BSD-3-Clause"
99

1010
[dependencies]
1111
lumol-core = {path = "../core"}
12-
toml = "0.2"
12+
toml = "0.3"
1313
log = "0.3"
1414
chemfiles = "0.7"
1515

src/input/src/error.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::fmt;
66
use std::result;
77
use std::path::PathBuf;
88

9-
use toml::Parser;
109
use chemfiles;
1110

1211
use lumol::units::ParseError;
@@ -19,7 +18,7 @@ pub type Result<T> = result::Result<T, Error>;
1918
#[derive(Debug)]
2019
pub enum Error {
2120
/// Error in the TOML input file
22-
TOML(String),
21+
TOML(Box<error::Error>),
2322
/// IO error, and associated file path
2423
Io(io::Error, PathBuf),
2524
/// Error while reading a trajectory file
@@ -63,31 +62,34 @@ impl From<ParseError> for Error {
6362
impl fmt::Display for Error {
6463
fn fmt(&self, fmt: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
6564
use std::error::Error as StdError;
66-
let message = match *self {
65+
match *self {
6766
Error::Io(ref err, ref path) => {
6867
match err.kind() {
6968
io::ErrorKind::NotFound => {
70-
format!("can not find '{}'", path.display())
69+
try!(write!(fmt, "can not find '{}'", path.display()))
7170
}
7271
io::ErrorKind::PermissionDenied => {
73-
format!("permission to access '{}' denied", path.display())
72+
try!(write!(fmt, "permission to access '{}' denied", path.display()))
7473
}
7574
_ => {
76-
format!("error with '{}': {}", path.display(), self.description())
75+
try!(write!(fmt, "error with '{}': {}", path.display(), self.description()))
7776
}
7877
}
7978
}
80-
_ => String::from(self.description())
79+
Error::Trajectory(ref err) => try!(write!(fmt, "{}", err)),
80+
Error::TOML(ref err) => try!(write!(fmt, "{}", err)),
81+
Error::Config(ref err) => try!(write!(fmt, "{}", err)),
82+
Error::Unit(ref err) => try!(write!(fmt, "{}", err)),
8183
};
82-
try!(write!(fmt, "{}", message));
8384
Ok(())
8485
}
8586
}
8687

8788
impl error::Error for Error {
8889
fn description(&self) -> &str {
8990
match *self {
90-
Error::TOML(ref err) | Error::Config(ref err) => err,
91+
Error::Config(ref err) => err,
92+
Error::TOML(ref err) => err.description(),
9193
Error::Io(ref err, _) => err.description(),
9294
Error::Trajectory(ref err) => err.description(),
9395
Error::Unit(ref err) => err.description(),
@@ -103,13 +105,3 @@ impl error::Error for Error {
103105
}
104106
}
105107
}
106-
107-
pub fn toml_error_to_string(parser: &Parser) -> String {
108-
let errors = parser.errors.iter().map(|error|{
109-
let (line, _) = parser.to_linecol(error.lo);
110-
format!("{} at line {}", error.desc, line + 1)
111-
}).collect::<Vec<_>>().join("\n ");
112-
113-
let plural = if errors.len() == 1 {""} else {"s"};
114-
return format!("TOML parsing error{}: {}", plural, errors);
115-
}

src/input/src/extract.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::{Table, Value};
3+
use toml::value::{Table, Value};
44
use error::{Error, Result};
55

66

@@ -68,9 +68,10 @@ pub fn slice<'a>(key: &str, config: &'a Table, context: &str) -> Result<&'a [Val
6868
let array = try!(config.get(key).ok_or(Error::from(
6969
format!("Missing '{}' key in {}", key, context)
7070
)));
71-
return array.as_slice().ok_or(Error::from(
71+
let array = array.as_array().ok_or(Error::from(
7272
format!("'{}' must be an array in {}", key, context)
73-
))
73+
));
74+
return array.map(|arr| arr.as_slice());
7475
}
7576

7677
/// Extract the string 'type' key in a TOML table

src/input/src/interactions/angles.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::{Value, Table};
3+
use toml::value::{Value, Table};
44

55
use lumol::sys::System;
66
use lumol::energy::{Harmonic, CosineHarmonic, Torsion, NullPotential};
@@ -22,7 +22,7 @@ impl InteractionsInput {
2222
None => return Ok(())
2323
};
2424

25-
let angles = try!(angles.as_slice().ok_or(
25+
let angles = try!(angles.as_array().ok_or(
2626
Error::from("The 'angles' section must be an array")
2727
));
2828

@@ -58,7 +58,7 @@ impl InteractionsInput {
5858
None => return Ok(())
5959
};
6060

61-
let dihedrals = try!(dihedrals.as_slice().ok_or(
61+
let dihedrals = try!(dihedrals.as_array().ok_or(
6262
Error::from("The 'dihedrals' section must be an array")
6363
));
6464

src/input/src/interactions/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::{Parser, Table, Value};
3+
use toml::de::from_str as parse;
4+
use toml::value::{Table, Value};
45

56
use std::io::prelude::*;
67
use std::fs::File;
@@ -11,7 +12,6 @@ use lumol::energy::PairRestriction;
1112

1213
use {Error, Result};
1314
use validate;
14-
use error::toml_error_to_string;
1515

1616
mod toml;
1717
mod pairs;
@@ -38,13 +38,11 @@ impl InteractionsInput {
3838
// TODO: use restricted privacy here
3939
#[doc(hidden)]
4040
pub fn from_string(string: &str) -> Result<InteractionsInput> {
41-
let mut parser = Parser::new(string);
42-
let config = try!(parser.parse().ok_or(
43-
Error::TOML(toml_error_to_string(&parser))
44-
));
45-
41+
let config = try!(parse(string).map_err(|err| {
42+
Error::TOML(Box::new(err))
43+
}));
4644
try!(validate(&config));
47-
return InteractionsInput::from_toml(config);
45+
return InteractionsInput::from_toml(config.clone());
4846
}
4947

5048
/// Read the interactions from a TOML table. This is an internal function,

src/input/src/interactions/pairs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::{Value, Table};
3+
use toml::value::{Value, Table};
44

55
use lumol::sys::System;
66
use lumol::units;
@@ -62,7 +62,7 @@ impl InteractionsInput {
6262
None => return Ok(())
6363
};
6464

65-
let pairs = try!(pairs.as_slice().ok_or(
65+
let pairs = try!(pairs.as_array().ok_or(
6666
Error::from("The 'pairs' section must be an array")
6767
));
6868

@@ -157,7 +157,7 @@ impl InteractionsInput {
157157
None => return Ok(())
158158
};
159159

160-
let bonds = try!(bonds.as_slice().ok_or(
160+
let bonds = try!(bonds.as_array().ok_or(
161161
Error::from("The 'bonds' section must be an array")
162162
));
163163

src/input/src/interactions/toml.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
33

44
//! Convert TOML values to Lumol types.
5-
use toml::Table;
5+
use toml::value::Table;
66

77
use error::{Error, Result};
88
use FromToml;

src/input/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extern crate toml;
6767
extern crate chemfiles;
6868
extern crate lumol;
6969

70-
use toml::Table;
70+
use toml::value::Table;
7171

7272
macro_rules! try_io {
7373
($expr: expr, $path: expr) => (
@@ -108,7 +108,7 @@ fn validate(config: &Table) -> Result<()> {
108108
Error::from("Missing 'input' table")
109109
));
110110

111-
let version = try!(input.lookup("version").ok_or(
111+
let version = try!(input.get("version").ok_or(
112112
Error::from("Missing 'version' key in 'input' table")
113113
));
114114

src/input/src/simulations/mc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::Table;
3+
use toml::value::Table;
44
use std::path::PathBuf;
55

66
use lumol::sys::{read_molecule, molecule_type};

src/input/src/simulations/md.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::Table;
3+
use toml::value::Table;
44

55
use lumol::sim::md::*;
66
use lumol::units;
@@ -68,7 +68,7 @@ impl FromToml for MolecularDynamics {
6868
}
6969

7070
if let Some(controls) = config.get("controls") {
71-
let controls = try!(controls.as_slice().ok_or(Error::from(
71+
let controls = try!(controls.as_array().ok_or(Error::from(
7272
"'controls' must be an array of tables in molecular dynamics"
7373
)));
7474

src/input/src/simulations/min.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::Table;
3+
use toml::value::Table;
44

55
use lumol::sim::min::*;
66
use lumol::units;

src/input/src/simulations/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::{Parser, Table};
3+
use toml::de::from_str as parse;
4+
use toml::value::Table;
45

56
use std::io::prelude::*;
67
use std::path::{Path, PathBuf};
78
use std::fs::File;
89

910
use validate;
1011
use error::{Error, Result};
11-
use error::toml_error_to_string;
1212

1313
use lumol::sim::Simulation;
1414
use lumol::sys::System;
@@ -54,14 +54,12 @@ impl Input {
5454
/// Read the `Input` from a TOML formatted string.
5555
// TODO: use restricted privacy here
5656
#[doc(hidden)]
57-
pub fn from_str(path: PathBuf, buffer: &str) -> Result<Input> {
58-
let mut parser = Parser::new(buffer);
59-
let config = try!(parser.parse().ok_or(
60-
Error::TOML(toml_error_to_string(&parser))
61-
));
62-
57+
pub fn from_str(path: PathBuf, string: &str) -> Result<Input> {
58+
let config = try!(parse(string).map_err(|err| {
59+
Error::TOML(Box::new(err))
60+
}));
6361
try!(validate(&config));
64-
Ok(Input{path: path, config: config})
62+
Ok(Input{path: path, config: config.clone()})
6563
}
6664

6765
/// Read input file and get the corresponding `Config`

src/input/src/simulations/outputs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::Table;
3+
use toml::value::Table;
44
use std::path::PathBuf;
55

66
use lumol::out::Output;
@@ -19,7 +19,7 @@ impl Input {
1919
pub fn read_outputs(&self) -> Result<Vec<(Box<Output>, u64)>> {
2020
let config = try!(self.simulation_table());
2121
if let Some(outputs) = config.get("outputs") {
22-
let outputs = try!(outputs.as_slice().ok_or(
22+
let outputs = try!(outputs.as_array().ok_or(
2323
Error::from("'outputs' must be an array of tables in simulation")
2424
));
2525

src/input/src/simulations/simulations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::Table;
3+
use toml::value::Table;
44
use lumol::sim::Simulation;
55

66
use error::{Error, Result};

src/input/src/simulations/system.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lumol, an extensible molecular simulation engine
22
// Copyright (C) 2015-2016 Lumol's contributors — BSD license
3-
use toml::{Table, Value};
3+
use toml::value::{Table, Value};
44

55
use lumol::units;
66
use lumol::sys::*;

0 commit comments

Comments
 (0)