lib-nested/nested/src/make_editor.rs

198 lines
8.2 KiB
Rust
Raw Normal View History

2022-05-08 23:30:49 +02:00
use {
crate::{
2022-06-19 23:13:21 +02:00
core::{TypeLadder, Context},
terminal::{TerminalView},
tree_nav::{TerminalTreeEditor},
2022-05-08 23:30:49 +02:00
integer::PosIntEditor,
2022-06-19 23:13:21 +02:00
list::{ListEditor, PTYListEditor},
sequence::{decorator::{SeqDecorStyle}},
product::editor::ProductEditor,
char_editor::CharEditor
2022-05-08 23:30:49 +02:00
},
2022-06-26 00:49:35 +02:00
cgmath::{Vector2, Point2},
2022-06-19 23:13:21 +02:00
std::sync::{Arc, RwLock},
2022-05-08 23:30:49 +02:00
};
pub fn make_editor(ctx: Arc<RwLock<Context>>, t: &TypeLadder, depth: usize) -> Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>> {
let c = ctx.read().unwrap();
if t[0] == c.type_term_from_str("( PosInt 16 BigEndian )").unwrap() {
Arc::new(RwLock::new(PosIntEditor::new(16))) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
} else if t[0] == c.type_term_from_str("( PosInt 10 BigEndian )").unwrap() {
Arc::new(RwLock::new(PosIntEditor::new(10))) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
} else if t[0] == c.type_term_from_str("( String )").unwrap() {
2022-06-19 23:13:21 +02:00
Arc::new(RwLock::new(
PTYListEditor::new(
Box::new(|| {
Arc::new(RwLock::new(CharEditor::new()))
}),
SeqDecorStyle::DoubleQuote,
depth
)
))
2022-05-08 23:30:49 +02:00
} else if t[0] == c.type_term_from_str("( List String )").unwrap() {
Arc::new(RwLock::new(
PTYListEditor::new(
Box::new({
let d = depth + 1;
let ctx = ctx.clone();
move || {
make_editor(
ctx.clone(),
&vec![ctx.read().unwrap().type_term_from_str("( String )").unwrap()],
d
)
}
}),
SeqDecorStyle::EnumSet,
depth
)
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
2022-05-08 23:30:49 +02:00
} else if t[0] == c.type_term_from_str("( List Char )").unwrap() {
2022-06-19 23:13:21 +02:00
Arc::new(RwLock::new(
PTYListEditor::new(
Box::new(
|| { Arc::new(RwLock::new(CharEditor::new())) }
),
SeqDecorStyle::Plain,
depth+1
2022-06-19 23:13:21 +02:00
)
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
2022-05-08 23:30:49 +02:00
} else if t[0] == c.type_term_from_str("( List )").unwrap() {
2022-06-19 23:13:21 +02:00
Arc::new(RwLock::new(
PTYListEditor::new(
Box::new(|| {
Arc::new(RwLock::new(PosIntEditor::new(16)))
}),
SeqDecorStyle::EnumSet,
depth
)
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
2022-05-08 23:30:49 +02:00
} else if t[0] == c.type_term_from_str("( Path )").unwrap() {
2022-06-19 23:13:21 +02:00
Arc::new(RwLock::new(PTYListEditor::new(
Box::new({
let d= depth+1;
2022-06-19 23:13:21 +02:00
move || {
Arc::new(RwLock::new(PTYListEditor::new(
Box::new(|| {
Arc::new(RwLock::new(CharEditor::new()))
}),
SeqDecorStyle::Plain,
d
)))
}}),
SeqDecorStyle::Path,
depth
2022-05-08 23:30:49 +02:00
))) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
} else if t[0] == c.type_term_from_str("( List Path )").unwrap() {
Arc::new(RwLock::new(
PTYListEditor::new(
Box::new({
let d = depth + 1;
let ctx = ctx.clone();
move || {
make_editor(
ctx.clone(),
&vec![ctx.read().unwrap().type_term_from_str("( Path )").unwrap()],
d
)
}
}),
SeqDecorStyle::EnumSet,
depth
)
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
2022-05-08 23:30:49 +02:00
} else if t[0] == c.type_term_from_str("( List RGB )").unwrap() {
2022-06-19 23:13:21 +02:00
Arc::new(RwLock::new(
PTYListEditor::<dyn TerminalTreeEditor + Send +Sync>::new(
2022-08-12 18:57:40 +02:00
{
2022-06-19 23:13:21 +02:00
let d = depth+1;
let ctx = ctx.clone();
2022-08-12 18:57:40 +02:00
Box::new(move || {
2022-06-19 23:13:21 +02:00
make_editor(ctx.clone(), &vec![ ctx.read().unwrap().type_term_from_str("( RGB )").unwrap() ], d)
2022-08-12 18:57:40 +02:00
})
},
2022-06-19 23:13:21 +02:00
SeqDecorStyle::VerticalSexpr,
depth
)
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
2022-05-08 23:30:49 +02:00
} else if t[0] == c.type_term_from_str("( RGB )").unwrap() {
Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone())
2022-06-26 00:49:35 +02:00
.with_t(Point2::new(0, 0), "{")
.with_t(Point2::new(1, 1), "r: ")
.with_n(Point2::new(2, 1), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() ] )
.with_t(Point2::new(1, 2), "g: ")
.with_n(Point2::new(2, 2), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() ] )
.with_t(Point2::new(1, 3), "b: ")
.with_n(Point2::new(2, 3), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() ] )
2022-08-12 18:57:40 +02:00
.with_t(Point2::new(0, 4), "}")
2022-05-08 23:30:49 +02:00
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
} else if t[0] == c.type_term_from_str("( Vec3i )").unwrap() {
Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone())
2022-06-26 00:49:35 +02:00
.with_t(Point2::new(0, 0), "{")
.with_t(Point2::new(1, 1), "x: ")
.with_n(Point2::new(2, 1), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] )
.with_t(Point2::new(1, 2), "y: ")
.with_n(Point2::new(2, 2), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] )
.with_t(Point2::new(1, 3), "z: ")
.with_n(Point2::new(2, 3), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] )
2022-08-12 18:57:40 +02:00
.with_t(Point2::new(0, 4), "}")
2022-05-08 23:30:49 +02:00
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
2022-06-26 00:49:35 +02:00
} else if t[0] == c.type_term_from_str("( Json )").unwrap() {
Arc::new(RwLock::new(
PTYListEditor::<dyn TerminalTreeEditor + Send + Sync>::new(
Box::new({
let ctx = ctx.clone();
move || {
Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone())
.with_n(Point2::new(0, 0), vec![ ctx.read().unwrap().type_term_from_str("( String )").unwrap() ] )
.with_t(Point2::new(1, 0), ": ")
.with_n(Point2::new(2, 0), vec![ ctx.read().unwrap().type_term_from_str("( Json )").unwrap() ] )
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
}
}),
SeqDecorStyle::VerticalSexpr,
depth
)
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
2022-05-08 23:30:49 +02:00
} else if t[0] == c.type_term_from_str("( List Term )").unwrap() {
2022-06-19 23:13:21 +02:00
Arc::new(RwLock::new(
PTYListEditor::<dyn TerminalTreeEditor + Send + Sync>::new(
Box::new({
let ctx = ctx.clone();
move || {
make_editor(ctx.clone(), &vec![ ctx.read().unwrap().type_term_from_str("( Term )").unwrap() ], depth+1)
}
}),
SeqDecorStyle::Tuple,
depth
)
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
2022-05-08 23:30:49 +02:00
} else { // else: term
Arc::new(RwLock::new(
2022-06-26 00:49:35 +02:00
PTYListEditor::new(
2022-08-12 18:57:40 +02:00
|| {
2022-06-26 00:49:35 +02:00
Arc::new(RwLock::new(CharEditor::new()))
2022-08-12 18:57:40 +02:00
},
2022-06-26 00:49:35 +02:00
SeqDecorStyle::DoubleQuote,
depth
)
))
2022-05-08 23:30:49 +02:00
}
}