diff --git a/Cargo.lock b/Cargo.lock index 3cfa93d7..2376e899 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1698,7 +1698,7 @@ dependencies = [ "gloo-console 0.4.0", "gloo-timers 0.4.0", "implicit-clone", - "luminescent-dreams-common", + "luminescent-dreams-common 0.1.0", "serde", "serde_json", "thiserror 2.0.19", @@ -2812,6 +2812,28 @@ dependencies = [ "tokio-macros", ] +[[package]] +name = "luminescent-dreams-common" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5426c81c7c05f72534e06ae7e395c47451ccc6bff0021b6db4a1236deef3285" +dependencies = [ + "luminescent-dreams-common-derive", + "nonempty", + "tokio", + "tokio-macros", +] + +[[package]] +name = "luminescent-dreams-common-derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "932c6d54c3029e6e8e6d52574944ca0e57190ad95ff22db89c8d124a0f75d2c8" +dependencies = [ + "quote", + "syn 3.0.3", +] + [[package]] name = "markdown" version = "1.0.0" @@ -2913,6 +2935,12 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nonempty" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9737e026353e5cd0736f98eddae28665118eb6f6600902a7f50db585621fecb6" + [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -5047,7 +5075,7 @@ dependencies = [ "implicit-clone", "jiff", "lazy_static", - "luminescent-dreams-common", + "luminescent-dreams-common 0.2.0", "markdown", "password-strength", "serde", diff --git a/Cargo.toml b/Cargo.toml index daf5a095..dcd2f6a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,7 @@ http = { version = "1.3" } implicit-clone = { version = "0.6" } jiff = { version = "0.2.18" } lazy_static = { version = "1.5" } -luminescent-dreams-common = { version = "0.1" } +luminescent-dreams-common = { version = "0.2" } markdown = { version = "1.0" } password-strength = { version = "1" } serde = { version = "1.0", features = ["derive", "serde_derive"] } diff --git a/visions/ui/src/types.rs b/visions/ui/src/types.rs index 49ca2105..fbb6e0b5 100644 --- a/visions/ui/src/types.rs +++ b/visions/ui/src/types.rs @@ -1,3 +1,4 @@ +use implicit_clone::{ImplicitClone, unsync::IArray}; use yew::prelude::*; #[derive(Clone, Copy, PartialEq)] @@ -49,3 +50,65 @@ pub fn AuthenticationOptionElement( } } */ + +/// An `IdSet` contains a bunch of items whose uniqueness is dictated by the Identifiable trait. +/// Internally, the `IdSet` contains an `IArray`, which makes cloning it cheap. +#[derive(Clone, ImplicitClone, PartialEq)] +pub struct IdSet(IArray); + +impl IdSet { + pub fn get(&self, id: &T::Id) -> Option<&T> { + self.0.iter().find(|i| i.id() == *id) + } + + pub fn insert(self, item: T) -> Self { + let item_id = item.id(); + IdSet( + self.0 + .into_iter() + .filter(|i| i.id() != item_id) + .chain([item]) + .collect::>() + .into(), + ) + } + + pub fn remove(self, id: T::Id) -> Self { + let item_id = id; + IdSet( + self.0 + .into_iter() + .filter(|i| i.id() != item_id) + .collect::>() + .into(), + ) + } + + /* + pub fn iter(&self) -> std::iter::Iter { + todo!() + } + */ +} + +impl Default for IdSet { + fn default() -> Self { + Self(IArray::default()) + } +} + +impl From> + for IArray +{ + fn from(set: IdSet) -> Self { + set.0 + } +} + +impl From> + for IdSet +{ + fn from(lst: Vec) -> Self { + Self(lst.into()) + } +}