Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions cot-cli/src/migration_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ impl MigrationOperationGenerator {
fn make_remove_model_operation(migration_model: &ModelInSource) -> DynOperation {
print_status_msg(
StatusType::Removing,
&format!("Model '{}'", &migration_model.model.name),
&format!("Model '{}'", migration_model.model.name),
);

let op = DynOperation::RemoveModel {
Expand All @@ -781,7 +781,7 @@ impl MigrationOperationGenerator {

print_status_msg(
StatusType::Removed,
&format!("Model '{}'", &migration_model.model.name),
&format!("Model '{}'", migration_model.model.name),
);

op
Expand All @@ -791,10 +791,7 @@ impl MigrationOperationGenerator {
fn make_add_field_operation(app_model: &ModelInSource, field: &Field) -> DynOperation {
print_status_msg(
StatusType::Adding,
&format!(
"Field '{}' to Model '{}'",
&field.name, app_model.model.name
),
&format!("Field '{}' to Model '{}'", field.name, app_model.model.name),
);

let op = DynOperation::AddField {
Expand All @@ -805,10 +802,7 @@ impl MigrationOperationGenerator {

print_status_msg(
StatusType::Added,
&format!(
"Field '{}' to Model '{}'",
&field.name, app_model.model.name
),
&format!("Field '{}' to Model '{}'", field.name, app_model.model.name),
);

op
Expand All @@ -828,7 +822,7 @@ impl MigrationOperationGenerator {
StatusType::Modifying,
&format!(
"Field '{}' from Model '{}'",
&migration_field.name, migration_model.model.name
migration_field.name, migration_model.model.name
),
);

Expand All @@ -839,7 +833,7 @@ impl MigrationOperationGenerator {
StatusType::Modified,
&format!(
"Field '{}' from Model '{}'",
&migration_field.name, migration_model.model.name
migration_field.name, migration_model.model.name
),
);
}
Expand All @@ -853,7 +847,7 @@ impl MigrationOperationGenerator {
StatusType::Removing,
&format!(
"Field '{}' from Model '{}'",
&migration_field.name, migration_model.model.name
migration_field.name, migration_model.model.name
),
);

Expand All @@ -867,7 +861,7 @@ impl MigrationOperationGenerator {
StatusType::Removed,
&format!(
"Field '{}' from Model '{}'",
&migration_field.name, migration_model.model.name
migration_field.name, migration_model.model.name
),
);

Expand Down
36 changes: 35 additions & 1 deletion cot-core/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl HtmlTag {
}
}

/// Creates a new `HtmlTag` instance for an input element.
/// Creates a new `HtmlTag` instance for an [input] element.
///
/// # Examples
///
Expand All @@ -201,13 +201,47 @@ impl HtmlTag {
/// let input = HtmlTag::input("text");
/// assert_eq!(input.render().as_str(), "<input type=\"text\"/>");
/// ```
///
/// [input]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
#[must_use]
pub fn input(input_type: &str) -> Self {
let mut input = Self::new("input");
input.attr("type", input_type);
input
}

/// Creates a new `HtmlTag` instance for a [datalist] element.
///
/// # Examples
/// ```
/// use cot::html::HtmlTag;
/// let data_list = HtmlTag::data_list(vec!["Option 1", "Option 2"], "my-datalist");
/// let rendered = data_list.render();
/// ```
///
/// [datalist]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/datalist
#[must_use]
pub fn data_list<I, S>(list: I, id: &str) -> Self

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should force providing id in the constructor, maybe getting rid of it and expecting users to do .attr("id", id") is enough?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm, The thing about the datalist element is that the id attr is tightly coupled with the list attribute in the input element (i.e the list attr in the input should match the id attr in the datalist) so if the user forgets to call .attr("id", id) it wont work. I had that as a parameter because it's required for correctness

where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut data_list = Self::new("datalist");
data_list.attr("id", id);

let mut options: Vec<HtmlNode> = Vec::new();

for item in list {
let l = item.as_ref();
let mut option = HtmlTag::new("option");
option.attr("value", l);
options.push(HtmlNode::Tag(option));
}

data_list.children = options;
data_list
}

/// Adds an attribute to the HTML tag.
///
/// # Safety
Expand Down
11 changes: 6 additions & 5 deletions cot-macros/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ impl FormDeriveBuilder {
Vec::new()
};
quote!(#field_ident: {
let options = #crate_ident::form::FormFieldOptions {
id: stringify!(#field_ident).to_owned(),
name: #name.to_owned(),
required: true,
};
let options = #crate_ident::form::FormFieldOptions::builder()
.id(stringify!(#field_ident).to_owned())
.name(#name.to_owned())
.required(true)
.build();

type Field = <#ty as #crate_ident::form::AsFormField>::Type;
type CustomOptions = <Field as #crate_ident::form::FormField>::CustomOptions;
let mut custom_options: CustomOptions = ::core::default::Default::default();
Expand Down
2 changes: 1 addition & 1 deletion cot/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ mod tests {
let id_2 = session.id();

assert!(id_2.is_some());
assert!(id_1 != id_2);
assert_ne!(id_1, id_2);
}

/// Test that the user is logged out when there is an invalid user ID in the
Expand Down
50 changes: 49 additions & 1 deletion cot/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use cot_core::headers::{MULTIPART_FORM_CONTENT_TYPE, URLENCODED_FORM_CONTENT_TYP
/// fields. If the form fields are not safe to render as HTML, the form context
/// will not be safe to render as HTML either.
pub use cot_macros::Form;
use derive_builder::Builder;
use derive_more::with_trait::Debug;
pub use field_value::{FormFieldValue, FormFieldValueError};
use http_body_util::BodyExt;
Expand Down Expand Up @@ -508,7 +509,9 @@ pub trait FormContext: Debug {
}

/// Generic options valid for all types of form fields.
#[derive(Debug)]
#[derive(Debug, Default, Clone, Builder)]
#[builder(build_fn(skip, error = std::convert::Infallible))]
#[non_exhaustive]
pub struct FormFieldOptions {
/// The HTML ID of the form field.
pub id: String,
Expand All @@ -521,6 +524,51 @@ pub struct FormFieldOptions {
pub required: bool,
}

impl FormFieldOptions {
/// Creates a new [`FormFieldOptionsBuilder`] to build a
/// [`FormFieldOptions`].
///
/// # Examples
///
/// ```
/// use cot::form::FormFieldOptions;
///
/// let options = FormFieldOptions::builder()
/// .name("field_name".to_owned())
/// .id("field_id".to_owned())
/// .required(true)
/// .build();
/// ```
#[must_use]
pub fn builder() -> FormFieldOptionsBuilder {
FormFieldOptionsBuilder::default()
}
}
impl FormFieldOptionsBuilder {
/// Builds the [`FormFieldOptions`], falling back to defaults for any field
/// that wasn't explicitly set.
///
/// # Examples
///
/// ```
/// use cot::form::FormFieldOptions;
///
/// let options = FormFieldOptions::builder()
/// .name("field_name".to_owned())
/// .id("field_id".to_owned())
/// .required(true)
/// .build();
/// ```
#[must_use]
pub fn build(&self) -> FormFieldOptions {
FormFieldOptions {
id: self.id.clone().unwrap_or_default(),
name: self.name.clone().unwrap_or_default(),
required: self.required.unwrap_or_default(),
}
}
}

/// A form field.
///
/// This trait is used to define a type of field that can be used in a form. It
Expand Down
Loading
Loading