move_stdlib_sdk/
lib.rs

1#![cfg_attr(all(doc, not(doctest)), feature(doc_auto_cfg))]
2
3//! Move types for the core `std` Sui package located at "0x1" onchain.
4
5af_sui_pkg_sdk::sui_pkg_sdk!(std @ "0x1" {
6    /// The `ASCII` module defines basic string and char newtypes in Move that verify
7    /// that characters are valid ASCII, and that strings consist of only valid ASCII characters.
8    module ascii {
9        /// The `String` struct holds a vector of bytes that all represent
10        /// valid ASCII characters. Note that these ASCII characters may not all
11        /// be printable. To determine if a `String` contains only "printable"
12        /// characters you should use the `all_characters_printable` predicate
13        /// defined in this module.
14        #[serde(transparent)]
15        struct String has copy, drop, store {
16            bytes: vector<u8>,
17        }
18
19        /// An ASCII character.
20        struct Char has copy, drop, store {
21            byte: u8,
22        }
23    }
24
25    module bit_vector {
26        struct BitVector has copy, drop, store {
27            length: u64,
28            bit_field: vector<bool>,
29        }
30    }
31
32    /// Defines a fixed-point numeric type with a 32-bit integer part and
33    /// a 32-bit fractional part.
34    module fixed_point32 {
35        /// Define a fixed-point numeric type with 32 fractional bits.
36        /// This is just a u64 integer but it is wrapped in a struct to
37        /// make a unique type. This is a binary representation, so decimal
38        /// values may not be exactly representable, but it provides more
39        /// than 9 decimal digits of precision both before and after the
40        /// decimal point (18 digits total). For comparison, double precision
41        /// floating-point has less than 16 decimal digits of precision, so
42        /// be careful about using floating-point to convert these values to
43        /// decimal.
44        struct FixedPoint32 has copy, drop, store { value: u64 }
45    }
46
47    /// This module defines the Option type and its methods to represent and handle an optional value.
48    module option {
49        /// Abstraction of a value that may or may not be present. Implemented with a vector of size
50        /// zero or one because Move bytecode does not have ADTs.
51        struct Option<Element> has copy, drop, store {
52            vec: vector<Element>
53        }
54    }
55
56    /// The `string` module defines the `String` type which represents UTF8 encoded strings.
57    module string {
58        /// Rust: prefer using Rust's native [`String`](std::string::String) which implements
59        /// `MoveType`.
60        ///
61        /// A `String` holds a sequence of bytes which is guaranteed to be in utf8 format.
62        struct String has copy, drop, store {
63            bytes: vector<u8>,
64        }
65    }
66
67    /// Functionality for converting Move types into values. Use with care!
68    module type_name {
69        struct TypeName has copy, drop, store {
70            /// String representation of the type. All types are represented
71            /// using their source syntax:
72            /// "u8", "u64", "bool", "address", "vector", and so on for primitive types.
73            /// Struct types are represented as fully qualified type names; e.g.
74            /// `00000000000000000000000000000001::string::String` or
75            /// `0000000000000000000000000000000a::module_name1::type_name1<0000000000000000000000000000000a::module_name2::type_name2<u64>>`
76            /// Addresses are hex-encoded lowercase values of length ADDRESS_LENGTH (16, 20, or 32 depending on the Move platform)
77            name: String
78        }
79    }
80});
81
82impl TryFrom<String> for ascii::String {
83    type Error = NotAscii;
84
85    fn try_from(value: String) -> Result<Self, Self::Error> {
86        if !value.is_ascii() {
87            return Err(NotAscii);
88        }
89        Ok(Self {
90            bytes: value.bytes().collect::<Vec<_>>().into(),
91        })
92    }
93}
94
95#[derive(thiserror::Error, Debug)]
96#[error("Not an ascii string")]
97pub struct NotAscii;
98
99impl TryFrom<ascii::String> for String {
100    type Error = std::string::FromUtf8Error;
101
102    fn try_from(value: ascii::String) -> Result<Self, Self::Error> {
103        Self::from_utf8(value.bytes.into())
104    }
105}