use crate::model::EndpointUrl;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormField {
name: String,
value: String,
}
impl FormField {
pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
value: value.into(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn value(&self) -> &str {
&self.value
}
pub(crate) fn into_pair(self) -> (String, String) {
(self.name, self.value)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PostForm {
action: EndpointUrl,
fields: Vec<FormField>,
}
impl PostForm {
pub fn new(action: EndpointUrl, fields: Vec<FormField>) -> Self {
Self { action, fields }
}
pub fn action(&self) -> &EndpointUrl {
&self.action
}
pub fn fields(&self) -> &[FormField] {
&self.fields
}
pub fn value(&self, name: &str) -> Option<&str> {
self.fields
.iter()
.find(|field| field.name() == name)
.map(FormField::value)
}
}