Skip to content

Commit 1d3ff4a

Browse files
committed
feat: Allow working in-memory
1 parent 1dd5ccb commit 1d3ff4a

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

src/lib.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ impl TodoList {
1313
let data = std::fs::read_to_string(path)
1414
.wrap_err_with(|| format!("Could not read {}", path.display()))?;
1515

16-
serde_yaml::from_str(&data)
16+
Self::parse_yaml(&data)
1717
.wrap_err_with(|| format!("Could not parse {}", path.display()))
1818
}
1919
#[cfg(feature = "json")]
2020
Some("json") => {
2121
let data = std::fs::read_to_string(path)
2222
.wrap_err_with(|| format!("Could not read {}", path.display()))?;
2323

24-
serde_json::from_str(&data)
24+
Self::parse_json(&data)
2525
.wrap_err_with(|| format!("Could not parse {}", path.display()))
2626
}
2727
#[cfg(feature = "toml")]
2828
Some("toml") => {
2929
let data = std::fs::read_to_string(path)
3030
.wrap_err_with(|| format!("Could not read {}", path.display()))?;
3131

32-
toml::from_str(&data)
32+
Self::parse_toml(&data)
3333
.wrap_err_with(|| format!("Could not parse {}", path.display()))
3434
}
3535
Some(other) => Err(eyre::eyre!("Unknown extension: {:?}", other)),
@@ -41,21 +41,24 @@ impl TodoList {
4141
match path.extension().and_then(std::ffi::OsStr::to_str) {
4242
#[cfg(feature = "yaml")]
4343
Some("yaml") | Some("yml") => {
44-
let raw = serde_yaml::to_string(self)
44+
let raw = self
45+
.to_yaml()
4546
.wrap_err_with(|| format!("Could not parse {}", path.display()))?;
4647
std::fs::write(path, &raw)
4748
.wrap_err_with(|| format!("Could not write {}", path.display()))
4849
}
4950
#[cfg(feature = "json")]
5051
Some("json") => {
51-
let raw = serde_json::to_string(self)
52+
let raw = self
53+
.to_json()
5254
.wrap_err_with(|| format!("Could not parse {}", path.display()))?;
5355
std::fs::write(path, &raw)
5456
.wrap_err_with(|| format!("Could not write {}", path.display()))
5557
}
5658
#[cfg(feature = "toml")]
5759
Some("toml") => {
58-
let raw = toml::to_string(self)
60+
let raw = self
61+
.to_toml()
5962
.wrap_err_with(|| format!("Could not parse {}", path.display()))?;
6063
std::fs::write(path, &raw)
6164
.wrap_err_with(|| format!("Could not write {}", path.display()))
@@ -65,6 +68,38 @@ impl TodoList {
6568
}
6669
}
6770

71+
#[cfg(feature = "yaml")]
72+
pub fn parse_yaml(data: &str) -> eyre::Result<Self> {
73+
serde_yaml::from_str(data).map_err(|err| err.into())
74+
}
75+
76+
#[cfg(feature = "json")]
77+
pub fn parse_json(data: &str) -> eyre::Result<Self> {
78+
serde_json::from_str(data).map_err(|err| err.into())
79+
}
80+
81+
#[cfg(feature = "toml")]
82+
pub fn parse_toml(data: &str) -> eyre::Result<Self> {
83+
toml::from_str(data).map_err(|err| err.into())
84+
}
85+
86+
#[cfg(feature = "yaml")]
87+
pub fn to_yaml(&self) -> eyre::Result<String> {
88+
serde_yaml::to_string(self).map_err(|err| err.into())
89+
}
90+
91+
#[cfg(feature = "json")]
92+
pub fn to_json(&self) -> eyre::Result<String> {
93+
serde_json::to_string(self).map_err(|err| err.into())
94+
}
95+
96+
#[cfg(feature = "toml")]
97+
pub fn to_toml(&self) -> eyre::Result<String> {
98+
toml::to_string(self).map_err(|err| err.into())
99+
}
100+
}
101+
102+
impl TodoList {
68103
pub fn run(self, cwd: &std::path::Path) -> eyre::Result<()> {
69104
let repo = if self.init {
70105
git2::Repository::init(cwd)?

0 commit comments

Comments
 (0)