|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use azure_core::{ |
| 5 | + headers::{ |
| 6 | + FromHeaders, HeaderName, Headers, BLOB_ACCESS_TIER, BLOB_TYPE, CREATION_TIME, LEASE_STATE, |
| 7 | + LEASE_STATUS, SERVER_ENCRYPTED, |
| 8 | + }, |
| 9 | + Error, Etag, LeaseStatus, |
| 10 | +}; |
| 11 | +use typespec_client_core::fmt::SafeDebug; |
| 12 | + |
| 13 | +use crate::models::{AccessTier, BlobType, LeaseState}; |
| 14 | + |
| 15 | +pub const CONTENT_LENGTH: HeaderName = HeaderName::from_static("content-length"); |
| 16 | +pub const CONTENT_MD5: HeaderName = HeaderName::from_static("content-md5"); |
| 17 | +pub const CONTENT_TYPE: HeaderName = HeaderName::from_static("content-type"); |
| 18 | +pub const ETAG: HeaderName = HeaderName::from_static("etag"); |
| 19 | +pub const LAST_MODIFIED: HeaderName = HeaderName::from_static("last-modified"); |
| 20 | +pub const BLOB_ACCESS_TIER_INFERRED: HeaderName = |
| 21 | + HeaderName::from_static("x-ms-access-tier-inferred"); |
| 22 | + |
| 23 | +/// Properties of an Azure Storage blob. |
| 24 | +/// |
| 25 | +#[derive(Clone, Default, SafeDebug)] |
| 26 | +pub struct BlobProperties { |
| 27 | + pub access_tier_inferred: Option<bool>, |
| 28 | + pub access_tier: Option<AccessTier>, |
| 29 | + pub blob_type: Option<BlobType>, |
| 30 | + pub content_length: Option<i64>, |
| 31 | + pub content_md5: Option<String>, |
| 32 | + pub content_type: Option<String>, |
| 33 | + pub creation_time: Option<String>, |
| 34 | + pub etag: Option<Etag>, |
| 35 | + pub last_modified: Option<String>, |
| 36 | + pub lease_state: Option<LeaseState>, |
| 37 | + pub lease_status: Option<LeaseStatus>, |
| 38 | + pub server_encrypted: Option<bool>, |
| 39 | +} |
| 40 | + |
| 41 | +impl FromHeaders for BlobProperties { |
| 42 | + type Error = Error; |
| 43 | + fn header_names() -> &'static [&'static str] { |
| 44 | + &[ |
| 45 | + "content-length", |
| 46 | + "content-md5", |
| 47 | + "content-type", |
| 48 | + "etag", |
| 49 | + "last-modified", |
| 50 | + "x-ms-access-tier-inferred", |
| 51 | + "x-ms-access-tier", |
| 52 | + "x-ms-blob-type", |
| 53 | + "x-ms-creation-time", |
| 54 | + "x-ms-lease-state", |
| 55 | + "x-ms-lease-status", |
| 56 | + "x-ms-server-encrypted", |
| 57 | + ] |
| 58 | + } |
| 59 | + |
| 60 | + fn from_headers(headers: &Headers) -> Result<Option<Self>, Error> { |
| 61 | + let mut properties = BlobProperties { |
| 62 | + ..Default::default() |
| 63 | + }; |
| 64 | + |
| 65 | + let access_tier_inferred: Option<bool> = |
| 66 | + headers.get_optional_as(&BLOB_ACCESS_TIER_INFERRED)?; |
| 67 | + properties.access_tier_inferred = access_tier_inferred; |
| 68 | + |
| 69 | + let access_tier: Option<AccessTier> = headers.get_optional_as(&BLOB_ACCESS_TIER)?; |
| 70 | + properties.access_tier = access_tier; |
| 71 | + |
| 72 | + let blob_type: Option<BlobType> = headers.get_optional_as(&BLOB_TYPE)?; |
| 73 | + properties.blob_type = blob_type; |
| 74 | + |
| 75 | + let content_length: Option<i64> = headers.get_optional_as(&CONTENT_LENGTH)?; |
| 76 | + properties.content_length = content_length; |
| 77 | + |
| 78 | + let content_md5 = headers.get_optional_str(&CONTENT_MD5); |
| 79 | + properties.content_md5 = content_md5.map(|s| s.to_string()); |
| 80 | + |
| 81 | + let content_type = headers.get_optional_str(&CONTENT_TYPE); |
| 82 | + properties.content_type = content_type.map(|s| s.to_string()); |
| 83 | + |
| 84 | + let creation_time = headers.get_optional_str(&CREATION_TIME); |
| 85 | + properties.creation_time = creation_time.map(|s| s.to_string()); |
| 86 | + |
| 87 | + let etag: Option<Etag> = headers.get_optional_as(&ETAG)?; |
| 88 | + properties.etag = etag; |
| 89 | + |
| 90 | + let last_modified = headers.get_optional_str(&LAST_MODIFIED); |
| 91 | + properties.last_modified = last_modified.map(|s| s.to_string()); |
| 92 | + |
| 93 | + let lease_state: Option<LeaseState> = headers.get_optional_as(&LEASE_STATE)?; |
| 94 | + properties.lease_state = lease_state; |
| 95 | + |
| 96 | + let lease_status: Option<LeaseStatus> = headers.get_optional_as(&LEASE_STATUS)?; |
| 97 | + properties.lease_status = lease_status; |
| 98 | + |
| 99 | + let server_encrypted: Option<bool> = headers.get_optional_as(&SERVER_ENCRYPTED)?; |
| 100 | + properties.server_encrypted = server_encrypted; |
| 101 | + |
| 102 | + Ok(Some(properties)) |
| 103 | + } |
| 104 | +} |
0 commit comments