fn validate_constants(&self) -> Result<(), ParseError> {
        // Search ast for constants.
        for (ty, constant) in
            self.namespaces.iter().flat_map(|(_, decls)| decls.iter()).filter_map(|decl| match decl
            {
                Decl::Constant { ty, value, .. } => Some((ty, value)),
                _ => None,
            })
        {
            let Constant(string) = constant;
            if string.len() > 2
                && (string.get(0..2) == Some("0x") || string.get(0..2) == Some("0b"))
            {
                // TODO(bwb): validate constants if hex as well
                return Ok(());
            }
            match ty {
                Ty::Int8 => {
                    i8::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Int16 => {
                    i16::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Int32 => {
                    i32::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Int64 => {
                    i64::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::UInt8 => {
                    u8::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::UInt16 => {
                    u16::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::UInt32 | Ty::UInt64 => {
                    u32::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Bool => {
                    bool::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Str { .. } => {
                    if !string.starts_with("\"") || !string.ends_with("\"") {
                        return Err(ParseError::InvalidConstType(constant.clone(), ty.clone()));
                    }
                }
                Ty::Identifier { id, .. } => {
                    let ident = Ident::new_raw(string);
                    // Special case: enum references "Enum.foo".
                    if let Some(ns) = &ident.namespace {
                        if ns == &id.name {
                            // The constant is a reference to an enum variant.
                            continue;
                        }
                    }
                    return Err(ParseError::InvalidConstType(constant.clone(), ty.clone()));
                }
                _ => {
                    return Err(ParseError::InvalidConstType(constant.clone(), ty.clone()));
                }
            };
        }

        // TODO(surajmalhotra): Find every bound array, string, and validate their bound is a valid
        // usize.
        Ok(())
    }
// Validates that the constants are of the right type.
Validates that the constants are of the right type.
Validates that the constants are of the right type .
fn validate_constants(&self) -> Result<(), ParseError> {
        // Search ast for constants.
        for (ty, constant) in
            self.namespaces.iter().flat_map(|(_, decls)| decls.iter()).filter_map(|decl| match decl
            {
                Decl::Constant { ty, value, .. } => Some((ty, value)),
                _ => None,
            })
        {
            let Constant(string) = constant;
            if string.len() > 2
                && (string.get(0..2) == Some("0x") || string.get(0..2) == Some("0b"))
            {
                // TODO(bwb): validate constants if hex as well
                return Ok(());
            }
            match ty {
                Ty::Int8 => {
                    i8::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Int16 => {
                    i16::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Int32 => {
                    i32::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Int64 => {
                    i64::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::UInt8 => {
                    u8::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::UInt16 => {
                    u16::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::UInt32 | Ty::UInt64 => {
                    u32::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Bool => {
                    bool::from_str(string)
                        .map_err(|_| ParseError::InvalidConstType(constant.clone(), ty.clone()))?;
                }
                Ty::Str { .. } => {
                    if !string.starts_with("\"") || !string.ends_with("\"") {
                        return Err(ParseError::InvalidConstType(constant.clone(), ty.clone()));
                    }
                }
                Ty::Identifier { id, .. } => {
                    let ident = Ident::new_raw(string);
                    // Special case: enum references "Enum.foo".
                    if let Some(ns) = &ident.namespace {
                        if ns == &id.name {
                            // The constant is a reference to an enum variant.
                            continue;
                        }
                    }
                    return Err(ParseError::InvalidConstType(constant.clone(), ty.clone()));
                }
                _ => {
                    return Err(ParseError::InvalidConstType(constant.clone(), ty.clone()));
                }
            };
        }
        // TODO(surajmalhotra): Find every bound array, string, and validate their bound is a valid
        // usize.
        Ok(())
    }
fn validate_constants ( & self ) -> Result < ( ) , ParseError > { // Search ast for constants. for ( ty , constant ) in self . namespaces . iter ( ) . flat_map ( | ( _ , decls ) | decls . iter ( ) ) . filter_map ( | decl | match decl { Decl :: Constant { ty , value , .. } => Some ( ( ty , value ) ) , _ => None , } ) { let Constant ( string ) = constant ; if string . len ( ) > 2 && ( string . get ( 0 .. 2 ) == Some ( " " ) || string . get ( 0 .. 2 ) == Some ( " " ) ) { // TODO(bwb): validate constants if hex as well return Ok ( ( ) ) ; } match ty { Ty :: Int8 => { i8 :: from_str ( string ) . map_err ( | _ | ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ? ; } Ty :: Int16 => { i16 :: from_str ( string ) . map_err ( | _ | ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ? ; } Ty :: Int32 => { i32 :: from_str ( string ) . map_err ( | _ | ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ? ; } Ty :: Int64 => { i64 :: from_str ( string ) . map_err ( | _ | ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ? ; } Ty :: UInt8 => { u8 :: from_str ( string ) . map_err ( | _ | ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ? ; } Ty :: UInt16 => { u16 :: from_str ( string ) . map_err ( | _ | ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ? ; } Ty :: UInt32 | Ty :: UInt64 => { u32 :: from_str ( string ) . map_err ( | _ | ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ? ; } Ty :: Bool => { bool :: from_str ( string ) . map_err ( | _ | ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ? ; } Ty :: Str { .. } => { if ! string . starts_with ( " \" " ) || ! string . ends_with ( " \" " ) { return Err ( ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ; } } Ty :: Identifier { id , .. } => { let ident = Ident :: new_raw ( string ) ; // Special case: enum references "Enum.foo". if let Some ( ns ) = & ident . namespace { if ns == & id . name { // The constant is a reference to an enum variant. continue ; } } return Err ( ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ; } _ => { return Err ( ParseError :: InvalidConstType ( constant . clone ( ) , ty . clone ( ) ) ) ; } } ; } // TODO(surajmalhotra): Find every bound array, string, and validate their bound is a valid // usize. Ok ( ( ) ) }
Validates that the constants are of the right type.
Validates that the constants are of the right type .
&self
&self