#codec #plant-uml #deflate #decoding #hex #graphics

plantuml_encoding

对文本plantuml图进行编码和解码,以方便通过URL进行交流。

10个版本 (6个稳定版本)

2.0.3 2022年7月1日
2.0.2 2022年6月30日
1.0.1 2022年6月23日
0.1.4 2022年6月22日

#1069 in 编码

Download history 13/week @ 2024-03-31 1/week @ 2024-04-07 9/week @ 2024-04-21 8/week @ 2024-04-28 21/week @ 2024-05-12 21/week @ 2024-05-19 46/week @ 2024-05-26 18/week @ 2024-06-02 23/week @ 2024-06-09 22/week @ 2024-06-16 40/week @ 2024-06-23 12/week @ 2024-06-30 35/week @ 2024-07-07

110 每月下载次数
用于 2 crates

MIT/Apache

33KB
378

plantuml_encoding

build-svg test-svg codecov-svg crates-svg docs-svg deps-svg

对文本plantuml图进行编码和解码,以方便通过URL进行交流。

概览

考虑以下纯文本plantuml图

@startuml
PUML -> RUST: HELLO
@enduml

它可以编码为 0IO0sVz0StHXSdHrRMmAK5LDJ20jFY1ILLDKEY18HKnCJo0AG6LkP7LjR000,并在plantuml服务器的帮助下(https://www.plantuml.com/plantuml/uml/)可以通过URL进行共享 通过URL

还可以在相反的方向进行解码。

Plantuml 声明支持以下压缩算法

但实际上,plantuml只支持 deflate(带有 类似于base64的额外转换)和 hex(带有 额外的 ~h 前缀)。 brotli 已关闭。因此,该crate也只支持 deflatehex

安装

为了使用此crate,您需要将其添加到您的 [dependencies] 下的 Cargo.toml

[dependencies]
plantuml_encoding = "2.0.3"

文章

有一篇 文章 详细描述了该库的内部结构。

示例

use plantuml_encoding::{
    decode_plantuml_deflate, decode_plantuml_hex,
    encode_plantuml_deflate, encode_plantuml_hex,
    FromPlantumlError,
};

fn main() -> Result<(), FromPlantumlError> {
    // original puml
    println!("--- Original puml ---");

    let puml = "@startuml\nPUML -> RUST\n@enduml";

    println!("Original puml:\n{}\n", puml);

    // deflate
    println!("--- Deflate ---");

    let encoded_deflate = encode_plantuml_deflate(puml)?;
    let decoded_deflate = decode_plantuml_deflate(&encoded_deflate)?;

    println!("Encoded deflate: {}", encoded_deflate);
    println!("Decoded deflate:\n{}\n", decoded_deflate);

    // hex
    println!("--- Hex ---");

    let encoded_hex = encode_plantuml_hex(puml)?;
    let decoded_hex = decode_plantuml_hex(&encoded_hex)?;

    println!("Encoded hex: {}", encoded_hex);
    println!("Decoded hex:\n{}\n", decoded_hex);

    // deflate errors
    println!("--- Deflate errors ---");

    let empty_encoded_deflate = "";

    let decoded_deflate = decode_plantuml_deflate(empty_encoded_deflate)
        .unwrap_or_else(|_| "It's not decoded deflate".to_string());

    println!("Decoded deflate error:\n{}\n", decoded_deflate);

    let decoded_deflate = match decode_plantuml_deflate(empty_encoded_deflate) {
        Ok(plantuml) => plantuml,
        Err(FromPlantumlError(err)) => {
            eprintln!("Decoded deflate error: {:?}", err);
            String::from("Result from deflate error")
        }
    };

    println!("Match decoded deflate error result:\n{}\n", decoded_deflate);

    // hex errors
    println!("--- Hex errors ---");

    let decoded_hex = match decode_plantuml_hex("12345") {
        Ok(plantuml) => plantuml,
        Err(FromPlantumlError(err)) => {
            eprintln!("Decoded hex error: {:?}", err);
            String::from("Result from hex error")
        }
    };

    println!("Match decoded hex error result:\n{}", decoded_hex);

    Ok(())
}

以下示例的 cargo run 命令后的控制台输出

--- Original puml ---
Original puml:
@startuml
PUML -> RUST
@enduml

--- Deflate ---
Encoded deflate: SoWkIImgAStDuGe8zVLHqBLJ20eD3k5oICrB0Ge20000
Decoded deflate:
@startuml
PUML -> RUST
@enduml

--- Hex ---
Encoded hex: ~h407374617274756d6c0a50554d4c202d3e20525553540a40656e64756d6c
Decoded hex:
@startuml
PUML -> RUST
@enduml

--- Deflate errors ---
Decoded deflate error:
It's not decoded deflate

Decoded deflate error: "there is a problem during deflate decoding: `deflate decompression error`"
Match decoded deflate error result:
Result from deflate error

--- Hex errors ---
Decoded hex error: "there is a problem during hex decoding: `Odd number of digits`"
Match decoded hex error result:
Result from hex error

您还可以考虑文件内的测试。

依赖关系

~335KB