Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Add JSON Schema validations (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Jun 25, 2024
1 parent 557af4b commit 90f446e
Show file tree
Hide file tree
Showing 14 changed files with 788 additions and 32 deletions.
6 changes: 5 additions & 1 deletion crates/tbdex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
homepage.workspace = true
repository.workspace = true
license-file.workspace = true
build = "build.rs"

[dependencies]
base64 = "0.22.0"
Expand All @@ -20,4 +21,7 @@ serde_json = { workspace = true }
thiserror = { workspace = true }
type-safe-id = { version = "0.3.0", features = ["serde"] }
uuid = "1.9.0"
web5 = { workspace = true }
web5 = { workspace = true }

[build-dependencies]
reqwest = { version = "0.12.5", features = ["blocking"] }
65 changes: 65 additions & 0 deletions crates/tbdex/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::fs;
use std::io::{self, BufRead};
use std::path::Path;

fn write_json_schemas() -> Result<(), Box<dyn std::error::Error>> {
let commit_hash = "96a1a7164e8e0a608befa31b6cf0c9a4e5cc0f07";
let schemas = vec![
("DEFINITIONS_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/definitions.json", commit_hash)),
("RESOURCE_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/resource.schema.json", commit_hash)),
("BALANCE_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/balance.schema.json", commit_hash)),
("OFFERING_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/offering.schema.json", commit_hash)),
("MESSAGE_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/message.schema.json", commit_hash)),
("RFQ_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/rfq.schema.json", commit_hash)),
("RFQ_PRIVATE_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/rfq-private.schema.json", commit_hash)),
("QUOTE_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/quote.schema.json", commit_hash)),
("ORDER_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/order.schema.json", commit_hash)),
("ORDER_STATUS_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/orderstatus.schema.json", commit_hash)),
("CLOSE_DATA_JSON_SCHEMA", format!("https://raw.githubusercontent.com/TBD54566975/tbdex/{}/hosted/json-schemas/close.schema.json", commit_hash)),
];

let dest_path = Path::new("src/json_schemas").join("generated.rs");

// Check if the existing file contains the same commit hash
if let Ok(file) = fs::File::open(&dest_path) {
let reader = io::BufReader::new(file);
let mut lines = reader.lines();
let third_line = lines.nth(3); // Get the third line (index 2)

if let Some(Ok(line)) = third_line {
if line == format!("pub const GIT_COMMIT_HASH: &str = \"{}\";", commit_hash) {
// The commit hash matches, no need to re-fetch the schemas
println!("Schemas are up-to-date, skipping download.");
return Ok(());
}
}
}

let mut file_content = String::new();

// Add the commit hash to the top of the file
file_content.push_str("// THIS FILE IS AUTO-GENERATED BY build.rs\n");
file_content.push_str("#[warn(unused_imports)]\n");
file_content.push_str("#[allow(dead_code)]\n");
file_content.push_str(&format!(
"pub const GIT_COMMIT_HASH: &str = \"{}\";\n",
commit_hash
));

for (name, url) in schemas {
let response = reqwest::blocking::get(&url)?;
let mut schema = response.text()?;
schema = schema.replace('#', "\\#");
file_content.push_str(&format!("pub const {}: &str = r#\"{}\"#;\n", name, schema));
}

fs::write(dest_path, file_content)?;

Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
write_json_schemas()?;

Ok(())
}
Loading

0 comments on commit 90f446e

Please sign in to comment.