Skip to content

Instantly share code, notes, and snippets.

@sheepla
Last active June 21, 2024 13:05
Show Gist options
  • Save sheepla/2e40d30fd748505405d6338a1d70b835 to your computer and use it in GitHub Desktop.
Save sheepla/2e40d30fd748505405d6338a1d70b835 to your computer and use it in GitHub Desktop.
HAR: HTTP Archive をパースする
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Deserialize, Serialize)]
pub struct Har {
pub log: Log,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Log {
pub version: String,
pub creator: Creator,
pub browser: Option<Creator>,
pub pages: Option<Vec<Page>>,
pub entries: Vec<Entry>,
pub comment: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Creator {
pub name: String,
pub version: String,
pub comment: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Page {
// Define fields according to Page schema in JSON Schema
pub id: String,
pub startedDateTime: String,
pub title: String,
// Add other fields as necessary
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Entry {
// Define fields according to Entry schema in JSON Schema
pub pageref: String,
pub startedDateTime: String,
pub time: f64,
pub request: Request,
pub response: Response,
// Add other fields as necessary
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Request {
// Define fields according to Request schema in JSON Schema
pub method: String,
pub url: String,
// Add other fields as necessary
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Response {
// Define fields according to Response schema in JSON Schema
pub status: i32,
pub statusText: String,
// Add other fields as necessary
}
fn main() {
// Example usage:
let har_json = r#"
{
"log": {
"version": "1.2",
"creator": {
"name": "Example Creator",
"version": "1.0"
},
"entries": [
{
"pageref": "page_1",
"startedDateTime": "2024-06-21T12:00:00Z",
"time": 100.0,
"request": {
"method": "GET",
"url": "https://example.com"
},
"response": {
"status": 200,
"statusText": "OK"
}
}
]
}
}
"#;
// Deserialize from JSON string to Har struct
let har: Har = serde_json::from_str(har_json).unwrap();
println!("{:#?}", har);
// Serialize Har struct to JSON string
let serialized_har = serde_json::to_string_pretty(&har).unwrap();
println!("{}", serialized_har);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment