Skip to main content

tightbeam_asn/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(any(target_arch = "wasm32", feature = "emit")), no_std)]
3
4extern crate alloc;
5
6// Each Rust sample of docs/asn.md compiles as a doctest of this crate.
7#[cfg(doctest)]
8#[doc = include_str!("../../../docs/asn.md")]
9mod docs_asn {}
10
11mod error;
12#[cfg(target_arch = "wasm32")]
13mod js;
14mod leaf;
15
16#[cfg(feature = "emit")]
17pub mod emit;
18
19pub use error::FieldError;
20pub use tightbeam_asn_derive::AsnCodec;
21
22/// The RustCrypto `der` crate, which encodes and decodes each DER type.
23pub use der;
24
25#[cfg(target_arch = "wasm32")]
26use wasm_bindgen::JsValue;
27
28/// The items that the `#[derive(AsnCodec)]` expansion names in the deriving
29/// crate.
30#[doc(hidden)]
31pub mod __private {
32	#[cfg(target_arch = "wasm32")]
33	pub use crate::js::{ChoiceEntry, JsField, JsList, JsObject, JsRecord, WireError};
34	pub use crate::leaf::{JsLeaf, TsSection};
35
36	/// The supertrait that reserves [`AsnCodec`](crate::AsnCodec) for the
37	/// derive, by convention.
38	pub trait Sealed {}
39}
40
41/// A `der` type with codec exports for TypeScript, which the derive implements:
42/// [docs/asn.md][crates].
43///
44/// [crates]: https://github.com/wahidgroup/tightbeam-ext/blob/master/docs/asn.md#crates
45pub trait AsnCodec: __private::Sealed + __private::JsLeaf {
46	/// Build `Self` from a plain JavaScript value.
47	///
48	/// # Errors
49	///
50	/// - The [`FieldError`] of a `value`, or a field inside it, that does not fit its DER type.
51	#[cfg(target_arch = "wasm32")]
52	fn from_js(value: &JsValue) -> Result<Self, FieldError> {
53		<Self as __private::JsLeaf>::read(value)
54	}
55
56	/// Project `Self` into a plain JavaScript value.
57	///
58	/// # Errors
59	///
60	/// - The [`FieldError`] of a DER projection or an object write that failed.
61	#[cfg(target_arch = "wasm32")]
62	fn to_js(&self) -> Result<JsValue, FieldError> {
63		<Self as __private::JsLeaf>::project(self)
64	}
65}