pub use component_shape::{
ComponentCapabilities, ComponentPrototyping, ComponentShapeFor, ComponentShapeMetadata,
ComponentSuffix, McpInput, McpInputShape, McpPrimitiveKind, McpRangeBoundKind,
RenderCapability, ValueBindingCapability, ValueChange,
is_valid_component_suffix as is_valid_component_field_suffix,
};
pub use component_shape_gpui::{
DeclaredGpuiComponentShape, DefaultGpuiComponentShapeBuilder, GpuiComponentEventOf,
GpuiComponentRender, GpuiComponentShape, GpuiComponentShapeBuilder, GpuiComponentShapeFor,
GpuiComponentStateOf, GpuiComponentStateValueBinding, GpuiComponentValueBinding,
NoGpuiRenderComponent, build_component_shape, seed_value_binding_state, value_change,
};
pub trait GpuiFormComponentShapePolicy {
type ValueStoragePolicy: ComponentValueStoragePolicy;
}
mod sealed {
pub trait ValueStoragePolicy {}
}
pub trait ComponentValueStoragePolicy: sealed::ValueStoragePolicy {
const REQUIRES_VALUE: bool;
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct RequiredValueStorage;
impl sealed::ValueStoragePolicy for RequiredValueStorage {}
impl ComponentValueStoragePolicy for RequiredValueStorage {
const REQUIRES_VALUE: bool = true;
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct DirectValueStorage;
impl sealed::ValueStoragePolicy for DirectValueStorage {}
impl ComponentValueStoragePolicy for DirectValueStorage {
const REQUIRES_VALUE: bool = false;
}
pub trait ValueStorage<T>: ComponentValueStoragePolicy {
type Storage;
fn present(value: T) -> Self::Storage;
fn present_unless_default(value: T, default: T) -> Self::Storage
where
T: PartialEq;
fn map_into_value<Output, Present, Missing>(
storage: Self::Storage,
present: Present,
missing: Missing,
) -> Output
where
Present: FnOnce(T) -> Output,
Missing: FnOnce() -> Output;
fn try_map_into_value<Output, Error, Present>(
storage: Self::Storage,
present: Present,
missing: Error,
) -> Result<Output, Error>
where
Present: FnOnce(T) -> Output;
fn map_present_cloned<Output, Present>(
storage: &Self::Storage,
present: Present,
) -> Option<Output>
where
T: Clone,
Present: FnOnce(T) -> Output;
fn is_present(storage: &Self::Storage) -> bool;
}
#[diagnostic::on_unimplemented(
message = "gpui-form cannot synthesize direct storage for `{T}` with this component shape policy",
note = "add an intent-scoped `default = ...`, make `{T}` implement `Default`, or use `RequiredValueStorage` for the shape's form storage policy"
)]
pub trait DefaultValueStorage<T>: ValueStorage<T> {
fn default_storage() -> Self::Storage;
}
pub trait InfallibleValueStorage<T>: ValueStorage<T> {
fn map_into_value<Output, Present>(storage: Self::Storage, present: Present) -> Output
where
Present: FnOnce(T) -> Output;
}
impl<T> ValueStorage<T> for RequiredValueStorage {
type Storage = Option<T>;
fn present(value: T) -> Self::Storage {
Some(value)
}
fn present_unless_default(value: T, default: T) -> Self::Storage
where
T: PartialEq,
{
if value == default { None } else { Some(value) }
}
fn map_into_value<Output, Present, Missing>(
storage: Self::Storage,
present: Present,
missing: Missing,
) -> Output
where
Present: FnOnce(T) -> Output,
Missing: FnOnce() -> Output,
{
match storage {
Some(value) => present(value),
None => missing(),
}
}
fn try_map_into_value<Output, Error, Present>(
storage: Self::Storage,
present: Present,
missing: Error,
) -> Result<Output, Error>
where
Present: FnOnce(T) -> Output,
{
storage.map(present).ok_or(missing)
}
fn map_present_cloned<Output, Present>(
storage: &Self::Storage,
present: Present,
) -> Option<Output>
where
T: Clone,
Present: FnOnce(T) -> Output,
{
storage.clone().map(present)
}
fn is_present(storage: &Self::Storage) -> bool {
storage.is_some()
}
}
impl<T> DefaultValueStorage<T> for RequiredValueStorage {
fn default_storage() -> Self::Storage {
None
}
}
impl<T> ValueStorage<T> for DirectValueStorage {
type Storage = T;
fn present(value: T) -> Self::Storage {
value
}
fn present_unless_default(value: T, _default: T) -> Self::Storage
where
T: PartialEq,
{
value
}
fn map_into_value<Output, Present, Missing>(
storage: Self::Storage,
present: Present,
_missing: Missing,
) -> Output
where
Present: FnOnce(T) -> Output,
Missing: FnOnce() -> Output,
{
present(storage)
}
fn try_map_into_value<Output, Error, Present>(
storage: Self::Storage,
present: Present,
_missing: Error,
) -> Result<Output, Error>
where
Present: FnOnce(T) -> Output,
{
Ok(present(storage))
}
fn map_present_cloned<Output, Present>(
storage: &Self::Storage,
present: Present,
) -> Option<Output>
where
T: Clone,
Present: FnOnce(T) -> Output,
{
Some(present(storage.clone()))
}
fn is_present(_storage: &Self::Storage) -> bool {
true
}
}
impl<T: Default> DefaultValueStorage<T> for DirectValueStorage {
fn default_storage() -> Self::Storage {
T::default()
}
}
impl<T> InfallibleValueStorage<T> for DirectValueStorage {
fn map_into_value<Output, Present>(storage: Self::Storage, present: Present) -> Output
where
Present: FnOnce(T) -> Output,
{
present(storage)
}
}
#[cfg(test)]
mod tests {
use super::{
ComponentPrototyping, ComponentValueStoragePolicy as _, DefaultValueStorage,
DirectValueStorage, InfallibleValueStorage, RequiredValueStorage, ValueStorage,
is_valid_component_field_suffix,
};
#[test]
fn component_field_suffix_validator_accepts_identifier_suffixes() {
assert!(is_valid_component_field_suffix("input"));
assert!(is_valid_component_field_suffix("_input"));
assert!(is_valid_component_field_suffix("input_2"));
}
#[test]
fn component_field_suffix_validator_rejects_non_identifier_suffixes() {
assert!(!is_valid_component_field_suffix(""));
assert!(!is_valid_component_field_suffix("_"));
assert!(!is_valid_component_field_suffix("2input"));
assert!(!is_valid_component_field_suffix("input-field"));
}
#[test]
#[should_panic(expected = "component suffix must be a non-empty ASCII identifier suffix")]
fn component_prototyping_rejects_invalid_field_suffixes() {
let _ = ComponentPrototyping::new().field_suffix("input-field");
}
#[test]
fn required_storage_preserves_presence_and_default_semantics() {
assert!(std::hint::black_box(RequiredValueStorage::REQUIRES_VALUE));
assert_eq!(
<RequiredValueStorage as ValueStorage<i32>>::present(7),
Some(7)
);
assert_eq!(
<RequiredValueStorage as ValueStorage<i32>>::present_unless_default(7, 7),
None
);
assert_eq!(
<RequiredValueStorage as ValueStorage<i32>>::present_unless_default(8, 7),
Some(8)
);
assert_eq!(
<RequiredValueStorage as DefaultValueStorage<i32>>::default_storage(),
None
);
assert!(<RequiredValueStorage as ValueStorage<i32>>::is_present(
&Some(1)
));
assert!(!<RequiredValueStorage as ValueStorage<i32>>::is_present(
&None
));
}
#[test]
fn required_storage_maps_present_and_missing_values() {
let present = <RequiredValueStorage as ValueStorage<i32>>::map_into_value(
Some(4),
|value| value * 2,
|| 99,
);
let missing = <RequiredValueStorage as ValueStorage<i32>>::map_into_value(
None,
|value| value * 2,
|| 99,
);
assert_eq!((present, missing), (8, 99));
assert_eq!(
<RequiredValueStorage as ValueStorage<i32>>::try_map_into_value(
Some(4),
|value| value * 2,
"missing"
),
Ok(8)
);
assert_eq!(
<RequiredValueStorage as ValueStorage<i32>>::try_map_into_value(
None,
|value| value * 2,
"missing"
),
Err("missing")
);
assert_eq!(
<RequiredValueStorage as ValueStorage<String>>::map_present_cloned(
&Some("form".to_owned()),
|value| value.len()
),
Some(4)
);
assert_eq!(
<RequiredValueStorage as ValueStorage<String>>::map_present_cloned(&None, |value| {
value.len()
}),
None
);
}
#[test]
fn direct_storage_always_maps_a_present_value() {
assert!(!std::hint::black_box(DirectValueStorage::REQUIRES_VALUE));
assert_eq!(<DirectValueStorage as ValueStorage<i32>>::present(7), 7);
assert_eq!(
<DirectValueStorage as ValueStorage<i32>>::present_unless_default(7, 7),
7
);
assert_eq!(
<DirectValueStorage as DefaultValueStorage<i32>>::default_storage(),
0
);
assert!(<DirectValueStorage as ValueStorage<i32>>::is_present(&7));
assert_eq!(
<DirectValueStorage as ValueStorage<i32>>::map_into_value(4, |value| value * 2, || 99),
8
);
assert_eq!(
<DirectValueStorage as ValueStorage<i32>>::try_map_into_value(
4,
|value| value * 2,
"missing"
),
Ok(8)
);
assert_eq!(
<DirectValueStorage as ValueStorage<String>>::map_present_cloned(
&"form".to_owned(),
|value| value.len()
),
Some(4)
);
assert_eq!(
<DirectValueStorage as InfallibleValueStorage<i32>>::map_into_value(4, |value| value
* 2),
8
);
}
}