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
use base64::{prelude::BASE64_URL_SAFE, DecodeError, Engine};
use raphtory::{
    core::utils::errors::GraphError,
    db::api::view::MaterializedGraph,
    serialise::{StableDecode, StableEncode},
};

#[derive(thiserror::Error, Debug)]
pub enum UrlDecodeError {
    #[error("Bincode operation failed")]
    GraphError {
        #[from]
        source: GraphError,
    },
    #[error("Base64 decoding failed")]
    DecodeError {
        #[from]
        source: DecodeError,
    },
}

pub fn url_encode_graph<G: Into<MaterializedGraph>>(graph: G) -> Result<String, GraphError> {
    let g: MaterializedGraph = graph.into();
    Ok(BASE64_URL_SAFE.encode(g.encode_to_vec()))
}

pub fn url_decode_graph<T: AsRef<[u8]>>(graph: T) -> Result<MaterializedGraph, UrlDecodeError> {
    Ok(MaterializedGraph::decode_from_bytes(
        &BASE64_URL_SAFE.decode(graph)?,
    )?)
}