Skip to content

Commit 6774774

Browse files
committed
Add Rust examples, fix syntax highlighting for Rust
1 parent 0ec851f commit 6774774

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

docs/tasks/build.mdx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,58 @@ auto builder = Builder(manifest_json);
329329
330330
<TabItem value="rust" label="Rust">
331331
This is how to attach and sign a manifest using Rust.
332+
333+
From [`c2pa-rs/sdk/examples/v2api.rs`](https://github.com/contentauth/c2pa-rs/blob/main/sdk/examples/v2api.rs#L88C5-L134C1):
334+
335+
```rust
336+
let json = manifest_def(title, format);
337+
338+
let mut builder = Builder::from_json(&json)?;
339+
builder.add_ingredient_from_stream(
340+
json!({
341+
"title": parent_name,
342+
"relationship": "parentOf"
343+
})
344+
.to_string(),
345+
format,
346+
&mut source,
347+
)?;
348+
349+
let thumb_uri = builder
350+
.definition
351+
.thumbnail
352+
.as_ref()
353+
.map(|t| t.identifier.clone());
354+
355+
// add a manifest thumbnail ( just reuse the image for now )
356+
if let Some(uri) = thumb_uri {
357+
if !uri.starts_with("self#jumbf") {
358+
source.rewind()?;
359+
builder.add_resource(&uri, &mut source)?;
360+
}
361+
}
362+
363+
// write the manifest builder to a zipped stream
364+
let mut zipped = Cursor::new(Vec::new());
365+
builder.to_archive(&mut zipped)?;
366+
367+
// write the zipped stream to a file for debugging
368+
//let debug_path = format!("{}/../target/test.zip", env!("CARGO_MANIFEST_DIR"));
369+
// std::fs::write(debug_path, zipped.get_ref())?;
370+
371+
// unzip the manifest builder from the zipped stream
372+
zipped.rewind()?;
373+
374+
let ed_signer =
375+
|_context: *const (), data: &[u8]| CallbackSigner::ed25519_sign(data, PRIVATE_KEY);
376+
let signer = CallbackSigner::new(ed_signer, SigningAlg::Ed25519, CERTS);
377+
378+
let mut builder = Builder::from_archive(&mut zipped)?;
379+
// sign the ManifestStoreBuilder and write it to the output stream
380+
let mut dest = Cursor::new(Vec::new());
381+
builder.sign(&signer, format, &mut source, &mut dest)?;
382+
```
383+
332384
</TabItem>
333385
334386
</Tabs>

docs/tasks/get-resources.mdx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,37 @@ try
179179
180180
<TabItem value="rust" label="Rust">
181181
This is how to get resources using Rust.
182+
183+
This is from [`resource_to_stream`](https://docs.rs/c2pa/latest/c2pa/struct.Reader.html#method.resource_to_stream) API doc:
184+
185+
```rust
186+
use c2pa::Reader;
187+
let stream = std::io::Cursor::new(Vec::new());
188+
let reader = Reader::from_file("path/to/file.jpg").unwrap();
189+
let manifest = reader.active_manifest().unwrap();
190+
let uri = &manifest.thumbnail_ref().unwrap().identifier;
191+
let bytes_written = reader.resource_to_stream(uri, stream).unwrap();
192+
```
193+
194+
This is from [`c2pa-rs/examples/v2api.rs`](https://github.com/contentauth/c2pa-rs/blob/main/sdk/examples/v2api.rs#L138):
195+
196+
```rust
197+
let reader = Reader::from_stream(format, &mut dest)?;
198+
199+
// extract a thumbnail image from the ManifestStore
200+
let mut thumbnail = Cursor::new(Vec::new());
201+
if let Some(manifest) = reader.active_manifest() {
202+
if let Some(thumbnail_ref) = manifest.thumbnail_ref() {
203+
reader.resource_to_stream(&thumbnail_ref.identifier, &mut thumbnail)?;
204+
println!(
205+
"wrote thumbnail {} of size {}",
206+
thumbnail_ref.format,
207+
thumbnail.get_ref().len()
208+
);
209+
}
210+
}
211+
```
212+
182213
</TabItem>
183214
184215
</Tabs>

docs/tasks/read.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Use the [`Reader`](https://docs.rs/c2pa/latest/c2pa/struct.Reader.html) struct t
114114
115115
Use [`from_file`](https://docs.rs/c2pa/latest/c2pa/struct.Reader.html#method.from_file) to read manifest data from a file:
116116
117-
```rs
117+
```rust
118118
use c2pa::Reader;
119119
let reader = Reader::from_file("path/to/file.jpg").unwrap();
120120
```
@@ -125,7 +125,7 @@ There is also an asynchronous version of this method, [`from_stream_async`](http
125125
126126
Use [`from_stream`](https://docs.rs/c2pa/latest/c2pa/struct.Reader.html#method.from_stream) to read manifest data from a stream:
127127
128-
```rs
128+
```rust
129129
use std::io::Cursor;
130130

131131
use c2pa::Reader;

docs/tasks/setup.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ using namespace c2pa;
9191
</TabItem>
9292
9393
<TabItem value="rust" label="Rust">
94-
This is how to setup the Rust library.
94+
This is how to setup your code to use the Rust library.
9595
9696
```rust
9797
use std::{

docusaurus.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const config = {
140140
prism: {
141141
theme: lightCodeTheme,
142142
darkTheme: darkCodeTheme,
143+
additionalLanguages: ['rust'],
143144
},
144145
algolia: {
145146
// The application ID provided by Algolia

0 commit comments

Comments
 (0)