Skip to content

Provide an impl error::Error for FromPlantumlError #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

drmoose
Copy link

@drmoose drmoose commented May 4, 2025

This request adds std::fmt::Display and std::error::Error boilerplate to FromPlantumlError, making it compatible with anyhow and the ? operator.

@maksugr
Copy link
Owner

maksugr commented Jul 16, 2025

Hi, @drmoose! Sorry for not noticing the PR for so long, I’ll take a look at it in the next few hours. If you don’t mind me asking, one quick question: what are you using this package for, if it’s not a secret?

@drmoose
Copy link
Author

drmoose commented Jul 16, 2025

It's not a secret, but it's also not very interesting. Gitlab wiki supports inline plantuml, and I was editing a large wiki page on my company's intranet that had a lot of diagrams in it. Because I wasn't very familiar with the syntax, I wanted a way to quickly preview all my diagrams, so I wrote a bash script that extracted all the ```plantuml blocks and rendered the diagrams to a browser.

Originally, I was using the official plantuml jar to encode URLs:

time perl -ne '
  if ($f) {
    if (/^```/) { 
      $f=0; 
      $ENV{uml}=$n; 
      @argv=("zsh", "-c", "java -jar plantuml.jar -encodeurl =(echo \"\$uml\")");
      system(@argv) 
    } else { 
      $n .= $_ 
    } 
  } elsif (/^```plantuml/) {
     $f = 1 
  }
' < notes.md

But, java -jar has a very long startup time, so this took perl -ne < notes.md 6.18s user per render. A 6 sec cycle time may not seem like much, but I don't know the plantuml syntax that well and was guessing one byte at a time, so I went looking for something faster, found your library, and made it into a standalone program called planturl.

fn main() -> anyhow::Result<()> {
	let mut had_arg = false;
	for argument in env::args().skip(1) {
		had_arg = true;
		if argument == "-" {
			encode_stdin()?;
		} else {
			let encoded = encode_plantuml_deflate(argument)?;
			println!("{}", encoded);
		}
	}
	if had_arg { Ok(()) } else { encode_stdin() }
}

fn encode_stdin() -> anyhow::Result<()> {
	let mut stdin = io::stdin();
	let mut buf = String::new();
	stdin.read_to_string(&mut buf)?;
	let encoded = encode_plantuml_deflate(buf)?;
	println!("{}", encoded);

	Ok(())
}

With this in my $PATH, I replaced my perl script above with this one

time perl -ne '
  if ($f) {
    if (/^```/) { 
      $f=0; 
      @argv=("planturl", "$n");
      system(@argv) 
    } else { 
      $n .= $_ 
    } 
  } elsif (/^```plantuml/) {
     $f = 1 
  }
' < notes.md

And got a glorious perl -ne < notes.md 0.01s user, which let me have a per-keystroke live preview of the diagrams in my document, which made it possible for me to guess the correct syntax :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants