-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
var
interface change and docs (#1962)
<!-- Reference any GitHub issues resolved by this PR --> Closes #1950 ## Introduced changes <!-- A brief description of the changes --> - ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md`
- Loading branch information
Showing
10 changed files
with
68 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,15 @@ | ||
# `var` | ||
|
||
> `fn var(name: ByteArray) -> felt252` | ||
> `fn var(name: ByteArray) -> Array<felt252>` | ||
Read and parse felt252 or cairo short string (encoded as felt) value from an environment variable. | ||
Reads an environment variable, without parsing it. | ||
|
||
- `name` - name of an environment variable | ||
The serialized output is correlated with the inferred input type, same as during [reading from a file](../fs/read_txt.md#accepted-format). | ||
|
||
```rust | ||
use snforge_std::env::var; | ||
> 📝 **Note** | ||
> | ||
> If you want snfoundry to treat your variable like a short string, surround it with 'single quotes'. | ||
> | ||
> If you would like it to be serialized as a ByteArray, use "double quoting". It will be then de-serializable with `Serde`. | ||
#[test] | ||
fn reading_env_vars() { | ||
// ... | ||
let felt252_value = var("FELT_ENV_VAR"); | ||
let short_string_value = var("STRING_ENV_VAR"); | ||
|
||
assert(felt252_value == 987654321, 'invalid felt value'); | ||
assert(short_string_value == 'abcde', 'invalid short string value'); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
use starknet::testing::cheatcode; | ||
use super::super::byte_array::byte_array_as_felt_array; | ||
|
||
fn var(name: ByteArray) -> felt252 { | ||
let outputs = cheatcode::<'var'>(byte_array_as_felt_array(@name).span()); | ||
*outputs[0] | ||
/// Reads an environment variable, without parsing it | ||
/// `name` - name of an environment variable | ||
/// Returns the read array of felts | ||
fn var(name: ByteArray) -> Array<felt252> { | ||
let mut output_array: Array<felt252> = array![]; | ||
let result = cheatcode::<'var'>(byte_array_as_felt_array(@name).span()); | ||
output_array.append_span(result); | ||
output_array | ||
} |