Skip to main content

switchback_avro/
meta_schemas.rs

1//! Vendored Avro format JSON Schemas from
2//! [asyncapi/spec-json-schemas](https://github.com/asyncapi/spec-json-schemas).
3
4use std::fs;
5use std::io;
6use std::path::{Path, PathBuf};
7
8/// A vendored JSON Schema asset (SHA-256 in `meta-schemas.lock.toml`).
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct MetaSchemaAsset {
11    /// Stable asset id in the lock file.
12    pub id: &'static str,
13    /// Path relative to `meta-schemas/`.
14    pub relative_path: &'static str,
15    /// Upstream raw URL.
16    pub source_url: &'static str,
17}
18
19mod assets_inner {
20    use super::MetaSchemaAsset;
21    include!("meta_schemas_assets.rs");
22}
23
24pub use assets_inner::*;
25
26/// Find a vendored asset by its relative path under `meta-schemas/`.
27pub fn asset_by_path(path: &str) -> Option<&'static MetaSchemaAsset> {
28    ALL.iter().find(|asset| asset.relative_path == path)
29}
30
31/// Root directory containing vendored meta-schema files.
32pub fn manifest_dir() -> &'static Path {
33    Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/meta-schemas"))
34}
35
36/// Resolve a vendored asset path on disk.
37pub fn resolve_path(asset: &MetaSchemaAsset) -> PathBuf {
38    manifest_dir().join(asset.relative_path)
39}
40
41/// Read a vendored asset as UTF-8 text.
42pub fn read(asset: &MetaSchemaAsset) -> io::Result<String> {
43    fs::read_to_string(resolve_path(asset))
44}