lexoffice_cli/actions/
updatable.rs

1use edit::edit_with_builder as edit;
2use lexoffice::request::HasId;
3use lexoffice::request::RequestWithState;
4use lexoffice::request::{ById, Endpoint, ResultInfo, Updatable};
5use lexoffice::{Error, Result};
6use serde::{de::DeserializeOwned, Serialize};
7use serde_any::{from_str, to_string_pretty, Format};
8use structopt::StructOpt;
9
10#[derive(Debug, StructOpt)]
11pub struct UpdatableOpt {
12    /// uuid of the element
13    id: String,
14}
15
16impl UpdatableOpt {
17    pub async fn exec<T, U>(
18        &self,
19        request: RequestWithState<T, U>,
20    ) -> Result<ResultInfo<T>>
21    where
22        RequestWithState<T, U>: Updatable + ById + Endpoint + Clone,
23        T: Serialize + DeserializeOwned + Send + HasId + Clone,
24        U: Clone,
25    {
26        let get = request.clone();
27        let object: T = get.by_id_str(&self.id).await?;
28        let id = object.id().ok_or(Error::NoUuid)?;
29
30        let new_str = edit(
31            to_string_pretty(&object, Format::Yaml).unwrap(),
32            edit::Builder::new().suffix(".yaml"),
33        )?;
34        let new_obj: T = from_str(&new_str, Format::Yaml).unwrap();
35
36        request.update_with_id(id, new_obj).await
37    }
38}