isokit: project layer, Studio GUI, MCP server, viewer, CLI, demo project and docs
This commit is contained in:
1 parent
95c80ec6b1
commit
564aeb75f6
47 files changed
+5978
-366
No files matched your search
@@ -129,13 +129,27 @@ impl PrefabMeta {
|
||||
pub fn resolve(&self, props: &Map<String, Value>) -> Map<String, Value> {
|
||||
let mut out = props.clone();
|
||||
for d in &self.props {
|
||||
let v = out.get(&d.key).cloned().unwrap_or_else(|| d.default.clone());
|
||||
let v = out
|
||||
.get(&d.key)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| d.default.clone());
|
||||
let v = match (d.kind.as_str(), &v) {
|
||||
("float", Value::Number(n)) => n.as_f64().map(Value::from).unwrap_or(v),
|
||||
("float", Value::String(s)) => s.parse::<f64>().map(Value::from).unwrap_or(d.default.clone()),
|
||||
("int", Value::Number(n)) => n.as_f64().map(|f| Value::from(f.round() as i64)).unwrap_or(v),
|
||||
("int", Value::String(s)) => s.parse::<f64>().map(|f| Value::from(f.round() as i64)).unwrap_or(d.default.clone()),
|
||||
("bool", Value::String(s)) => Value::Bool(matches!(s.as_str(), "true" | "1" | "yes" | "on")),
|
||||
("float", Value::String(s)) => s
|
||||
.parse::<f64>()
|
||||
.map(Value::from)
|
||||
.unwrap_or(d.default.clone()),
|
||||
("int", Value::Number(n)) => n
|
||||
.as_f64()
|
||||
.map(|f| Value::from(f.round() as i64))
|
||||
.unwrap_or(v),
|
||||
("int", Value::String(s)) => s
|
||||
.parse::<f64>()
|
||||
.map(|f| Value::from(f.round() as i64))
|
||||
.unwrap_or(d.default.clone()),
|
||||
("bool", Value::String(s)) => {
|
||||
Value::Bool(matches!(s.as_str(), "true" | "1" | "yes" | "on"))
|
||||
}
|
||||
("bool", Value::Number(n)) => Value::Bool(n.as_f64().unwrap_or(0.0) != 0.0),
|
||||
_ => v,
|
||||
};
|
||||
@@ -156,13 +170,26 @@ impl Doc {
|
||||
pub fn free_id(&self, base: &str) -> String {
|
||||
let base: String = base
|
||||
.chars()
|
||||
.map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c.to_ascii_lowercase() } else { '-' })
|
||||
.map(|c| {
|
||||
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
|
||||
c.to_ascii_lowercase()
|
||||
} else {
|
||||
'-'
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let base = if base.is_empty() { "obj".to_string() } else { base };
|
||||
let base = if base.is_empty() {
|
||||
"obj".to_string()
|
||||
} else {
|
||||
base
|
||||
};
|
||||
if self.get(&base).is_none() {
|
||||
return base;
|
||||
}
|
||||
(2..).map(|i| format!("{base}-{i}")).find(|id| self.get(id).is_none()).unwrap()
|
||||
(2..)
|
||||
.map(|i| format!("{base}-{i}"))
|
||||
.find(|id| self.get(id).is_none())
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,19 +200,38 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn object_round_trips_compactly() {
|
||||
let o: Object = serde_json::from_value(json!({"id": "t1", "type": "tree", "x": 3, "y": 4.5})).unwrap();
|
||||
let o: Object =
|
||||
serde_json::from_value(json!({"id": "t1", "type": "tree", "x": 3, "y": 4.5})).unwrap();
|
||||
assert_eq!(o.kind, "tree");
|
||||
let back = serde_json::to_value(&o).unwrap();
|
||||
assert_eq!(back, json!({"id": "t1", "type": "tree", "x": 3.0, "y": 4.5}));
|
||||
assert_eq!(
|
||||
back,
|
||||
json!({"id": "t1", "type": "tree", "x": 3.0, "y": 4.5})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn defaults_and_coercion() {
|
||||
let m = PrefabMeta {
|
||||
props: vec![
|
||||
PropDef { key: "r".into(), kind: "float".into(), default: json!(1.5), ..Default::default() },
|
||||
PropDef { key: "n".into(), kind: "int".into(), default: json!(2), ..Default::default() },
|
||||
PropDef { key: "on".into(), kind: "bool".into(), default: json!(false), ..Default::default() },
|
||||
PropDef {
|
||||
key: "r".into(),
|
||||
kind: "float".into(),
|
||||
default: json!(1.5),
|
||||
..Default::default()
|
||||
},
|
||||
PropDef {
|
||||
key: "n".into(),
|
||||
kind: "int".into(),
|
||||
default: json!(2),
|
||||
..Default::default()
|
||||
},
|
||||
PropDef {
|
||||
key: "on".into(),
|
||||
kind: "bool".into(),
|
||||
default: json!(false),
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -199,8 +245,16 @@ mod tests {
|
||||
fn free_ids() {
|
||||
let mut d = Doc::default();
|
||||
assert_eq!(d.free_id("Tree"), "tree");
|
||||
d.objects.push(Object { id: "tree".into(), kind: "tree".into(), ..Default::default() });
|
||||
d.objects.push(Object { id: "tree-2".into(), kind: "tree".into(), ..Default::default() });
|
||||
d.objects.push(Object {
|
||||
id: "tree".into(),
|
||||
kind: "tree".into(),
|
||||
..Default::default()
|
||||
});
|
||||
d.objects.push(Object {
|
||||
id: "tree-2".into(),
|
||||
kind: "tree".into(),
|
||||
..Default::default()
|
||||
});
|
||||
assert_eq!(d.free_id("tree"), "tree-3");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user