Last active
March 15, 2024 02:55
-
-
Save PsichiX/1f226b33dde4539d008b040fa2c963b1 to your computer and use it in GitHub Desktop.
Intuicio - Name and Text framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use intuicio_core::prelude::*; | |
use intuicio_data::prelude::*; | |
use intuicio_derive::*; | |
use intuicio_framework_text::{Name, Text}; | |
use std::collections::HashMap; | |
#[derive(IntuicioStruct, Debug, Default)] | |
#[intuicio(module_name = "test")] | |
struct Loc { | |
#[intuicio(ignore)] | |
map: HashMap<Name, Text>, | |
} | |
#[intuicio_methods(module_name = "test")] | |
impl Loc { | |
#[intuicio_method()] | |
pub fn get(this: ManagedBox<Self>, key: Name) -> Text { | |
this.read() | |
.unwrap() | |
.map | |
.get(&key) | |
.cloned() | |
.unwrap_or_default() | |
} | |
#[intuicio_method()] | |
pub fn set(mut this: ManagedBox<Self>, key: Name, value: Text) { | |
this.write().unwrap().map.insert(key, value); | |
} | |
} | |
#[test] | |
fn test_text() { | |
let mut context = Context::new(10240, 10240); | |
let mut registry = Registry::default().with_basic_types(); | |
registry.add_type(Name::define_struct(®istry)); | |
registry.add_type(Text::define_struct(®istry)); | |
registry.add_type(NativeStructBuilder::new::<ManagedBox<Loc>>().build()); | |
registry.add_type(Loc::define_struct(®istry)); | |
let get = registry.add_function(Loc::get__define_function(®istry)); | |
let set = registry.add_function(Loc::set__define_function(®istry)); | |
let foo_name = Name::new_static("foo"); | |
let bar_name = Name::new_static("bar"); | |
let foo_text = Text::new("Foo"); | |
let bar_text = Text::new("Bar"); | |
let loc = ManagedBox::<Loc>::default(); | |
Loc::set(loc.clone(), foo_name, foo_text.clone()); | |
let (v,) = get.call::<(Text,), _>(&mut context, ®istry, (loc.clone(), foo_name), false); | |
assert_eq!(v, foo_text); | |
set.call::<(), _>( | |
&mut context, | |
®istry, | |
(loc.clone(), bar_name, bar_text.clone()), | |
false, | |
); | |
assert_eq!(Loc::get(loc, bar_name), bar_text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment