1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
/*
Hurry Curry! - a game about cooking
Copyright (C) 2025 Hurry Curry! Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use anyhow::{Result, anyhow};
use hurrycurry_protocol::{book::Diagram, glam::Vec2};
use serde::Deserialize;
use std::{
collections::BTreeMap,
fmt::Write as W2,
io::Write,
process::{Command, Stdio},
};
pub struct Layout {
pub nodes: BTreeMap<String, Vec2>,
pub edges: Vec<(usize, usize)>,
}
pub fn diagram_layout(diagram: &mut Diagram) -> Result<()> {
let mut child = Command::new("dot")
.arg("-Tjson")
.arg("-Kdot")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
let mut out = String::new();
writeln!(out, "digraph {{")?;
for (i, _) in diagram.nodes.iter().enumerate() {
writeln!(out, "k{i} [width=1, height=1]")?;
}
for edge in &diagram.edges {
writeln!(out, "k{} -> k{}", edge.src, edge.dst)?;
}
writeln!(out, "}}")?;
child.stdin.as_mut().unwrap().write_all(out.as_bytes())?;
let output = child.wait_with_output()?;
#[derive(Deserialize)]
struct Out {
objects: Vec<Object>,
}
#[derive(Deserialize)]
struct Object {
name: String,
pos: String,
}
let graph: Out = serde_json::from_slice(&output.stdout)?;
for o in graph.objects {
let pos = o.pos.split_once(",").ok_or(anyhow!("malformed position"))?;
let pos = Vec2::new(pos.0.parse()?, pos.1.parse()?);
let index = o
.name
.strip_prefix("k")
.ok_or(anyhow!("invalid node name"))?
.parse::<usize>()?;
diagram.nodes[index].position = pos
}
Ok(())
}
|