Unverified Commit 8b5eab68 authored by Nugine's avatar Nugine
Browse files

codegen: error: static code list

parent 30f5c363
Loading
Loading
Loading
Loading
+19 −6
Original line number Diff line number Diff line
@@ -159,18 +159,31 @@ pub fn codegen(model: &smithy::Model, g: &mut Codegen) {
    g.ln("impl S3ErrorCode {");

    {
        g.ln("#[must_use]");
        g.ln("pub fn as_str(&self) -> &str {");
        g.ln("const STATIC_CODE_LIST: &'static [&'static str] = &[");
        for err in errors.values() {
            g.ln(f!("\"{}\",", err.code));
        }
        g.ln("];");
        g.lf();

        g.ln("#[inline(always)]");
        g.ln("#[must_use]");
        g.ln("fn as_enum_tag(&self) -> usize {");
        g.ln("match self {");
        for err in errors.values() {
            g.ln(f!("Self::{} => \"{}\",", err.code, err.code));
        for (idx, err) in errors.values().enumerate() {
            g.ln(f!("Self::{} => {},", err.code, idx));
        }
        g.ln("Self::Custom(s) => s,");
        g.ln("Self::Custom(_) => usize::MAX,");
        g.ln("}");

        g.ln("}");
        g.lf();

        g.lines([
            "pub(crate) fn as_static_str(&self) -> Option<&'static str> {",
            "    Self::STATIC_CODE_LIST.get(self.as_enum_tag()).copied()",
            "}",
        ]);
        g.lf();
    }

    {
+170 −82
Original line number Diff line number Diff line
@@ -491,93 +491,181 @@ pub enum S3ErrorCode {
}

impl S3ErrorCode {
    const STATIC_CODE_LIST: &'static [&'static str] = &[
        "AccessDenied ",
        "AccountProblem",
        "AllAccessDisabled",
        "AmbiguousGrantByEmailAddress",
        "AuthorizationHeaderMalformed",
        "BadDigest",
        "BucketAlreadyExists",
        "BucketAlreadyOwnedByYou",
        "BucketNotEmpty",
        "CredentialsNotSupported",
        "CrossLocationLoggingProhibited",
        "EntityTooLarge",
        "EntityTooSmall",
        "ExpiredToken",
        "IllegalVersioningConfigurationException ",
        "IncompleteBody",
        "IncorrectNumberOfFilesInPostRequest",
        "InlineDataTooLarge",
        "InternalError",
        "InvalidAccessKeyId",
        "InvalidAddressingHeader",
        "InvalidArgument",
        "InvalidBucketName",
        "InvalidBucketState",
        "InvalidDigest",
        "InvalidEncryptionAlgorithmError",
        "InvalidLocationConstraint",
        "InvalidObjectState",
        "InvalidPart",
        "InvalidPartOrder",
        "InvalidPayer",
        "InvalidPolicyDocument",
        "InvalidRange",
        "InvalidRequest",
        "InvalidSOAPRequest",
        "InvalidSecurity",
        "InvalidStorageClass",
        "InvalidTargetBucketForLogging",
        "InvalidToken",
        "InvalidURI",
        "KeyTooLongError",
        "MalformedACLError",
        "MalformedPOSTRequest ",
        "MalformedXML",
        "MaxMessageLengthExceeded",
        "MaxPostPreDataLengthExceededError",
        "MetadataTooLarge",
        "MethodNotAllowed",
        "MissingAttachment",
        "MissingContentLength",
        "MissingRequestBodyError",
        "MissingSecurityElement",
        "MissingSecurityHeader",
        "NoLoggingStatusForKey",
        "NoSuchBucket",
        "NoSuchBucketPolicy",
        "NoSuchKey",
        "NoSuchLifecycleConfiguration",
        "NoSuchUpload",
        "NoSuchVersion ",
        "NotImplemented",
        "NotSignedUp",
        "OperationAborted",
        "PermanentRedirect",
        "PreconditionFailed",
        "Redirect",
        "RequestIsNotMultiPartContent",
        "RequestTimeTooSkewed",
        "RequestTimeout",
        "RequestTorrentOfBucketError",
        "RestoreAlreadyInProgress",
        "ServiceUnavailable",
        "SignatureDoesNotMatch",
        "SlowDown",
        "TemporaryRedirect",
        "TokenRefreshRequired",
        "TooManyBuckets",
        "UnexpectedContent",
        "UnresolvableGrantByEmailAddress",
        "UserKeyMustBeSpecified",
    ];

    #[inline(always)]
    #[must_use]
    pub fn as_str(&self) -> &str {
    fn as_enum_tag(&self) -> usize {
        match self {
            Self::AccessDenied => "AccessDenied ",
            Self::AccountProblem => "AccountProblem",
            Self::AllAccessDisabled => "AllAccessDisabled",
            Self::AmbiguousGrantByEmailAddress => "AmbiguousGrantByEmailAddress",
            Self::AuthorizationHeaderMalformed => "AuthorizationHeaderMalformed",
            Self::BadDigest => "BadDigest",
            Self::BucketAlreadyExists => "BucketAlreadyExists",
            Self::BucketAlreadyOwnedByYou => "BucketAlreadyOwnedByYou",
            Self::BucketNotEmpty => "BucketNotEmpty",
            Self::CredentialsNotSupported => "CredentialsNotSupported",
            Self::CrossLocationLoggingProhibited => "CrossLocationLoggingProhibited",
            Self::EntityTooLarge => "EntityTooLarge",
            Self::EntityTooSmall => "EntityTooSmall",
            Self::ExpiredToken => "ExpiredToken",
            Self::IllegalVersioningConfigurationException => "IllegalVersioningConfigurationException ",
            Self::IncompleteBody => "IncompleteBody",
            Self::IncorrectNumberOfFilesInPostRequest => "IncorrectNumberOfFilesInPostRequest",
            Self::InlineDataTooLarge => "InlineDataTooLarge",
            Self::InternalError => "InternalError",
            Self::InvalidAccessKeyId => "InvalidAccessKeyId",
            Self::InvalidAddressingHeader => "InvalidAddressingHeader",
            Self::InvalidArgument => "InvalidArgument",
            Self::InvalidBucketName => "InvalidBucketName",
            Self::InvalidBucketState => "InvalidBucketState",
            Self::InvalidDigest => "InvalidDigest",
            Self::InvalidEncryptionAlgorithmError => "InvalidEncryptionAlgorithmError",
            Self::InvalidLocationConstraint => "InvalidLocationConstraint",
            Self::InvalidObjectState => "InvalidObjectState",
            Self::InvalidPart => "InvalidPart",
            Self::InvalidPartOrder => "InvalidPartOrder",
            Self::InvalidPayer => "InvalidPayer",
            Self::InvalidPolicyDocument => "InvalidPolicyDocument",
            Self::InvalidRange => "InvalidRange",
            Self::InvalidRequest => "InvalidRequest",
            Self::InvalidSOAPRequest => "InvalidSOAPRequest",
            Self::InvalidSecurity => "InvalidSecurity",
            Self::InvalidStorageClass => "InvalidStorageClass",
            Self::InvalidTargetBucketForLogging => "InvalidTargetBucketForLogging",
            Self::InvalidToken => "InvalidToken",
            Self::InvalidURI => "InvalidURI",
            Self::KeyTooLongError => "KeyTooLongError",
            Self::MalformedACLError => "MalformedACLError",
            Self::MalformedPOSTRequest => "MalformedPOSTRequest ",
            Self::MalformedXML => "MalformedXML",
            Self::MaxMessageLengthExceeded => "MaxMessageLengthExceeded",
            Self::MaxPostPreDataLengthExceededError => "MaxPostPreDataLengthExceededError",
            Self::MetadataTooLarge => "MetadataTooLarge",
            Self::MethodNotAllowed => "MethodNotAllowed",
            Self::MissingAttachment => "MissingAttachment",
            Self::MissingContentLength => "MissingContentLength",
            Self::MissingRequestBodyError => "MissingRequestBodyError",
            Self::MissingSecurityElement => "MissingSecurityElement",
            Self::MissingSecurityHeader => "MissingSecurityHeader",
            Self::NoLoggingStatusForKey => "NoLoggingStatusForKey",
            Self::NoSuchBucket => "NoSuchBucket",
            Self::NoSuchBucketPolicy => "NoSuchBucketPolicy",
            Self::NoSuchKey => "NoSuchKey",
            Self::NoSuchLifecycleConfiguration => "NoSuchLifecycleConfiguration",
            Self::NoSuchUpload => "NoSuchUpload",
            Self::NoSuchVersion => "NoSuchVersion ",
            Self::NotImplemented => "NotImplemented",
            Self::NotSignedUp => "NotSignedUp",
            Self::OperationAborted => "OperationAborted",
            Self::PermanentRedirect => "PermanentRedirect",
            Self::PreconditionFailed => "PreconditionFailed",
            Self::Redirect => "Redirect",
            Self::RequestIsNotMultiPartContent => "RequestIsNotMultiPartContent",
            Self::RequestTimeTooSkewed => "RequestTimeTooSkewed",
            Self::RequestTimeout => "RequestTimeout",
            Self::RequestTorrentOfBucketError => "RequestTorrentOfBucketError",
            Self::RestoreAlreadyInProgress => "RestoreAlreadyInProgress",
            Self::ServiceUnavailable => "ServiceUnavailable",
            Self::SignatureDoesNotMatch => "SignatureDoesNotMatch",
            Self::SlowDown => "SlowDown",
            Self::TemporaryRedirect => "TemporaryRedirect",
            Self::TokenRefreshRequired => "TokenRefreshRequired",
            Self::TooManyBuckets => "TooManyBuckets",
            Self::UnexpectedContent => "UnexpectedContent",
            Self::UnresolvableGrantByEmailAddress => "UnresolvableGrantByEmailAddress",
            Self::UserKeyMustBeSpecified => "UserKeyMustBeSpecified",
            Self::Custom(s) => s,
            Self::AccessDenied => 0,
            Self::AccountProblem => 1,
            Self::AllAccessDisabled => 2,
            Self::AmbiguousGrantByEmailAddress => 3,
            Self::AuthorizationHeaderMalformed => 4,
            Self::BadDigest => 5,
            Self::BucketAlreadyExists => 6,
            Self::BucketAlreadyOwnedByYou => 7,
            Self::BucketNotEmpty => 8,
            Self::CredentialsNotSupported => 9,
            Self::CrossLocationLoggingProhibited => 10,
            Self::EntityTooLarge => 11,
            Self::EntityTooSmall => 12,
            Self::ExpiredToken => 13,
            Self::IllegalVersioningConfigurationException => 14,
            Self::IncompleteBody => 15,
            Self::IncorrectNumberOfFilesInPostRequest => 16,
            Self::InlineDataTooLarge => 17,
            Self::InternalError => 18,
            Self::InvalidAccessKeyId => 19,
            Self::InvalidAddressingHeader => 20,
            Self::InvalidArgument => 21,
            Self::InvalidBucketName => 22,
            Self::InvalidBucketState => 23,
            Self::InvalidDigest => 24,
            Self::InvalidEncryptionAlgorithmError => 25,
            Self::InvalidLocationConstraint => 26,
            Self::InvalidObjectState => 27,
            Self::InvalidPart => 28,
            Self::InvalidPartOrder => 29,
            Self::InvalidPayer => 30,
            Self::InvalidPolicyDocument => 31,
            Self::InvalidRange => 32,
            Self::InvalidRequest => 33,
            Self::InvalidSOAPRequest => 34,
            Self::InvalidSecurity => 35,
            Self::InvalidStorageClass => 36,
            Self::InvalidTargetBucketForLogging => 37,
            Self::InvalidToken => 38,
            Self::InvalidURI => 39,
            Self::KeyTooLongError => 40,
            Self::MalformedACLError => 41,
            Self::MalformedPOSTRequest => 42,
            Self::MalformedXML => 43,
            Self::MaxMessageLengthExceeded => 44,
            Self::MaxPostPreDataLengthExceededError => 45,
            Self::MetadataTooLarge => 46,
            Self::MethodNotAllowed => 47,
            Self::MissingAttachment => 48,
            Self::MissingContentLength => 49,
            Self::MissingRequestBodyError => 50,
            Self::MissingSecurityElement => 51,
            Self::MissingSecurityHeader => 52,
            Self::NoLoggingStatusForKey => 53,
            Self::NoSuchBucket => 54,
            Self::NoSuchBucketPolicy => 55,
            Self::NoSuchKey => 56,
            Self::NoSuchLifecycleConfiguration => 57,
            Self::NoSuchUpload => 58,
            Self::NoSuchVersion => 59,
            Self::NotImplemented => 60,
            Self::NotSignedUp => 61,
            Self::OperationAborted => 62,
            Self::PermanentRedirect => 63,
            Self::PreconditionFailed => 64,
            Self::Redirect => 65,
            Self::RequestIsNotMultiPartContent => 66,
            Self::RequestTimeTooSkewed => 67,
            Self::RequestTimeout => 68,
            Self::RequestTorrentOfBucketError => 69,
            Self::RestoreAlreadyInProgress => 70,
            Self::ServiceUnavailable => 71,
            Self::SignatureDoesNotMatch => 72,
            Self::SlowDown => 73,
            Self::TemporaryRedirect => 74,
            Self::TokenRefreshRequired => 75,
            Self::TooManyBuckets => 76,
            Self::UnexpectedContent => 77,
            Self::UnresolvableGrantByEmailAddress => 78,
            Self::UserKeyMustBeSpecified => 79,
            Self::Custom(_) => usize::MAX,
        }
    }

    pub(crate) fn as_static_str(&self) -> Option<&'static str> {
        Self::STATIC_CODE_LIST.get(self.as_enum_tag()).copied()
    }

    #[must_use]
    pub fn from_bytes(s: &[u8]) -> Option<Self> {
        match s {
+12 −0
Original line number Diff line number Diff line
@@ -193,3 +193,15 @@ impl FromStr for S3ErrorCode {
        Ok(Self::from_bytes(s.as_bytes()).unwrap())
    }
}

impl S3ErrorCode {
    pub fn as_str(&self) -> &str {
        if let Some(s) = self.as_static_str() {
            return s;
        }
        if let Self::Custom(s) = self {
            return s;
        }
        unreachable!()
    }
}