Skip to main content

Target

Struct Target 

Source
pub struct Target {
    pub app_id: String,
    pub table_name: String,
    pub field_name: String,
}
Expand description

The record field a stored file is destined for.

Fields§

§app_id: String

The app that owns the table.

§table_name: String

The table the record lives in.

§field_name: String

The :document field the handle is stored in.

Implementations§

Source§

impl Target

Source

pub fn new( app_id: impl Into<String>, table_name: impl Into<String>, field_name: impl Into<String>, ) -> Target

A target named by its app, its table and its field.

use simpleplatform_sdk::storage::Target;

let target = Target::new("dev.simple.system", "documents", "attachment");

assert_eq!(target.field_name, "attachment");
Examples found in repository?
examples/file_expense_receipt.rs (line 124)
106fn handler(request: Request<Input>) -> Result<Output, Error> {
107    let employee = request.data.employee_id.trim();
108    let receipt_url = request.data.receipt_url.trim();
109
110    if employee.is_empty() {
111        return Err(Error::invalid("An expense is filed against an employee.")
112            .hint("Pass the id of the employee the receipt belongs to."));
113    }
114
115    if receipt_url.is_empty() {
116        return Err(Error::invalid("An expense is filed from a receipt.")
117            .hint("Pass the address the receipt is read from."));
118    }
119
120    let policy: Policy = simple::settings::get(APP_ID, &["approval_limit", "currency"])?;
121
122    let receipt = simple::storage::upload_external(
123        Source::url(receipt_url),
124        Target::new(APP_ID, "expense", "receipt"),
125    )?;
126
127    let read: Execution<Receipt> = simple::ai::extract(
128        simple::serde_json::to_value(&receipt)?,
129        "Read the merchant, the currency and the total from this receipt.",
130        json!({
131            "type": "object",
132            "properties": {
133                "merchant": { "type": "string" },
134                "currency": { "type": "string" },
135                "total": { "type": "number" }
136            },
137            "required": ["merchant", "currency", "total"]
138        }),
139        Options::default(),
140    )?;
141
142    let claim = read.data;
143
144    if claim.currency != policy.currency {
145        return Err(Error::domain(
146            "RECEIPT_CURRENCY_MISMATCH",
147            format!(
148                "The receipt is in {} and this app files expenses in {}.",
149                claim.currency, policy.currency
150            ),
151        )
152        .details(json!({ "expected": policy.currency, "receipt": claim.currency }))
153        .hint("File it against an app configured for that currency. The file is stored; the expense is not."));
154    }
155
156    if claim.total > policy.approval_limit {
157        return Err(Error::domain(
158            "EXPENSE_OVER_LIMIT",
159            format!(
160                "{} {} is over the {} approval limit.",
161                claim.total, claim.currency, policy.approval_limit
162            ),
163        )
164        .details(json!({
165            "limit": policy.approval_limit,
166            "merchant": claim.merchant,
167            "total": claim.total,
168        }))
169        .hint("Send it for approval instead. The file is stored; the expense is not."));
170    }
171
172    let filed: Filed = simple::graphql::mutate(
173        FILE_EXPENSE,
174        json!({
175            "employee": employee,
176            "merchant": &claim.merchant,
177            "receipt": &receipt,
178            "total": claim.total,
179        }),
180    )?;
181
182    Ok(Output {
183        expense_id: filed.expense.id,
184        merchant: claim.merchant,
185        total: claim.total,
186        receipt,
187    })
188}

Trait Implementations§

Source§

impl Clone for Target

Source§

fn clone(&self) -> Target

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 Target

Source§

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

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

impl Default for Target

Source§

fn default() -> Target

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Target

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 Target

Source§

impl PartialEq for Target

Source§

fn eq(&self, other: &Target) -> 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 Target

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 Target

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<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> 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.