Compare commits
7 Commits
59830deeff
...
v0.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 57403929b6 | |||
| ec3cad1a05 | |||
| ef0d9f4395 | |||
| 7bd24ff0e7 | |||
| fa161be227 | |||
| 465aa8dfab | |||
| 6766f04578 |
15
Cargo.toml
15
Cargo.toml
@@ -13,7 +13,7 @@ categories.workspace = true
|
||||
readme.workspace = true
|
||||
|
||||
[workspace.package]
|
||||
version = "0.2.1"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.81"
|
||||
|
||||
@@ -26,7 +26,7 @@ categories = ["api-bindings"]
|
||||
readme = "README.md"
|
||||
|
||||
[dependencies]
|
||||
aws-macros = { path = "./aws_macros", version = "0.2.*" }
|
||||
aws-macros = { path = "./aws_macros", version = "0.3.*" }
|
||||
aws-config = { version = "1.*", default-features = false }
|
||||
aws-sdk-ec2 = { version = "1.*", default-features = false, features = [
|
||||
"rustls",
|
||||
@@ -56,8 +56,15 @@ chrono = { version = "0.4.*", default-features = false, features = [
|
||||
serde = { version = "1.*", default-features = false, features = [
|
||||
"std",
|
||||
"derive",
|
||||
] }
|
||||
serde_json = { version = "1.*", default-features = false, features = ["std"] }
|
||||
], optional = true }
|
||||
serde_json = { version = "1.*", default-features = false, features = [
|
||||
"std",
|
||||
], optional = true }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
serde = ["dep:serde"]
|
||||
serde-tags = ["dep:serde", "dep:serde_json"]
|
||||
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
|
||||
5
Makefile
5
Makefile
@@ -7,11 +7,12 @@ docs:
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
@cargo test --workspace --color=always
|
||||
cargo hack --feature-powerset --no-dev-deps check
|
||||
cargo test --workspace --color=always
|
||||
|
||||
.PHONY: lint
|
||||
lint:
|
||||
@cargo clippy --workspace --tests --color=always
|
||||
cargo clippy --workspace --tests --color=always
|
||||
|
||||
.PHONY: fmt
|
||||
fmt:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# AWS
|
||||
|
||||
* Helper functions for frequently used AWS operations (create EC2 instance, Route53 record, ...)
|
||||
* Type-safe operations on AWS tags (e.g. from an EC2 instance), see [the separate README](./src/tags/README.md)
|
||||
|
||||
- Helper functions for frequently used AWS operations (create EC2 instance,
|
||||
Route53 record, ...)
|
||||
- Type-safe operations on AWS tags (e.g. from an EC2 instance), see
|
||||
[the separate README](./src/tags/README.md)
|
||||
|
||||
@@ -209,7 +209,7 @@ pub(crate) fn transform(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<#name> for RawTagValue {
|
||||
impl From<#name> for #root::tags::RawTagValue {
|
||||
fn from(value: #name) -> Self {
|
||||
<#ty as #root::tags::TagValue<#ty>>::into_raw_tag(value.0)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,26 @@ struct Element {
|
||||
ty: syn::Path,
|
||||
kind: ElementKind,
|
||||
name: String,
|
||||
attrs: Vec<syn::Attribute>,
|
||||
}
|
||||
|
||||
fn is_cfg_attribute(attr: &syn::Attribute) -> bool {
|
||||
match attr.meta {
|
||||
syn::Meta::List(ref meta_list) => {
|
||||
let segments = &meta_list.path.segments;
|
||||
match (segments.first(), segments.len()) {
|
||||
(Some(segment), 1) => segment.ident == "cfg",
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn cfg_attrs(v: &[syn::Attribute]) -> Vec<&syn::Attribute> {
|
||||
v.iter()
|
||||
.filter(|attr: &&syn::Attribute| is_cfg_attribute(attr))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parse_type(input: syn::Type) -> (syn::Path, ElementKind) {
|
||||
@@ -66,22 +86,31 @@ fn parse_type(input: syn::Type) -> (syn::Path, ElementKind) {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_field_attrs(attrs: &[syn::Attribute]) -> Option<String> {
|
||||
match (attrs.first(), attrs.len()) {
|
||||
(Some(attr), 1) => {
|
||||
assert!(
|
||||
attr.style == syn::AttrStyle::Outer,
|
||||
"field attribute style needs to be an outer attribute"
|
||||
);
|
||||
match attr.meta {
|
||||
fn parse_field_attrs(attrs: &mut Vec<syn::Attribute>) -> Option<String> {
|
||||
let index_of_tag_attribute = attrs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(_i, attr)| attr.style == syn::AttrStyle::Outer)
|
||||
.find_map(|(i, attr)| match attr.meta {
|
||||
syn::Meta::List(ref meta_list) => {
|
||||
let tag = &meta_list.path;
|
||||
let tag_name = match (tag.segments.first(), tag.segments.len()) {
|
||||
(Some(segment), 1) => segment.ident.to_string(),
|
||||
(_, 0) => return None,
|
||||
_ => panic!("invalid field attribute path"),
|
||||
};
|
||||
assert!(tag_name == "tag", "invalid field attribute path {tag_name}");
|
||||
if let (Some(segment), 1) = (tag.segments.first(), tag.segments.len()) {
|
||||
if segment.ident == "tag" {
|
||||
Some((i, meta_list.clone()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
|
||||
match index_of_tag_attribute {
|
||||
Some((i, meta_list)) => {
|
||||
let removed_attribute = attrs.remove(i);
|
||||
drop(removed_attribute);
|
||||
|
||||
let expr: syn::Expr = match meta_list.parse_args() {
|
||||
Ok(expr) => expr,
|
||||
@@ -112,22 +141,18 @@ fn parse_field_attrs(attrs: &[syn::Attribute]) -> Option<String> {
|
||||
_ => panic!("right side of tag field attribute not a literal"),
|
||||
}
|
||||
}
|
||||
_ => panic!("invalid field attribute"),
|
||||
}
|
||||
}
|
||||
(_, 0) => None,
|
||||
_ => panic!("invalid field attributes"),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_fields(input: impl IntoIterator<Item = syn::Field>) -> Vec<Element> {
|
||||
let mut elements = Vec::new();
|
||||
for field in input {
|
||||
for mut field in input {
|
||||
let ident = field.ident.expect("tuple structs not supported");
|
||||
let vis = field.vis;
|
||||
let (ty, kind) = parse_type(field.ty);
|
||||
|
||||
let name = parse_field_attrs(&field.attrs);
|
||||
let name = parse_field_attrs(&mut field.attrs);
|
||||
|
||||
elements.push(Element {
|
||||
ident: ident.clone(),
|
||||
@@ -135,6 +160,7 @@ fn parse_fields(input: impl IntoIterator<Item = syn::Field>) -> Vec<Element> {
|
||||
ty,
|
||||
kind,
|
||||
name: name.unwrap_or_else(|| ident.to_string()),
|
||||
attrs: field.attrs,
|
||||
});
|
||||
}
|
||||
elements
|
||||
@@ -165,14 +191,19 @@ fn build_output(input: Input) -> TokenStream {
|
||||
let ident = &element.ident;
|
||||
let vis = &element.vis;
|
||||
let ty = &element.ty;
|
||||
let attrs = &element.attrs;
|
||||
match element.kind {
|
||||
ElementKind::Required => {
|
||||
quote!(
|
||||
#(#attrs)
|
||||
*
|
||||
#vis #ident: #ty
|
||||
)
|
||||
}
|
||||
ElementKind::Optional => {
|
||||
quote!(
|
||||
#(#attrs)
|
||||
*
|
||||
#vis #ident: ::std::option::Option<#ty>
|
||||
)
|
||||
}
|
||||
@@ -191,9 +222,18 @@ fn build_output(input: Input) -> TokenStream {
|
||||
let params = input.elements.iter().map(|element| {
|
||||
let ident = &element.ident;
|
||||
let ty = &element.ty;
|
||||
let attrs = cfg_attrs(&element.attrs);
|
||||
match element.kind {
|
||||
ElementKind::Required => quote!(#ident: #ty),
|
||||
ElementKind::Optional => quote!(#ident: ::std::option::Option<#ty>),
|
||||
ElementKind::Required => quote! {
|
||||
#(#attrs)
|
||||
*
|
||||
#ident: #ty
|
||||
},
|
||||
ElementKind::Optional => quote! {
|
||||
#(#attrs)
|
||||
*
|
||||
#ident: ::std::option::Option<#ty>
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
@@ -202,7 +242,12 @@ fn build_output(input: Input) -> TokenStream {
|
||||
.iter()
|
||||
.map(|element| {
|
||||
let ident = &element.ident;
|
||||
quote! {#ident: #ident}
|
||||
let attrs = cfg_attrs(&element.attrs);
|
||||
quote! {
|
||||
#(#attrs)
|
||||
*
|
||||
#ident: #ident
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -210,6 +255,7 @@ fn build_output(input: Input) -> TokenStream {
|
||||
let ident = &element.ident;
|
||||
let ty = &element.ty;
|
||||
let tag_name = &element.name;
|
||||
let attrs = cfg_attrs(&element.attrs);
|
||||
|
||||
let try_convert = quote! {
|
||||
let value: ::std::result::Result<#ty, #root::tags::ParseTagsError> = <#ty as #root::tags::TagValue<#ty>>::from_raw_tag(value)
|
||||
@@ -261,6 +307,8 @@ fn build_output(input: Input) -> TokenStream {
|
||||
};
|
||||
|
||||
quote! {
|
||||
#(#attrs)
|
||||
*
|
||||
#ident: {
|
||||
let key: #root::tags::TagKey = #root::tags::TagKey::new(#tag_name.to_owned());
|
||||
|
||||
@@ -286,9 +334,12 @@ fn build_output(input: Input) -> TokenStream {
|
||||
let ident = &element.ident;
|
||||
let ty= &element.ty;
|
||||
let tag_name = &element.name;
|
||||
let attrs= &element.attrs;
|
||||
match element.kind {
|
||||
ElementKind::Required => {
|
||||
quote! {
|
||||
#(#attrs)
|
||||
*
|
||||
{
|
||||
let key = #root::tags::TagKey::new(#tag_name.to_owned());
|
||||
let value: #root::tags::RawTagValue = <#ty as #root::tags::TagValue<#ty>>::into_raw_tag(self.#ident);
|
||||
@@ -298,6 +349,8 @@ fn build_output(input: Input) -> TokenStream {
|
||||
}
|
||||
ElementKind::Optional => {
|
||||
quote! {
|
||||
#(#attrs)
|
||||
*
|
||||
{
|
||||
match self.#ident {
|
||||
::std::option::Option::Some(value) => {
|
||||
|
||||
71
src/lib.rs
71
src/lib.rs
@@ -11,6 +11,7 @@ use std::{
|
||||
use aws_config::retry::RetryConfig;
|
||||
use aws_sdk_ec2::client::Waiters;
|
||||
use chrono::{DateTime, Utc};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
mod error;
|
||||
@@ -40,6 +41,7 @@ macro_rules! wrap_aws_enum {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@@ -49,6 +51,7 @@ macro_rules! wrap_aws_enum {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl<'de> Deserialize<'de> for $name {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
@@ -196,11 +199,12 @@ impl Instance {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum Region {
|
||||
#[serde(rename = "eu-central-1")]
|
||||
#[cfg_attr(feature = "serde", serde(rename = "eu-central-1"))]
|
||||
EuCentral1,
|
||||
#[serde(rename = "us-east-1")]
|
||||
#[cfg_attr(feature = "serde", serde(rename = "us-east-1"))]
|
||||
UsEast1,
|
||||
}
|
||||
|
||||
@@ -269,7 +273,8 @@ pub struct RegionClient {
|
||||
pub cdn: RegionClientCdn,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InstanceProfileName(String);
|
||||
|
||||
impl InstanceProfileName {
|
||||
@@ -278,7 +283,8 @@ impl InstanceProfileName {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InstanceKeypairName(String);
|
||||
|
||||
impl InstanceKeypairName {
|
||||
@@ -287,7 +293,8 @@ impl InstanceKeypairName {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SecurityGroupId(String);
|
||||
|
||||
impl SecurityGroupId {
|
||||
@@ -296,12 +303,14 @@ impl SecurityGroupId {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SecurityGroup {
|
||||
id: SecurityGroupId,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SubnetId(String);
|
||||
|
||||
impl SubnetId {
|
||||
@@ -328,8 +337,9 @@ impl fmt::Display for SubnetId {
|
||||
|
||||
macro_rules! string_newtype {
|
||||
($name:ident) => {
|
||||
#[Tag(translate = serde)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
|
||||
#[Tag(translate = transparent)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct $name(String);
|
||||
|
||||
impl std::fmt::Display for $name {
|
||||
@@ -342,7 +352,8 @@ macro_rules! string_newtype {
|
||||
|
||||
string_newtype!(AvailabilityZone);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Subnet {
|
||||
pub id: SubnetId,
|
||||
pub availability_zone: AvailabilityZone,
|
||||
@@ -383,7 +394,8 @@ impl AmiId {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug)]
|
||||
pub struct Ami {
|
||||
pub id: AmiId,
|
||||
pub tags: TagList,
|
||||
@@ -411,7 +423,8 @@ impl TryFrom<aws_sdk_ec2::types::Image> for Ami {
|
||||
}
|
||||
|
||||
#[Tag(translate = manual)]
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
||||
pub struct Timestamp(DateTime<Utc>);
|
||||
|
||||
impl Timestamp {
|
||||
@@ -468,7 +481,8 @@ impl TryFrom<RawImageCreationDate> for Timestamp {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Ip(net::IpAddr);
|
||||
|
||||
impl Ip {
|
||||
@@ -499,7 +513,8 @@ impl EipAllocationId {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug)]
|
||||
pub struct Eip {
|
||||
pub allocation_id: EipAllocationId,
|
||||
pub ip: Ip,
|
||||
@@ -568,8 +583,9 @@ impl Eip {
|
||||
|
||||
string_newtype!(CloudfrontDistributionId);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
|
||||
pub enum CloudfrontDistributionStatus {
|
||||
Deployed,
|
||||
Other(String),
|
||||
@@ -597,10 +613,12 @@ impl From<String> for CloudfrontDistributionStatus {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EfsId(String);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Efs {
|
||||
id: EfsId,
|
||||
region: Region,
|
||||
@@ -633,7 +651,8 @@ impl From<String> for CloudfrontDistributionDomain {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CloudfrontOrigin {
|
||||
id: CloudfrontOriginId,
|
||||
domain: CloudfrontOriginDomain,
|
||||
@@ -679,7 +698,8 @@ impl From<aws_sdk_cloudfront::types::Origin> for CloudfrontOrigin {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CloudfrontDistribution {
|
||||
pub id: CloudfrontDistributionId,
|
||||
pub status: CloudfrontDistributionStatus,
|
||||
@@ -793,7 +813,8 @@ pub async fn load_sdk_clients<const C: usize>(
|
||||
region_clients
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Account {
|
||||
id: String,
|
||||
}
|
||||
@@ -808,7 +829,8 @@ impl Account {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HostedZoneId(String);
|
||||
|
||||
impl HostedZoneId {
|
||||
@@ -821,7 +843,8 @@ impl HostedZoneId {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Route53Zone {
|
||||
hosted_zone_id: HostedZoneId,
|
||||
name: String,
|
||||
|
||||
@@ -56,14 +56,10 @@ in a struct that is using `#[Tags]`:
|
||||
|
||||
```rust
|
||||
use aws_lib::tags::{Tag, Tags};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[Tag(translate = serde)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
struct MyTag {
|
||||
foo: String,
|
||||
bar: bool,
|
||||
}
|
||||
#[Tag(translate = transparent)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct MyTag(String);
|
||||
|
||||
#[Tags]
|
||||
struct MyTags {
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
#![doc = include_str!("README.md")]
|
||||
use std::fmt::{self, Debug};
|
||||
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
#[cfg(feature = "serde-tags")]
|
||||
use serde::de::DeserializeOwned;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::Deserialize;
|
||||
#[cfg(any(feature = "serde-tags", feature = "serde"))]
|
||||
use serde::Serialize;
|
||||
|
||||
mod error;
|
||||
mod helpers;
|
||||
@@ -43,10 +48,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
pub struct RawTagValue(String);
|
||||
helpers::impl_string_wrapper!(RawTagValue);
|
||||
|
||||
#[cfg(feature = "serde-tags")]
|
||||
pub struct TranslateSerde;
|
||||
pub struct TranslateManual;
|
||||
|
||||
@@ -57,6 +64,7 @@ pub trait Translator<S: ?Sized, T> {
|
||||
fn into_raw_tag(value: T) -> RawTagValue;
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-tags")]
|
||||
pub trait TranslatableSerde: Serialize + DeserializeOwned {}
|
||||
pub trait TranslatableManual:
|
||||
TryFrom<RawTagValue, Error: Into<ParseTagValueError>> + Into<RawTagValue>
|
||||
@@ -76,6 +84,7 @@ pub trait TagValue<V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-tags")]
|
||||
impl<S, T> Translator<S, T> for TranslateSerde
|
||||
where
|
||||
T: TranslatableSerde,
|
||||
@@ -118,7 +127,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct RawTag {
|
||||
key: TagKey,
|
||||
value: RawTagValue,
|
||||
@@ -155,7 +165,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub struct TagKey(String);
|
||||
helpers::impl_string_wrapper!(TagKey);
|
||||
|
||||
@@ -252,7 +263,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct TagList(Vec<RawTag>);
|
||||
|
||||
impl TagList {
|
||||
@@ -292,6 +304,7 @@ impl TagList {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[cfg(feature = "serde-tags")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::*;
|
||||
@@ -303,6 +316,7 @@ mod tests {
|
||||
B,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-tags")]
|
||||
#[Tag(translate = serde)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
struct MyStructTag {
|
||||
@@ -358,7 +372,9 @@ mod tests {
|
||||
#[tag(key = "anothername")]
|
||||
tag6: Option<MyTag>,
|
||||
tag7: Option<MyTag>,
|
||||
#[cfg(feature = "serde-tags")]
|
||||
tag8: MyStructTag,
|
||||
#[cfg(feature = "serde-tags")]
|
||||
tag9: Option<MyStructTag>,
|
||||
}
|
||||
|
||||
@@ -368,6 +384,7 @@ mod tests {
|
||||
RawTag::new("tag3".to_owned(), "false".to_owned()),
|
||||
RawTag::new("myname".to_owned(), "A".to_owned()),
|
||||
RawTag::new("anothername".to_owned(), "B".to_owned()),
|
||||
#[cfg(feature = "serde-tags")]
|
||||
RawTag::new("tag8".to_owned(), r#"{"foo":"hi","bar":false}"#.to_owned()),
|
||||
]);
|
||||
|
||||
@@ -380,6 +397,7 @@ mod tests {
|
||||
assert!(tags.tag5 == MyTag::A);
|
||||
assert!(tags.tag6 == Some(MyTag::B));
|
||||
assert!(tags.tag7.is_none());
|
||||
#[cfg(feature = "serde-tags")]
|
||||
assert!(
|
||||
tags.tag8
|
||||
== MyStructTag {
|
||||
@@ -387,6 +405,7 @@ mod tests {
|
||||
bar: false
|
||||
}
|
||||
);
|
||||
#[cfg(feature = "serde-tags")]
|
||||
assert!(tags.tag9.is_none());
|
||||
|
||||
let into_tags = tags.into_tags();
|
||||
@@ -399,6 +418,7 @@ mod tests {
|
||||
RawTag::new("tag3".to_owned(), "false".to_owned()),
|
||||
RawTag::new("myname".to_owned(), "A".to_owned()),
|
||||
RawTag::new("anothername".to_owned(), "B".to_owned()),
|
||||
#[cfg(feature = "serde-tags")]
|
||||
RawTag::new("tag8".to_owned(), r#"{"foo":"hi","bar":false}"#.to_owned(),),
|
||||
])
|
||||
);
|
||||
@@ -407,7 +427,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_transparent_tag() {
|
||||
#[Tag(translate = transparent)]
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct MyTag(String);
|
||||
|
||||
assert_eq!(
|
||||
|
||||
@@ -1,14 +1,35 @@
|
||||
use super::{
|
||||
ParseTagValueError, TagValue, TranslatableManual, TranslatableSerde, TranslateManual,
|
||||
TranslateSerde,
|
||||
};
|
||||
use super::{ParseTagValueError, RawTagValue, TagValue, TranslatableManual, TranslateManual};
|
||||
|
||||
// Bools can just be handled by serde.
|
||||
impl TranslatableSerde for bool {}
|
||||
impl TranslatableManual for bool {}
|
||||
|
||||
const TRUE_STR: &str = "true";
|
||||
const FALSE_STR: &str = "false";
|
||||
|
||||
impl TagValue<Self> for bool {
|
||||
type Error = ParseTagValueError;
|
||||
type Translator = TranslateSerde;
|
||||
type Translator = TranslateManual;
|
||||
}
|
||||
|
||||
impl TryFrom<RawTagValue> for bool {
|
||||
type Error = ParseTagValueError;
|
||||
|
||||
fn try_from(value: RawTagValue) -> Result<Self, Self::Error> {
|
||||
match value.as_str() {
|
||||
TRUE_STR => Ok(true),
|
||||
FALSE_STR => Ok(false),
|
||||
_ => Err(ParseTagValueError::InvalidBoolValue { value }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for RawTagValue {
|
||||
fn from(value: bool) -> Self {
|
||||
if value {
|
||||
Self::new(TRUE_STR.to_owned())
|
||||
} else {
|
||||
Self::new(FALSE_STR.to_owned())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Due to quoting, we cannot use serde here. It would produce quoted
|
||||
|
||||
Reference in New Issue
Block a user