Skip to content

Commit ce7e195

Browse files
committed
docs(stack-encrypt): say what a plan with scopes is, with a doc test
The doc of `Plan::with_scopes` said in one sentence what a scope does to a field's context, and it was hard to follow, as the review of #1595 said. It now says it in plain words, and a doc test shows the scope at the end of the field's context through `Pending::context()`. Refs #1486.
1 parent 4438430 commit ce7e195

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

‎packages/stack-encrypt/src/dynamic/record.rs‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,9 +1139,45 @@ impl Plan {
11391139
&self.bindings
11401140
}
11411141

1142-
/// This plan with `parts` after its extension: a caller's scopes, each
1143-
/// one more part of every field's context, as a plan's `"context"` key
1144-
/// carries them.
1142+
/// Add a caller's scopes to this plan. Each scope becomes one more part
1143+
/// at the end of every field's context, after the parts the plan
1144+
/// already has, in the order given. A scoped call runs the plan this
1145+
/// returns. So a field is sealed and indexed under its label, then the
1146+
/// plan's own extension, then the scopes. A call with no scope borrows
1147+
/// the plan instead, and copies nothing.
1148+
///
1149+
/// A plan with a field that names an EQL type takes no scope. An EQL
1150+
/// value is stored under its table and column alone.
1151+
///
1152+
/// The scope shows in the context of the call. Here a record under a
1153+
/// one-field plan is encrypted under the scope `42u64`:
1154+
///
1155+
/// ```
1156+
/// use stack_encrypt::dynamic::{record, Planned, ValueFrame};
1157+
/// use stack_encrypt::kms::FakeDataKeySource;
1158+
/// use stack_encrypt::target::Scope;
1159+
/// use stack_encrypt::{AsProtectedRef, EncryptInto, Pending, StackCipher, StackCipherText};
1160+
/// use vitaminc_aead_frame::serde::to_vec;
1161+
///
1162+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1163+
/// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?;
1164+
/// let keyset = cipher.default_keyset();
1165+
/// let declaration = to_vec(&serde_json::json!({
1166+
/// "email": { "context": ["users", "email"], "outputs": ["c"], "type": "string" },
1167+
/// }))?;
1168+
/// let plan = record::plan(declaration.as_protected_ref())?;
1169+
/// let values = ValueFrame::from_bytes(
1170+
/// to_vec(&serde_json::json!({"email": "ada@example.com"}))?.as_protected_ref(),
1171+
/// )?;
1172+
/// let call: Pending<'_, StackCipherText, FakeDataKeySource> =
1173+
/// Planned::new(&plan, values).encrypt_into_with_context(&keyset, Scope::new(42u64));
1174+
/// // The field's context is its label, then the scope.
1175+
/// # #[cfg(feature = "testing")]
1176+
/// stack_encrypt::assert_context!(call.context(), [["users", "email"], 42u64]);
1177+
/// let _row = call.await?;
1178+
/// # Ok::<(), Box<dyn std::error::Error>>(())
1179+
/// # }).unwrap();
1180+
/// ```
11451181
///
11461182
/// # Errors
11471183
///

0 commit comments

Comments
 (0)