Lines
95.83 %
Functions
20.83 %
Branches
100 %
//! The create pages over a shared mutation form.
use std::collections::BTreeMap;
use axum::response::Response;
use forms::{FieldError, Form, FormValues};
use uuid::Uuid;
use super::attempt::{Attempt, attempt};
use super::references;
use crate::form_invalid::form_invalid;
use crate::form_render::{References, render_form};
use crate::response::WebError;
/// A create form and the path it posts to.
pub struct Create {
pub form: &'static Form,
pub action: &'static str,
}
/// A submit's answer: the stored entity with its notice, or the response
/// that replaces the form.
pub enum Created {
Saved { id: Uuid, notice: Option<String> },
Invalid(Response),
impl Create {
fn render(
&self,
values: &FormValues,
errors: &[FieldError],
references: &References,
) -> Result<String, WebError> {
render_form(self.form, values, errors, self.action, references)
.map_err(|e| WebError::internal("Create form render failed", e))
/// The empty form.
pub async fn blank(&self, user_id: Uuid) -> Result<String, WebError> {
let references = references(self.form, user_id, BTreeMap::new()).await?;
self.render(&FormValues::default(), &[], &references)
pub async fn submit(
user_id: Uuid,
pairs: Vec<(String, String)>,
) -> Result<Created, WebError> {
Ok(match attempt(self.form, user_id, pairs).await? {
Attempt::Saved { id, notice } => Created::Saved { id, notice },
Attempt::Rejected(rejected) => Created::Invalid(form_invalid(
self.form,
self.render(&rejected.values, &rejected.errors, &rejected.references)?,
)),
})