Skip to content

Commit 3638260

Browse files
committed
Add gpptool binary
This tool allows decoding of GPP strings to JSON in the console.
1 parent 583f10c commit 3638260

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
3+
"gpptool",
34
"iab_gpp",
45
"iab_gpp_derive"
56
]

gpptool/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "gpptool"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Marc Noirot <[email protected]>"]
6+
description = "IAB GPP Consent String implementation"
7+
homepage = "https://github.com/noirotm/iabgpp-rs"
8+
repository = "https://github.com/noirotm/iabgpp-rs"
9+
license = "Apache-2.0"
10+
keywords = ["tcf", "iab", "gpp", "decode"]
11+
categories = ["parser-implementations"]
12+
documentation = "https://docs.rs/iab_gpp/"
13+
14+
[dependencies]
15+
clap = { version = "4.5.28", features = ["derive"] }
16+
colored_json = "5.0.0"
17+
iab_gpp = { path = "../iab_gpp", features = ["serde"] }
18+
serde = { version = "1.0.217", features = ["derive"] }
19+
serde_json = "1.0.138"
20+
num-traits = "0.2.19"

gpptool/src/main.rs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use clap::{Parser, Subcommand};
2+
use colored_json::ToColoredJson;
3+
use iab_gpp::sections::SectionId;
4+
use iab_gpp::v1::GPPString;
5+
use num_traits::cast::FromPrimitive;
6+
use num_traits::ToPrimitive;
7+
use std::str::FromStr;
8+
9+
#[derive(Parser)]
10+
#[command(version, about, long_about = None)]
11+
struct Cli {
12+
#[command(subcommand)]
13+
cmd: Commands,
14+
}
15+
16+
#[derive(Subcommand)]
17+
enum Commands {
18+
/// Parse a GPP string and display it in the console
19+
Parse {
20+
/// GPP string to parse
21+
gpp_string: String,
22+
/// Section ID to parse
23+
#[arg(short, long)]
24+
section_id: Option<u32>,
25+
},
26+
/// List all sections
27+
List {
28+
/// GPP string to parse
29+
gpp_string: String,
30+
},
31+
}
32+
33+
fn main() {
34+
let args = Cli::parse();
35+
36+
let e = match args.cmd {
37+
Commands::Parse {
38+
gpp_string,
39+
section_id: None,
40+
} => parse_gpp_string(&gpp_string),
41+
Commands::Parse {
42+
gpp_string,
43+
section_id: Some(id),
44+
} => parse_gpp_string_section(&gpp_string, id),
45+
Commands::List { gpp_string } => list_sections(&gpp_string),
46+
};
47+
48+
if let Err(e) = e {
49+
eprintln!("{}", e);
50+
std::process::exit(1);
51+
}
52+
}
53+
54+
fn parse_gpp_string(s: &str) -> Result<(), Box<dyn std::error::Error>> {
55+
let gpp_str = GPPString::from_str(s)?;
56+
57+
let sections = gpp_str
58+
.decode_all_sections()
59+
.into_iter()
60+
.collect::<Result<Vec<_>, _>>()?;
61+
62+
println!(
63+
"{}",
64+
serde_json::to_string_pretty(&sections)?.to_colored_json_auto()?
65+
);
66+
67+
Ok(())
68+
}
69+
70+
fn parse_gpp_string_section(s: &str, id: u32) -> Result<(), Box<dyn std::error::Error>> {
71+
let gpp_str = GPPString::from_str(s)?;
72+
73+
let section = gpp_str.decode_section(SectionId::from_u32(id).ok_or("Invalid ID")?)?;
74+
75+
println!(
76+
"{}",
77+
serde_json::to_string_pretty(&section)?.to_colored_json_auto()?
78+
);
79+
80+
Ok(())
81+
}
82+
83+
fn list_sections(s: &str) -> Result<(), Box<dyn std::error::Error>> {
84+
let gpp_str = GPPString::from_str(s)?;
85+
86+
for s in gpp_str.section_ids() {
87+
println!("{}\t{:?}", s.to_u32().unwrap_or_default(), s);
88+
}
89+
90+
Ok(())
91+
}

0 commit comments

Comments
 (0)