Macro conciliator::edit_as_toml
source · macro_rules! edit_as_toml { ($x:ident) => { ... }; ($x:ident< $($i:ident),* >, $($i2:ident: $tp:path),*) => { ... }; }
Expand description
Implement Editable
for a type T: Serialize + DeserializeOwned
The generated Editable::into_edit
simply wraps Self
with TomlEditor
.
This macro supports plain structs as well as structs with generic parameters.
In the first case, simply do edit_as_toml!(MyType)
.
In the second case, there are a few limitations: all the generic parameters declared will be passed to the type, and all trait constraints must be passed in the form of T: Trait
(will be passed to a where
clause).
It’s not possible to write T: Serialize + DeserializeOwned
, write T: Serialize, T: DeserializeOwned
instead.
Finally, because the item will be deserialized from a short-lived string, there is no way to specify a lifetime.
In any case, this macro is provided only for convenience, implementing Editable
is trivial.
Simple form:
use serde::{Serialize, Deserialize};
use conciliator::edit::{Edited, edit_as_toml};
#[derive(Debug, Serialize, Deserialize)]
struct Entry {
id: usize,
name: String
}
edit_as_toml!(Entry);
let entry = Entry {id: 3, name: "Entry".to_owned()};
match con.edit(entry) {
Edited::Ok(s) => con.status(format!("Edited entry:\n{s:?}")),
Edited::Cancelled => con.info("Edit aborted!"),
Edited::Err(e) => con.error(format!("{e:?}"))
};
With generics:
use serde::{
Serialize,
Deserialize,
de::DeserializeOwned
};
#[derive(Debug, Serialize, Deserialize)]
struct Entry<T> {
id: usize,
name: String,
thing: T
}
edit_as_toml!(
Entry<T>,
T: Serialize,
T: DeserializeOwned
);
let entry = Entry {
id: 3,
name: "Entry".to_owned(),
thing: 99
};
match con.edit(entry) {
Edited::Ok(s) => con.status(format!("Edited entry:\n{s:?}")),
Edited::Cancelled => con.info("Edit aborted!"),
Edited::Err(e) => con.error(format!("{e:?}"))
};