Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add Definition::Primitive #222

Merged
merged 1 commit into from
Sep 18, 2023
Merged

feat!: add Definition::Primitive #222

merged 1 commit into from
Sep 18, 2023

Conversation

dj8yfo
Copy link
Collaborator

@dj8yfo dj8yfo commented Sep 15, 2023

this pr takes a step back and attempts to add definitions to all types, supported by BorshSchema,
in order to allow remove comments like

    /// Zero-sized types should follow the convention of either providing a [Definition] or
    /// specifying `"nil"` as their [Declaration] for this method to work correctly.

from various methods

@dj8yfo dj8yfo force-pushed the definition_of_primitives branch 2 times, most recently from f3eae2f to ff32f39 Compare September 15, 2023 18:07
@@ -326,6 +346,7 @@ mod tests {
test_ok::<Option<()>>(1);
test_ok::<Option<u8>>(2);
test_ok::<core::result::Result<u8, usize>>(9);
test_ok::<core::result::Result<u8, Vec<u8>>>(1 + 4 + MAX_LEN);
Copy link
Collaborator Author

@dj8yfo dj8yfo Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current master results in this fail of test assertion:

---- schema::container_ext::max_size::tests::max_serialized_size_built_in_types stdout ----
thread 'schema::container_ext::max_size::tests::max_serialized_size_built_in_types' panicked at 'assertion failed: `(left == right)`
  left: `Ok(9)`,
 right: `Err(MissingDefinition("Vec<u8>"))`'

@@ -48,7 +48,7 @@ fn validate_for_derived_types() {
test_ok::<Empty>();
test_ok::<Named>();
test_ok::<Unnamed>();
test_ok::<BorshSchemaContainer>();
// test_ok::<BorshSchemaContainer>();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thread 'validate_for_derived_types' panicked at 'assertion failed: `(left == right)`
  left: `Ok(())`,
 right: `Err(MissingDefinition("string"))`'

Copy link
Collaborator Author

@dj8yfo dj8yfo Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where str and String would be

pub enum Definition {
    /// `special_encoding` of `true` indicates that not all 
    /// sequences of `elements` type, allowed by the type, are valid with respect
    /// to `Sequence` type being defined 
    Sequence { elements: Declaration, special_encoding: bool },
    ...
}
impl BorshSchema for String {
    #[inline]
    fn add_definitions_recursively(
        definitions: &mut BTreeMap<Declaration, Definition>,
    ) {
        let definition = Definition::Sequence {
            elements: u8::declaration(),
            special_encoding: true,
        };
        add_definition(Self::declaration(), definition, definitions);
        u8::add_definitions_recursively(definitions);
        
    }
    #[inline]
    fn declaration() -> Declaration {
        "string".into()
    }
}

or just

        let definition = Definition::Sequence {
            elements: u8::declaration(),
        };

@dj8yfo dj8yfo force-pushed the definition_of_primitives branch 3 times, most recently from 46e4dfc to 1e6acb9 Compare September 15, 2023 19:14
@dj8yfo dj8yfo marked this pull request as ready for review September 15, 2023 19:23
@dj8yfo dj8yfo requested a review from frol as a code owner September 15, 2023 19:23
@dj8yfo dj8yfo force-pushed the definition_of_primitives branch 4 times, most recently from cbbc1ef to 66bf52e Compare September 18, 2023 19:52
@dj8yfo dj8yfo marked this pull request as draft September 18, 2023 19:54
@dj8yfo dj8yfo marked this pull request as ready for review September 18, 2023 19:58
@dj8yfo dj8yfo merged commit 4ff0a06 into master Sep 18, 2023
7 checks passed
@dj8yfo dj8yfo deleted the definition_of_primitives branch September 18, 2023 22:24
@frol frol mentioned this pull request Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants