1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
mod config;
mod manifest;
pub use self::{config::*, manifest::*};
use serde::{de::DeserializeOwned, ser::Serialize};
use std::{io::Error as IoError, path::Path, result::Result};
use toml::{de::Error, ser::Error as TomlError};
#[derive(Debug)]
pub struct CargoTomlError {
inner: ErrorKind,
}
impl std::fmt::Display for CargoTomlError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self.inner)
}
}
impl std::error::Error for CargoTomlError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.inner {
ErrorKind::Parse(e) => Some(e),
ErrorKind::Serialize(e) => Some(e),
ErrorKind::Io(e) => Some(e),
}
}
}
impl From<Error> for CargoTomlError {
fn from(e: Error) -> Self {
Self {
inner: ErrorKind::Parse(e),
}
}
}
impl From<IoError> for CargoTomlError {
fn from(e: IoError) -> Self {
Self {
inner: ErrorKind::Io(e),
}
}
}
impl From<TomlError> for CargoTomlError {
fn from(e: TomlError) -> Self {
Self {
inner: ErrorKind::Serialize(e),
}
}
}
#[derive(Debug)]
enum ErrorKind {
Parse(Error),
Serialize(TomlError),
Io(IoError),
}
pub fn from_path<T: AsRef<Path>, R: DeserializeOwned>(path: T) -> Result<R, CargoTomlError> {
let path = path.as_ref();
let toml = std::fs::read_to_string(path)?;
let x: R = toml::from_str(&toml)?;
Ok(x)
}
pub fn to_path<T: AsRef<Path>, R: Serialize>(path: T, save: R) -> Result<(), CargoTomlError> {
let path = path.as_ref();
let toml = toml::to_string(&save)?;
std::fs::write(path, toml)?;
Ok(())
}