pub struct Fields(/* private fields */);Expand description
What a form was filled in with.
Every answer arrives as text, because that is what a control on a page produces: a number box hands back the digits that were typed, and an empty box hands back nothing at all. So a field is read through the reader that suits it, and a field holding something that field cannot be is a 400 naming it rather than a panic or a silent zero.
Implementations§
Source§impl Fields
impl Fields
Sourcepub fn get(&self, key: &str) -> Option<&str>
pub fn get(&self, key: &str) -> Option<&str>
The answer as it was typed, or nothing where the form did not carry the field at all.
Sourcepub fn text(&self, key: &str) -> &str
pub fn text(&self, key: &str) -> &str
The answer as text, trimmed. A field nobody filled in is empty rather than absent, since a form that was saved answered every field it had.
Examples found in repository?
943 fn lend(fields: &Fields, args: &ViewArgs, ctx: &Context) -> Result<String, ApiError> {
944 let title = args.get_or("title", "");
945 let borrower = fields.text("borrower");
946 let days = fields.integer("days")?;
947 let from = fields.date("from")?;
948 let condition = fields.text("condition").to_lowercase();
949
950 if borrower.is_empty() {
951 return Err(ApiError::bad_request("a loan needs a borrower"));
952 }
953 if days < 1 {
954 return Err(ApiError::bad_request("a loan is at least one day long"));
955 }
956 let start = days_from_civil(from).ok_or_else(|| {
957 ApiError::bad_request(format!("{from} is not a date in the calendar"))
958 })?;
959 let due = civil_from_days(start + days);
960
961 let mut books: Vec<Book> = ctx.rows(BOOKS_FILE)?;
962 let book = books
963 .iter_mut()
964 .find(|b| b.title == title)
965 .ok_or_else(|| ApiError::bad_request(format!("no book is called \"{title}\"")))?;
966 if book.lent.unwrap_or(false) {
967 return Err(ApiError::bad_request(format!("\"{title}\" is already out")));
968 }
969 book.lent = Some(true);
970 book.due = due.clone();
971 book.notes = format!("Lent to {borrower} on {from}, {condition}.");
972
973 let problems = Books.validate(&books, ctx)?;
974 if let Some(first) = problems.first() {
975 return Err(ApiError::bad_request(format!(
976 "the loan would leave row {} in a state the table refuses: {}",
977 first.line, first.message
978 )));
979 }
980
981 let text = Books
982 .serialize(&books)
983 .map_err(|e| ApiError::server(format!("could not serialize {BOOKS_FILE}: {e}")))?;
984 ctx.write(BOOKS_FILE, &text)?;
985
986 Ok(format!("\"{title}\" is out to {borrower} until {due}."))
987 }Sourcepub fn integer(&self, key: &str) -> Result<i64, ApiError>
pub fn integer(&self, key: &str) -> Result<i64, ApiError>
The answer as a whole number.
Examples found in repository?
943 fn lend(fields: &Fields, args: &ViewArgs, ctx: &Context) -> Result<String, ApiError> {
944 let title = args.get_or("title", "");
945 let borrower = fields.text("borrower");
946 let days = fields.integer("days")?;
947 let from = fields.date("from")?;
948 let condition = fields.text("condition").to_lowercase();
949
950 if borrower.is_empty() {
951 return Err(ApiError::bad_request("a loan needs a borrower"));
952 }
953 if days < 1 {
954 return Err(ApiError::bad_request("a loan is at least one day long"));
955 }
956 let start = days_from_civil(from).ok_or_else(|| {
957 ApiError::bad_request(format!("{from} is not a date in the calendar"))
958 })?;
959 let due = civil_from_days(start + days);
960
961 let mut books: Vec<Book> = ctx.rows(BOOKS_FILE)?;
962 let book = books
963 .iter_mut()
964 .find(|b| b.title == title)
965 .ok_or_else(|| ApiError::bad_request(format!("no book is called \"{title}\"")))?;
966 if book.lent.unwrap_or(false) {
967 return Err(ApiError::bad_request(format!("\"{title}\" is already out")));
968 }
969 book.lent = Some(true);
970 book.due = due.clone();
971 book.notes = format!("Lent to {borrower} on {from}, {condition}.");
972
973 let problems = Books.validate(&books, ctx)?;
974 if let Some(first) = problems.first() {
975 return Err(ApiError::bad_request(format!(
976 "the loan would leave row {} in a state the table refuses: {}",
977 first.line, first.message
978 )));
979 }
980
981 let text = Books
982 .serialize(&books)
983 .map_err(|e| ApiError::server(format!("could not serialize {BOOKS_FILE}: {e}")))?;
984 ctx.write(BOOKS_FILE, &text)?;
985
986 Ok(format!("\"{title}\" is out to {borrower} until {due}."))
987 }Sourcepub fn number(&self, key: &str) -> Result<f64, ApiError>
pub fn number(&self, key: &str) -> Result<f64, ApiError>
The answer as a number, whole or not.
inf and NaN parse as floats and are refused with the rest: a row
holding one serialises to null, which would put a wrong value in a
file rather than say the answer was no good.
Sourcepub fn date(&self, key: &str) -> Result<&str, ApiError>
pub fn date(&self, key: &str) -> Result<&str, ApiError>
The answer as a YYYY-MM-DD date.
The shape is checked and the ranges with it, so nothing beyond a real month and a plausible day gets through; which days a month actually has is a calendar question, and the repository writing the date is what holds a calendar.
Examples found in repository?
943 fn lend(fields: &Fields, args: &ViewArgs, ctx: &Context) -> Result<String, ApiError> {
944 let title = args.get_or("title", "");
945 let borrower = fields.text("borrower");
946 let days = fields.integer("days")?;
947 let from = fields.date("from")?;
948 let condition = fields.text("condition").to_lowercase();
949
950 if borrower.is_empty() {
951 return Err(ApiError::bad_request("a loan needs a borrower"));
952 }
953 if days < 1 {
954 return Err(ApiError::bad_request("a loan is at least one day long"));
955 }
956 let start = days_from_civil(from).ok_or_else(|| {
957 ApiError::bad_request(format!("{from} is not a date in the calendar"))
958 })?;
959 let due = civil_from_days(start + days);
960
961 let mut books: Vec<Book> = ctx.rows(BOOKS_FILE)?;
962 let book = books
963 .iter_mut()
964 .find(|b| b.title == title)
965 .ok_or_else(|| ApiError::bad_request(format!("no book is called \"{title}\"")))?;
966 if book.lent.unwrap_or(false) {
967 return Err(ApiError::bad_request(format!("\"{title}\" is already out")));
968 }
969 book.lent = Some(true);
970 book.due = due.clone();
971 book.notes = format!("Lent to {borrower} on {from}, {condition}.");
972
973 let problems = Books.validate(&books, ctx)?;
974 if let Some(first) = problems.first() {
975 return Err(ApiError::bad_request(format!(
976 "the loan would leave row {} in a state the table refuses: {}",
977 first.line, first.message
978 )));
979 }
980
981 let text = Books
982 .serialize(&books)
983 .map_err(|e| ApiError::server(format!("could not serialize {BOOKS_FILE}: {e}")))?;
984 ctx.write(BOOKS_FILE, &text)?;
985
986 Ok(format!("\"{title}\" is out to {borrower} until {due}."))
987 }