commonmeta-schema 1.0.0-rc6

Commonmeta JSON Schemas and conformance fixtures
Documentation
#![forbid(unsafe_code)]

use include_dir::{include_dir, Dir};

/// The complete tree of Commonmeta conformance fixtures, embedded at compile
/// time. Organized as `<format>/<name>.<ext>` (e.g. `commonmeta/journal_article.json`,
/// `crossref/journal_article.json`, `orcid/0000-0002-0068-716X.xml`).
///
/// Consumers can look up a single file with [`fixture_str`] / [`fixture_bytes`],
/// or iterate a subtree via `FIXTURES.get_dir("commonmeta").unwrap().files()`.
pub static FIXTURES: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/fixtures");

/// Return an embedded fixture's raw bytes by relative path, e.g.
/// `fixture_bytes("commonmeta/journal_article.json")`.
pub fn fixture_bytes(path: &str) -> Option<&'static [u8]> {
    FIXTURES.get_file(path).map(|f| f.contents())
}

/// Return an embedded fixture's UTF-8 contents by relative path, e.g.
/// `fixture_str("crossref/journal_article.json")`. Returns `None` if the file
/// is absent or not valid UTF-8.
pub fn fixture_str(path: &str) -> Option<&'static str> {
    FIXTURES.get_file(path).and_then(|f| f.contents_utf8())
}

/// Embedded Commonmeta schema v1.0. Records stamp `schema_version` as
/// `https://commonmeta.org/commonmeta_v1.0.json`.
pub const COMMONMETA_SCHEMA_V1_0: &str = include_str!("../schemas/commonmeta_v1.0.json");

/// Embedded Commonmeta journal article fixture.
pub const COMMONMETA_FIXTURE_JOURNAL_ARTICLE: &str =
    include_str!("../fixtures/commonmeta/journal_article.json");

/// Return an embedded schema by Commonmeta version string.
///
/// Currently supported: "1.0".
pub fn schema(version: &str) -> Option<&'static str> {
    match version {
        "1.0" => Some(COMMONMETA_SCHEMA_V1_0),
        _ => None,
    }
}

/// Return an embedded Commonmeta fixture by name.
///
/// Currently supported: "journal_article".
pub fn fixture(name: &str) -> Option<&'static str> {
    match name {
        "journal_article" => Some(COMMONMETA_FIXTURE_JOURNAL_ARTICLE),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::{fixture, schema};

    #[test]
    fn known_version_is_available() {
        assert!(schema("1.0").is_some());
    }

    #[test]
    fn unknown_version_is_none() {
        assert!(schema("nope").is_none());
    }

    #[test]
    fn known_fixture_is_available() {
        assert!(fixture("journal_article").is_some());
    }

    #[test]
    fn unknown_fixture_is_none() {
        assert!(fixture("nope").is_none());
    }

    #[test]
    fn embedded_fixtures_are_available() {
        use super::{fixture_bytes, fixture_str, FIXTURES};
        // A known commonmeta fixture is reachable by relative path.
        assert!(fixture_str("commonmeta/journal_article.json").is_some());
        // A non-JSON fixture (ORCID XML) is reachable as bytes.
        assert!(fixture_bytes("orcid/0000-0002-0068-716X.xml").is_some());
        // Subtree iteration works and the commonmeta dir is non-empty.
        let commonmeta = FIXTURES.get_dir("commonmeta").expect("commonmeta dir");
        assert!(commonmeta.files().count() > 0);
        // Absent paths return None rather than panicking.
        assert!(fixture_str("nope/missing.json").is_none());
    }
}