Skip to main content

ContractDraft

Struct ContractDraft 

Source
pub struct ContractDraft {
    pub types: Vec<TypeNode>,
    pub declarations: Vec<Declaration>,
    pub actor: Option<Actor>,
    pub producer: Option<ProducerInfo>,
}
Expand description

A producer-side Contract draft: the parts an authoring tool supplies, and nothing it must not.

A draft carries only the type graph, named declarations, an optional actor, and optional producer metadata. It deliberately has no format/version/profile markers and no identity fields: building stamps the current CONTRACT_FORMAT/FORMAT_VERSION/SEMANTICS_PROFILE/ CANONICALIZATION_PROFILE constants and calculates fresh identities under the same validation and canonicalization budgets as every other entry point, so a draft can never carry a fake, stale, or placeholder identity. RawContract is the opposite boundary — the serde DTO for decoded external artifacts, whose supplied identities Contract::try_from_raw verifies instead of recalculating.

§Serialized shape

A serialized draft contains exactly the four fields above. Unknown keys are rejected; declarations defaults to empty when absent; actor is omitted when absent, and an explicit "actor": null is rejected just as RawContract rejects it; producer is omitted when absent and defaults at build time to ProducerInfo::current, while a present producer overrides that default.

use candid_core::{ContractDraft, PrimitiveType, TypeNode};

let contract = ContractDraft::new(
    vec![TypeNode::Primitive { primitive: PrimitiveType::Nat }],
    vec![candid_core::Declaration { name: "Amount".to_string(), ty: 0 }],
    None,
)
.build()?;
assert!(contract.contract_id().starts_with("candid-core:contract:v1:sha256:"));
assert_eq!(contract.producer(), &candid_core::ProducerInfo::current());

Fields§

§types: Vec<TypeNode>§declarations: Vec<Declaration>§actor: Option<Actor>

An actorless draft omits this property entirely; "actor": null is rejected on decode, exactly as RawContract rejects it.

§producer: Option<ProducerInfo>

Untrusted provenance about the authoring tool. None builds with ProducerInfo::current. Never part of a semantic identity; see ProducerInfo. An explicit "producer": null is rejected on decode: absence is the only spelling of “default producer”, mirroring the actor rule.

Implementations§

Source§

impl ContractDraft

Source

pub fn new( types: Vec<TypeNode>, declarations: Vec<Declaration>, actor: Option<Actor>, ) -> Self

Source

pub fn with_producer(self, producer: ProducerInfo) -> Self

Returns self with an explicit producer, overriding the ProducerInfo::current default applied at build time.

Source

pub fn build(self) -> Result<Contract, ContractValidationError>

Validate the draft graph, canonicalize it, and calculate its identities under Limits::default.

Source

pub fn build_with_limits( self, limits: &Limits, ) -> Result<Contract, ContractValidationError>

Build under caller-supplied limits.

Source

pub fn build_with_context( self, context: &RuntimeContext, ) -> Result<Contract, ContractValidationError>

Build under the caller’s context, sharing its budget, deadline, and cancellation token.

Trait Implementations§

Source§

impl Clone for ContractDraft

Source§

fn clone(&self) -> ContractDraft

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ContractDraft

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ContractDraft

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for ContractDraft

Source§

impl PartialEq for ContractDraft

Source§

fn eq(&self, other: &ContractDraft) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for ContractDraft

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for ContractDraft

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.