cainome_parser/tokens/
non_zero.rs

1//! This module provides a token type for the `NonZero` type.
2//!
3//! <https://github.com/starkware-libs/cairo/blob/main/corelib/src/zeroable.cairo#L80>
4use crate::tokens::Token;
5use crate::{CainomeResult, Error};
6
7use super::composite::escape_rust_keywords;
8use super::genericity;
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct NonZero {
12    pub type_path: String,
13    pub inner: Box<Token>,
14}
15
16impl NonZero {
17    pub fn parse(type_path: &str) -> CainomeResult<Self> {
18        let type_path = escape_rust_keywords(type_path);
19
20        if type_path.starts_with("core::zeroable::NonZero") {
21            let generic_args = genericity::extract_generics_args(&type_path)?;
22
23            if generic_args.len() != 1 {
24                return Err(Error::InvalidNonZeroTypePath(type_path.to_string()));
25            }
26
27            let (_, generic_arg_token) = &generic_args[0];
28
29            Ok(Self {
30                type_path: type_path.to_string(),
31                inner: Box::new(generic_arg_token.clone()),
32            })
33        } else {
34            Err(Error::TokenInitFailed(format!(
35                "NonZero couldn't be initialized from `{}`.",
36                type_path,
37            )))
38        }
39    }
40
41    pub fn apply_alias(&mut self, type_path: &str, alias: &str) {
42        self.inner.apply_alias(type_path, alias);
43    }
44}