Lines
76.67 %
Functions
80 %
Branches
100 %
//! `template-edit` submit: syntax, compile check, then the one `EditTemplate`
//! command under the template's key locks.
use forms::{FormValues, TEMPLATE_EDIT};
use server::command::template::EditTemplate;
use server::command::{CmdError, CmdResult};
use server::template_state::STALE_TEMPLATE;
use uuid::Uuid;
use super::Failure;
use super::template_create::checked_source;
use super::transaction_rows::error_at;
const CTX: &str = "submit-form";
/// Validate `template-edit` values and apply the name and the source.
pub(super) async fn edit_template(user_id: Uuid, values: &FormValues) -> Result<Uuid, Failure> {
let errors = values.check(&TEMPLATE_EDIT);
if !errors.is_empty() {
return Err(errors.into());
}
let text = |key| values.text(key).unwrap_or_default();
let template_id = Uuid::parse_str(text("id").trim())
.map_err(|_| error_at("id".to_string(), "unknown template"))?;
let mut command = EditTemplate::new()
.user_id(user_id)
.template_id(template_id)
.source(checked_source(values)?)
.revision(text("revision").trim().to_string());
if let Some(name) = values.text("name") {
command = command.name(name.to_string());
match command.run().await {
Ok(Some(CmdResult::Uuid(id))) => Ok(id),
Ok(other) => Err(Failure::Internal(wasmtime::Error::msg(format!(
"{CTX}: unexpected edit result {other:?}"
)))),
Err(err) => Err(edit_failure(err)),
/// The stale revision is a field error and an unknown template one at `id`;
/// the rest as elsewhere.
fn edit_failure(err: CmdError) -> Failure {
match err {
CmdError::Args(message) if message == STALE_TEMPLATE => {
error_at("revision".to_string(), message).into()
err if err.is_missing_row() => error_at("id".to_string(), "unknown template").into(),
other => Failure::from_command(CTX, other),