Lines
84 %
Functions
80 %
Branches
100 %
//! The `template-create` form submit: an optional name and the source.
use forms::{FormValues, TEMPLATE_CREATE};
use server::command::CmdResult;
use server::command::template::CreateTemplate;
use uuid::Uuid;
use super::Failure;
use super::transaction_rows::error_at;
use crate::template::validate_source;
const CTX: &str = "template-create";
/// The source, else a field error at `source` when it does not compile.
pub(super) fn checked_source(values: &FormValues) -> Result<String, Failure> {
let source = values.text("source").unwrap_or_default();
validate_source(source).map_err(|message| error_at("source".to_string(), message))?;
Ok(source.to_string())
}
/// `template-create` submit: the id of the new template.
pub(super) async fn create_template(user_id: Uuid, values: &FormValues) -> Result<Uuid, Failure> {
let errors = values.check(&TEMPLATE_CREATE);
if !errors.is_empty() {
return Err(errors.into());
let mut command = CreateTemplate::new()
.user_id(user_id)
.source(checked_source(values)?);
if let Some(name) = values.text("name") {
command = command.name(name.to_string());
match command
.run()
.await
.map_err(|err| Failure::from_command(CTX, err))?
{
Some(CmdResult::Uuid(id)) => Ok(id),
other => Err(Failure::Internal(wasmtime::Error::msg(format!(
"{CTX}: unexpected create result {other:?}"
)))),