Skip to content

Commit 399ac18

Browse files
committed
feat: add c2pa-types package with automatic type generation
1 parent cd68747 commit 399ac18

File tree

12 files changed

+203
-4
lines changed

12 files changed

+203
-4
lines changed

Cargo.lock

Lines changed: 49 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
resolver = '2'
44
members = [
55
'packages/c2pa-wasm',
6+
'packages/c2pa-types',
67
]
78

89
[profile.release]

packages/c2pa-types/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
schemas
2+
types

packages/c2pa-types/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "c2pa-types"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
c2pa = { version = "0.58.0", features = ["pdf", "rust_native_crypto", "json_schema"], default-features = false }
8+
schemars = "0.8.21"
9+
serde_json = "1.0.143"

packages/c2pa-types/index.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright 2025 Adobe
3+
* All Rights Reserved.
4+
*
5+
* NOTICE: Adobe permits you to use, modify, and distribute this file in
6+
* accordance with the terms of the Adobe license agreement accompanying
7+
* it.
8+
*/
9+
10+
import type { Reader } from './types/ManifestStore.js';
11+
12+
// Renames the auto-generated "Reader" type to the more appropriate "ManifestStore"
13+
export interface ManifestStore extends Reader {}
14+
15+
export type { Manifest } from './types/ManifestStore.js';

packages/c2pa-types/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@contentauth/c2pa-types",
3+
"description": "Typescript types generated from c2pa-rs",
4+
"type": "module",
5+
"version": "0.1.1",
6+
"types": "index.d.ts",
7+
"scripts": {
8+
"test": "echo \"No tests to run\" && exit 0",
9+
"build": "pnpm clean && json2ts -i schemas/ -o types/",
10+
"clean": "rimraf types"
11+
},
12+
"files": ["index.d.ts", "types/**/*"],
13+
"dependencies": {
14+
"json-schema-to-typescript": "^15.0.4",
15+
"rimraf": "^6.0.1"
16+
}
17+
}

packages/c2pa-types/project.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "c2pa-types",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"tags": ["lib"],
5+
"projectType": "library",
6+
"sourceRoot": "packages/c2pa-types/src",
7+
"targets": {
8+
"build": {
9+
"cache": true,
10+
"inputs": [
11+
"{projectRoot}/**/*",
12+
"!{projectRoot}/schemas/**/*",
13+
"!{projectRoot}/types/**/*"
14+
],
15+
"executor": "nx:run-commands",
16+
"options": {
17+
"commands": [
18+
"cargo run",
19+
"pnpm build"
20+
],
21+
"parallel": false,
22+
"cwd": "{projectRoot}"
23+
}
24+
}
25+
}
26+
}

packages/c2pa-types/src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::{fs, path::Path};
2+
3+
use c2pa::Reader;
4+
use schemars::{schema::RootSchema, schema_for};
5+
6+
fn main() {
7+
let output_dir = Path::new("./schemas");
8+
fs::remove_dir_all(output_dir).expect("Could not clear existing schema directory");
9+
fs::create_dir_all(output_dir).expect("Could not create schema directory");
10+
11+
write_schema(&schema_for!(Reader), &"ManifestStore", output_dir);
12+
}
13+
14+
fn write_schema(schema: &RootSchema, name: &str, output_dir: &Path) {
15+
println!("Exporting JSON schema for {name}");
16+
let output_path = output_dir.join(format!("{name}.json"));
17+
let output = serde_json::to_string_pretty(schema).expect("Failed to serialize schema");
18+
fs::write(&output_path, output).expect("Unable to write schema");
19+
println!("Wrote schema to {}", output_path.display());
20+
}

packages/c2pa-web/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
}
3131
},
3232
"dependencies": {
33-
"@contentauth/c2pa-wasm": "workspace:*"
33+
"@contentauth/c2pa-wasm": "workspace:*",
34+
"@contentauth/c2pa-types": "workspace:*"
3435
},
3536
"devDependencies": {
3637
"@playwright/test": "^1.55.0",

packages/c2pa-web/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
*/
99

1010
export * from './lib/c2pa.js';
11+
1112
export {
1213
isSupportedReaderFormat,
1314
READER_SUPPORTED_FORMATS,
1415
} from './lib/supportedFormats.js';
16+
1517
export { type Settings } from './lib/settings.js';
18+
19+
// Re-export types from c2pa-types for convenience.
20+
export type * from '@contentauth/c2pa-types';

0 commit comments

Comments
 (0)