xmpkit/lib.rs
1//! XMPKit - Pure Rust implementation of Adobe XMP Toolkit
2//!
3//! XMPKit is a pure Rust implementation of Adobe's XMP (Extensible Metadata Platform) Toolkit.
4//! It provides APIs for reading, writing, and manipulating XMP metadata in various file formats
5//! without c++ SDK bridge.
6//!
7//! ## Features
8//!
9//! - Pure Rust implementation
10//! - Compatible with Adobe XMP standard
11//! - Support for common file formats (e.g., JPEG, PNG, TIFF, MP3, GIF, MP4)
12//! - Memory safe and high performance
13//! - Cross-platform support (iOS, Android, HarmonyOS, macOS, Windows, Linux, Wasm)
14//!
15//! ## Quick Start
16//!
17//! ### Working with Files (Native Platforms)
18//!
19//! ```rust,no_run
20//! use xmpkit::{XmpFile, XmpMeta, XmpValue};
21//! use xmpkit::core::namespace::ns;
22//!
23//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
24//! // Open a file
25//! let mut file = XmpFile::new();
26//! file.open("image.jpg")?;
27//!
28//! // Read XMP metadata
29//! if let Some(meta) = file.get_xmp() {
30//! if let Some(creator) = meta.get_property(ns::XMP, "CreatorTool") {
31//! println!("Creator: {:?}", creator);
32//! }
33//! }
34//!
35//! // Modify metadata
36//! if let Some(mut meta) = file.get_xmp().cloned() {
37//! meta.set_property(
38//! ns::XMP,
39//! "CreatorTool",
40//! XmpValue::String("MyApp".to_string()),
41//! )?;
42//! file.put_xmp(meta);
43//! }
44//!
45//! // Save changes
46//! file.save("output.jpg")?;
47//! # Ok(())
48//! # }
49//! ```
50//!
51//! ### Working with Bytes (Wasm and Memory-based Operations)
52//!
53//! ```rust,no_run
54//! use xmpkit::{XmpFile, XmpMeta, XmpValue};
55//! use xmpkit::core::namespace::ns;
56//!
57//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
58//! let jpeg_data: &[u8] = &[]; // your JPEG file data
59//!
60//! // Load from bytes
61//! let mut file = XmpFile::new();
62//! file.from_bytes(jpeg_data)?;
63//!
64//! // Read and modify metadata
65//! if let Some(mut meta) = file.get_xmp().cloned() {
66//! meta.set_property(
67//! ns::XMP,
68//! "CreatorTool",
69//! XmpValue::String("MyApp".to_string()),
70//! )?;
71//! file.put_xmp(meta);
72//! }
73//!
74//! // Write back to bytes
75//! let output_data = file.write_to_bytes()?;
76//! # Ok(())
77//! # }
78//! ```
79//!
80//! ### Parsing XMP from String
81//!
82//! ```rust
83//! use xmpkit::{XmpMeta, XmpValue};
84//! use xmpkit::core::namespace::ns;
85//!
86//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
87//! let xmp_packet = r#"<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
88//! <x:xmpmeta xmlns:x="adobe:ns:meta/">
89//! <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
90//! <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
91//! <xmp:CreatorTool>MyApp</xmp:CreatorTool>
92//! </rdf:Description>
93//! </rdf:RDF>
94//! </x:xmpmeta><?xpacket end="w"?>"#;
95//!
96//! let meta = XmpMeta::parse(xmp_packet)?;
97//!
98//! if let Some(creator) = meta.get_property(ns::XMP, "CreatorTool") {
99//! println!("Creator: {:?}", creator);
100//! }
101//!
102//! // Modify and serialize
103//! let mut meta = meta;
104//! meta.set_property(
105//! ns::XMP,
106//! "ModifyDate",
107//! XmpValue::DateTime("2024-01-01T00:00:00Z".to_string()),
108//! )?;
109//!
110//! let serialized = meta.serialize_packet()?;
111//! # Ok(())
112//! # }
113//! ```
114//!
115//! ## Modules
116//!
117//! - [`core`] - Core XMP functionality (parsing, serialization, metadata API)
118//! - [`files`] - File format handlers for reading/writing XMP from files
119//! - [`types`] - Common types and data structures (XmpValue, Qualifier)
120//! - [`utils`] - Utility functions (date/time handling)
121//!
122//! ## Platform Support
123//!
124//! ### Native Platforms (iOS, Android, HarmonyOS, macOS, Windows, Linux)
125//!
126//! Native platforms support both file I/O and memory-based operations:
127//!
128//! - `XmpFile::open()` - Open files from file system
129//! - `XmpFile::save()` - Save files to file system
130//! - `XmpFile::from_bytes()` - Load from memory (also available)
131//! - `XmpFile::write_to_bytes()` - Write to memory (also available)
132//!
133//! ### WebAssembly (Wasm)
134//!
135//! Wasm platforms support memory-based operations only:
136//!
137//! - `XmpFile::from_bytes()` - Load from memory
138//! - `XmpFile::from_reader()` - Load from reader
139//! - `XmpFile::write_to_bytes()` - Write to memory
140//! - `XmpFile::write_to_writer()` - Write to writer
141//!
142//! To use xmpkit in JavaScript, enable the `wasm` feature:
143//!
144//! **Simple way (recommended):**
145//!
146//! 1. Create a new crate: `cargo new --lib my-wasm-app`
147//! 2. Add to `Cargo.toml`:
148//! ```toml
149//! [lib]
150//! crate-type = ["cdylib"]
151//! [dependencies]
152//! xmpkit = { version = "0.1.0", features = ["wasm"] }
153//! ```
154//! 3. Re-export bindings in `src/lib.rs`:
155//! ```rust,ignore
156//! pub use xmpkit::wasm::*;
157//! ```
158//! 4. Build: `wasm-pack build --target web`
159//!
160//! Then use in JavaScript:
161//!
162//! ```javascript
163//! import init, { read_xmp, write_xmp } from './pkg/my_wasm_app.js';
164//! await init();
165//! const result = read_xmp(new Uint8Array(/* file bytes */));
166//! ```
167//!
168//! **Alternative**: Create a custom binding crate (see `docs/WEBASSEMBLY.md` for details)
169//!
170//! ### OpenHarmony/HarmonyOS (ArkTS)
171//!
172//! To use xmpkit in OpenHarmony/HarmonyOS applications, enable the `ohos` feature:
173//!
174//! 1. Add to `Cargo.toml`:
175//! ```toml
176//! [lib]
177//! crate-type = ["cdylib"]
178//! [dependencies]
179//! xmpkit = { version = "0.1.0", features = ["ohos"] }
180//! ```
181//! 2. Re-export bindings in `src/lib.rs`:
182//! ```rust,ignore
183//! pub use xmpkit::ohos::*;
184//! ```
185//! 3. Build: `ohrs build`
186//!
187//! Then use in ArkTS:
188//!
189//! ```typescript
190//! import { XmpFile, XmpMeta } from 'libxmpkit.so';
191//! const file = new XmpFile();
192//! file.fromBytes(fileBytes);
193//! const meta = file.getXmp();
194//! ```
195//!
196//! ## Feature Flags
197//!
198//! - `core` - Core XMP functionality (enabled by default)
199//! - `files` - File format support infrastructure (enabled by default)
200//! - `jpeg`, `png`, `tiff`, `mp3`, `gif`, `mpeg4` - Individual file format handlers
201//! - `full-formats` - Enable all file format handlers (enabled by default)
202//! - `mutli-thread` - Multi-threaded runtime support (enabled by default)
203//! - `wasm` - WebAssembly JavaScript bindings (optional, enables wasm-bindgen integration)
204//! - `ohos` - OpenHarmony/HarmonyOS Node-API bindings (optional, enables napi-ohos integration)
205//!
206//! ## Supported File Formats
207//!
208//! | Format | Extensions | Read | Write |
209//! |--------|-----------|------|-------|
210//! | JPEG | .jpg, .jpeg | Yes | Yes |
211//! | PNG | .png | Yes | Yes |
212//! | TIFF | .tif, .tiff | Yes | Yes |
213//! | MP3 | .mp3 | Yes | Yes |
214//! | GIF | .gif | Yes | Yes |
215//! | MPEG4 | .mp4, .m4a, .m4v, .mov | Yes | Yes |
216
217#[cfg(feature = "core")]
218pub mod core;
219#[cfg(feature = "files")]
220pub mod files;
221pub mod types;
222pub mod utils;
223
224#[cfg(feature = "wasm")]
225pub mod wasm;
226
227#[cfg(all(feature = "ohos", target_ohos))]
228pub mod ohos;
229
230// Re-export commonly used types
231#[cfg(feature = "core")]
232pub use core::error::{XmpError, XmpResult};
233#[cfg(feature = "core")]
234pub use core::metadata::XmpMeta;
235#[cfg(feature = "core")]
236pub use core::namespace::{
237 get_all_registered_namespaces, get_builtin_namespace_uris, get_global_namespace_prefix,
238 get_global_namespace_uri, is_namespace_registered, ns, register_namespace,
239};
240#[cfg(feature = "files")]
241pub use files::{XmpFile, XmpOptions};
242pub use types::qualifier::Qualifier;
243pub use types::value::XmpValue;
244pub use utils::datetime::XmpDateTime;