use jdt_activity_pub::ApNote;
use serde_json;
use std::env;
use std::io::{self, Read};
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 && (args[1] == "--help" || args[1] == "-h") {
print_help();
return;
}
let json_input = if args.len() > 1 {
args[1].clone()
} else {
let mut buffer = String::new();
match io::stdin().read_to_string(&mut buffer) {
Ok(_) => buffer.trim().to_string(),
Err(e) => {
eprintln!("Error reading from stdin: {}", e);
std::process::exit(1);
}
}
};
if json_input.is_empty() {
eprintln!("Error: No JSON input provided");
print_help();
std::process::exit(1);
}
println!("๐ Testing JSON deserialization to ApNote");
println!("๐ง Input JSON:");
println!("{}", json_input);
println!();
match serde_json::from_str::<serde_json::Value>(&json_input) {
Ok(value) => {
println!("โ
Valid JSON structure");
println!("๐ Pretty printed:");
println!("{}", serde_json::to_string_pretty(&value).unwrap_or_else(|_| "Error pretty printing".to_string()));
println!();
}
Err(e) => {
eprintln!("โ Invalid JSON: {}", e);
std::process::exit(1);
}
}
match serde_json::from_str::<ApNote>(&json_input) {
Ok(note) => {
println!("๐ Successfully deserialized to ApNote!");
println!();
println!("๐ ApNote fields:");
println!(" ID: {:?}", note.id);
println!(" Type: {:?}", note.kind);
println!(" Name: {:?}", note.name);
println!(" Content: {:?}", note.content);
println!(" Published: {:?}", note.published);
println!(" URL: {:?}", note.url);
println!(" To: {:?}", note.to);
println!(" CC: {:?}", note.cc);
println!(" Tag: {:?}", note.tag);
println!(" Attributed To: {:?}", note.attributed_to);
println!(" In Reply To: {:?}", note.in_reply_to);
println!(" Conversation: {:?}", note.conversation);
println!(" Attachment: {:?}", note.attachment);
println!(" Summary: {:?}", note.summary);
println!(" Sensitive: {:?}", note.sensitive);
println!(" Ephemeral: {:?}", note.ephemeral);
println!();
match serde_json::to_string_pretty(¬e) {
Ok(serialized) => {
println!("๐ Re-serialized ApNote:");
println!("{}", serialized);
}
Err(e) => {
eprintln!("โ ๏ธ Could not re-serialize ApNote: {}", e);
}
}
}
Err(e) => {
eprintln!("โ Failed to deserialize to ApNote: {}", e);
println!();
println!("๐ก This might help debug the issue:");
println!(" - Check if required fields are present (type, to, published, attributedTo)");
println!(" - Verify field names match expected camelCase format");
println!(" - Ensure URL field format is compatible with ApUrl enum");
println!(" - Check that the 'type' field is a valid note type");
std::process::exit(1);
}
}
}
fn print_help() {
println!("๐งช ApNote JSON Deserialization Tester");
println!();
println!("USAGE:");
println!(" {} [JSON_STRING]", env::args().next().unwrap_or_else(|| "test_note_json".to_string()));
println!(" echo 'JSON' | {}", env::args().next().unwrap_or_else(|| "test_note_json".to_string()));
println!();
println!("EXAMPLES:");
println!(" # Test with inline JSON:");
println!(" {} '{{\"type\":\"Note\",\"to\":[\"https://www.w3.org/ns/activitystreams#Public\"],\"published\":\"2023-01-01T00:00:00Z\",\"attributedTo\":\"https://example.com/user\",\"content\":\"Hello world!\"}}'", env::args().next().unwrap_or_else(|| "test_note_json".to_string()));
println!();
println!(" # Test with file input:");
println!(" cat note.json | {}", env::args().next().unwrap_or_else(|| "test_note_json".to_string()));
println!();
println!(" # Test URL handling:");
println!(" {} '{{\"type\":\"Note\",\"to\":[\"https://www.w3.org/ns/activitystreams#Public\"],\"published\":\"2023-01-01T00:00:00Z\",\"attributedTo\":\"https://example.com/user\",\"url\":[\"https://example.com/note/1\",{{\"type\":\"Link\",\"href\":\"https://mirror.com/note/1\",\"rel\":\"canonical\"}}]}}'", env::args().next().unwrap_or_else(|| "test_note_json".to_string()));
println!();
println!("OPTIONS:");
println!(" -h, --help Show this help message");
}