Skip to content

Commit 421f101

Browse files
authored
Merge pull request #1 from dimacurrentai/mermaid
Mermaid sequence diagram generation.
2 parents ebc60b4 + 125e63d commit 421f101

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

step03_mermaid/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.mermaid.md

step03_mermaid/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Build the binaries statically, and then run them from a separate container.
2+
FROM alpine AS build
3+
4+
# Intentionally not installing `cargo`, etc. But it does need `clang`.
5+
RUN apk add rustup clang musl
6+
7+
# Init local rust via `rustup`, including static targets builder.
8+
RUN rustup-init -y -t x86_64-unknown-linux-musl --no-modify-path
9+
RUN mv /root/.cargo/bin/* /usr/local/bin/
10+
11+
# Build the project.
12+
COPY ./code /code
13+
WORKDIR /code
14+
RUN cargo build --release --target x86_64-unknown-linux-musl
15+
16+
# Prepare the static binary.
17+
RUN cp ./target/x86_64-unknown-linux-musl/release/mermaid /
18+
19+
# The resulting container with the static binary only.
20+
FROM scratch
21+
COPY --from=build /mermaid /mermaid
22+
ENTRYPOINT ["/mermaid"]

step03_mermaid/code/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "mermaid"
3+
edition = "2024"

step03_mermaid/code/src/main.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
mod mermaid {
2+
struct Data {
3+
contents: String,
4+
i: i32,
5+
}
6+
pub struct Canvas {
7+
instance: std::cell::RefCell<Data>,
8+
}
9+
pub struct Participant<'a> {
10+
canvas: &'a Canvas,
11+
i: i32,
12+
_name: String,
13+
}
14+
impl Canvas {
15+
pub fn new() -> Self {
16+
Self {
17+
instance: std::cell::RefCell::new(Data {
18+
contents: "sequenceDiagram\n create participant I0 as root\n autonumber\n".to_string(),
19+
i: 0,
20+
}),
21+
}
22+
}
23+
pub fn new_participant<S: AsRef<str>>(&self, s: S) -> Participant {
24+
let mut data = self.instance.borrow_mut();
25+
let curr_i = {
26+
let i: &mut i32 = &mut data.i;
27+
*i = *i + 1;
28+
*i
29+
};
30+
Canvas::append_into(
31+
data,
32+
&format!(" create participant I{} as {}\n I0-->>I{}: create\n", curr_i, s.as_ref(), curr_i),
33+
);
34+
Participant { canvas: self, i: curr_i, _name: String::from(s.as_ref()) }
35+
}
36+
fn append_into<S: AsRef<str>>(mut data: std::cell::RefMut<Data>, s: S) {
37+
data.contents.push_str(s.as_ref());
38+
}
39+
fn append<S: AsRef<str>>(&self, s: S) {
40+
Canvas::append_into(self.instance.borrow_mut(), s.as_ref());
41+
}
42+
pub fn output<F>(&self, f: F)
43+
where
44+
F: FnOnce(&String),
45+
{
46+
f(&self.instance.borrow().contents)
47+
}
48+
}
49+
impl<'a> Participant<'a> {
50+
pub fn add_arrow_to(&self, rhs: &Participant, text: &str) {
51+
self.canvas.append(format!(" I{}->>I{}: {}\n", self.i, rhs.i, text));
52+
}
53+
}
54+
impl<'a> Drop for Participant<'a> {
55+
fn drop(&mut self) {
56+
self.canvas.append(format!(" destroy I{}\n I{}-->>I0: destroy\n", self.i, self.i));
57+
}
58+
}
59+
}
60+
61+
use mermaid::Canvas;
62+
//use medmaid::Participant;
63+
64+
fn main() {
65+
let canvas = Canvas::new();
66+
67+
{
68+
canvas.new_participant("foo");
69+
}
70+
{
71+
let bar = canvas.new_participant("bar");
72+
{
73+
let baz = canvas.new_participant("baz");
74+
bar.add_arrow_to(&baz, "Hello!");
75+
}
76+
}
77+
78+
{
79+
let meh = canvas.new_participant("meh");
80+
{
81+
let blah = canvas.new_participant("blah");
82+
blah.add_arrow_to(&meh, "Whoa!");
83+
}
84+
}
85+
86+
canvas.output(|s| println!("{}", s));
87+
}

step03_mermaid/mermaid.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
```mermaid
2+
sequenceDiagram
3+
create participant I0 as root
4+
autonumber
5+
create participant I1 as foo
6+
I0-->>I1: create
7+
destroy I1
8+
I1-->>I0: destroy
9+
create participant I2 as bar
10+
I0-->>I2: create
11+
create participant I3 as baz
12+
I0-->>I3: create
13+
I2->>I3: Hello!
14+
destroy I3
15+
I3-->>I0: destroy
16+
destroy I2
17+
I2-->>I0: destroy
18+
create participant I4 as meh
19+
I0-->>I4: create
20+
create participant I5 as blah
21+
I0-->>I5: create
22+
I5->>I4: Whoa!
23+
destroy I5
24+
I5-->>I0: destroy
25+
destroy I4
26+
I4-->>I0: destroy
27+
28+
```

step03_mermaid/run.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
docker build . -t demo
6+
7+
echo '```mermaid' >.mermaid.md
8+
docker run --rm -t demo | tee >>.mermaid.md
9+
echo '```' >>.mermaid.md
10+
11+
# TODO(dkorolev): Fail with a nicer message, and add a git hook.
12+
diff mermaid.md .mermaid.md || echo 'Your `mermaid.md` file is not up to date.'

0 commit comments

Comments
 (0)