From 8d637a6f325a0c42d7303a487457f65f4fca121c Mon Sep 17 00:00:00 2001
From: Michael Sippel <micha@fragmental.art>
Date: Tue, 6 Aug 2024 16:29:28 +0200
Subject: [PATCH] add ReprTree::from_str() to simplify creation of initial
 representation nodes

---
 examples/tty-03-string/src/main.rs       | 18 +++++++++++-------
 examples/tty-04-posint/src/main.rs       | 22 ++++++++++++++--------
 lib-nested-core/src/repr_tree/context.rs |  2 +-
 lib-nested-core/src/repr_tree/node.rs    | 21 +++++++++++++++++++++
 4 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/examples/tty-03-string/src/main.rs b/examples/tty-03-string/src/main.rs
index c0b1bfc..3cdc66e 100644
--- a/examples/tty-03-string/src/main.rs
+++ b/examples/tty-03-string/src/main.rs
@@ -43,20 +43,24 @@ async fn main() {
 
     /* Create a Representation-Tree of type <List Char>
      */
-    let mut rt_string = ReprTree::new_arc( Context::parse(&ctx, "<List Char>") );
-
-    rt_string.insert_leaf(
-        Context::parse(&ctx, "<List~Vec EditTree>"),
-        nested::repr_tree::ReprLeaf::from_vec_buffer( VecBuffer::<Arc<RwLock<EditTree>>>::new() )
+    let mut rt_string = ReprTree::from_str(
+        Context::parse(&ctx, "<List Char>~<Vec Char>"),
+        "hello world"
     );
+
+    /* create EditTree
+     */
     ctx.read().unwrap().apply_morphism(
         &rt_string,
         &laddertypes::MorphismType {
-            src_type: Context::parse(&ctx, "<List Char~EditTree>~<Vec EditTree>"),
+            src_type: Context::parse(&ctx, "<List~Vec Char>"),
             dst_type: Context::parse(&ctx, "<List Char> ~ EditTree")
         }
     );
 
+    // .. avoid cycle of projections..
+    rt_string.write().unwrap().detach(&ctx);
+
     /* Setup the Editor-View for this ReprTree
      */
     let edittree_list = ctx.read().unwrap()
@@ -65,7 +69,7 @@ async fn main() {
             SingletonBuffer::new(0).get_port()
         ).unwrap();
 
-    /* In order to get acces to the values that are modified by the Editor,
+    /* In order to get access to the values that are modified by the Editor,
      * we apply a morphism that, given the List of Edit-Trees, extracts
      * the value from each EditTree and shows them in a ListView.
      */
diff --git a/examples/tty-04-posint/src/main.rs b/examples/tty-04-posint/src/main.rs
index 87ecfc1..b03bdd1 100644
--- a/examples/tty-04-posint/src/main.rs
+++ b/examples/tty-04-posint/src/main.rs
@@ -66,16 +66,22 @@ async fn main() {
     nested_tty::setup_edittree_hook(&ctx);
 
     /* Create a Representation-Tree of type `ℕ`
+     * with a specific representation-path (big-endian hexadecimal string)
      */
-    let mut rt_int = ReprTree::new_arc( Context::parse(&ctx, "ℕ") );
+    let mut rt_int = nested::repr_tree::ReprTree::from_str(
+        /* TYPE */
+        Context::parse(&ctx, "
+              ℕ
+            ~ <PosInt 16 BigEndian>
+            ~ <Seq <Digit 16>>
+            ~ <List <Digit 16>>
+            ~ <List Char>
+            ~ <Vec Char>
+        "),
 
-    /* Add a specific Representation-Path (big-endian hexadecimal)
-     */
-    rt_int.insert_leaf(
-        Context::parse(&ctx, "<PosInt 16 BigEndian>~<Seq <Digit 16>>~<List <Digit 16>>~<List Char>~<Vec Char>"),
-        nested::repr_tree::ReprLeaf::from_vec_buffer(
-            VecBuffer::with_data(vec![ 'c', 'f', 'f' ]
-        )));
+        /* VALUE */
+        "cff"
+    );
 
     /* initially copy values from Vec to EditTree...
      */
diff --git a/lib-nested-core/src/repr_tree/context.rs b/lib-nested-core/src/repr_tree/context.rs
index 6bbff0b..5e93f51 100644
--- a/lib-nested-core/src/repr_tree/context.rs
+++ b/lib-nested-core/src/repr_tree/context.rs
@@ -91,7 +91,7 @@ impl Context {
                             src_type: src_type.clone(),
                             dst_type: dst_type.clone()
                         }
-                    ) {                   
+                    ) {
                         let mut rt = rt.descend( τ ).expect("descend src repr");
                         (m.setup_projection)( &mut rt, &σ );
                     }
diff --git a/lib-nested-core/src/repr_tree/node.rs b/lib-nested-core/src/repr_tree/node.rs
index d6c3f99..6882b7d 100644
--- a/lib-nested-core/src/repr_tree/node.rs
+++ b/lib-nested-core/src/repr_tree/node.rs
@@ -139,6 +139,27 @@ impl ReprTree {
         Arc::new(RwLock::new(rt))
     }
 
+
+    pub fn from_str(
+        type_tag: impl Into<TypeTerm>,
+        val: &str
+    ) -> Arc<RwLock<Self>> {
+        let mut lnf = type_tag.into().get_lnf_vec();
+
+        let mut rt = ReprTree::from_vec_buffer(
+            lnf.pop().unwrap(),
+            VecBuffer::with_data( val.chars().collect() )
+        );
+
+        while let Some(t) = lnf.pop() {
+            let mut new_rt = ReprTree::new_arc(t);
+            new_rt.insert_branch(rt);
+            rt = new_rt;
+        }
+
+        rt
+    }
+
     pub fn from_vec_buffer<T>( type_tag: impl Into<TypeTerm>, buf: VecBuffer<T> ) -> Arc<RwLock<Self>>
     where T: Clone + Send + Sync + 'static
     {