-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba889de
commit ccdae59
Showing
28 changed files
with
292 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use quick_impl::QuickImpl; | ||
|
||
#[derive(QuickImpl, Default)] | ||
struct Config { | ||
#[quick_impl(pub get, pub set, take, pub with)] | ||
host: String, | ||
|
||
#[quick_impl(pub get, pub set, take, pub with)] | ||
port: u16, | ||
|
||
#[quick_impl(pub get, pub set, take, pub with)] | ||
max_connections: usize, | ||
} | ||
|
||
fn main() { | ||
// Using the `Default` trait to create a Config instance | ||
let mut config = Config::default(); | ||
|
||
// Setting values | ||
config.set_host("localhost".to_string()); | ||
config.set_port(8080); | ||
config.set_max_connections(100); | ||
|
||
assert_eq!(config.get_host(), "localhost"); | ||
assert_eq!(*config.get_port(), 8080); | ||
assert_eq!(*config.get_max_connections(), 100); | ||
|
||
// Using `take` to retrieve and reset fields | ||
let host = config.take_host(); | ||
assert_eq!(host, "localhost"); | ||
assert_eq!(config.get_host(), ""); // After `take`, it should be default (empty String) | ||
|
||
let port = config.take_port(); | ||
assert_eq!(port, 8080); | ||
assert_eq!(*config.get_port(), 0); // After `take`, it should be default (0) | ||
|
||
// Using `with` to modify fields fluently | ||
let new_config = config.with_host("127.0.0.1".to_string()).with_port(3000); | ||
assert_eq!(new_config.get_host(), "127.0.0.1"); | ||
assert_eq!(*new_config.get_port(), 3000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use quick_impl::QuickImpl; | ||
|
||
#[derive(Debug, QuickImpl)] | ||
pub enum Status { | ||
#[quick_impl(pub const is)] | ||
Pending, | ||
|
||
#[quick_impl(pub const is, pub try_into, impl TryInto)] | ||
Approved(i32), | ||
|
||
#[quick_impl(pub const is, pub try_into, impl TryInto)] | ||
Rejected(String), | ||
} | ||
|
||
#[derive(QuickImpl)] | ||
pub struct Wrapper { | ||
#[quick_impl(impl Deref, impl DerefMut)] | ||
data: Vec<u8>, | ||
} | ||
|
||
fn main() { | ||
// Enum example with `try_into` and `is` | ||
let status = Status::Approved(200); | ||
assert!(status.is_approved()); | ||
|
||
// Use `try_into` to convert to the associated data | ||
let approved_code: Result<i32, Status> = status.try_into_approved(); | ||
assert_eq!(approved_code.unwrap(), 200); | ||
|
||
let rejected_status = Status::Rejected("Invalid request".to_string()); | ||
assert!(rejected_status.is_rejected()); | ||
|
||
let rejection_reason: Result<String, Status> = rejected_status.try_into(); | ||
assert_eq!(rejection_reason.unwrap(), "Invalid request"); | ||
|
||
// Struct example with `Deref` and `DerefMut` | ||
let mut wrapper = Wrapper { | ||
data: vec![1, 2, 3], | ||
}; | ||
|
||
// Accessing inner `Vec<u8>` through Deref | ||
assert_eq!(wrapper.len(), 3); // Calls `Vec<u8>::len` | ||
|
||
// Mutating inner `Vec<u8>` through DerefMut | ||
wrapper.push(4); | ||
assert_eq!(wrapper.len(), 4); // Now has 4 elements | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use proc_macro2::{Delimiter, TokenStream}; | ||
use quote::quote; | ||
use syn::Variant; | ||
|
||
use crate::attributes::{Attribute, MethodAttribute}; | ||
use crate::config::{build_config, build_enum_doc, build_enum_name}; | ||
use crate::expand::Context; | ||
use crate::tokens::{destructure_data, destructure_data_with_types, get_delimiter}; | ||
|
||
build_enum_name! { ConfigName, "set_{}" } | ||
build_enum_doc! { | ||
ConfigDoc, | ||
"Replaces the current instance with a new instance of the [`{}::{}`] variant, returning the original instance.", | ||
} | ||
|
||
build_config! { | ||
Config, | ||
(name, ConfigName, true), | ||
(doc, ConfigDoc, false), | ||
} | ||
|
||
pub fn enum_method_set( | ||
context: &Context, | ||
variant: &Variant, | ||
attribute: &Attribute, | ||
method_attr: &MethodAttribute, | ||
) -> syn::Result<TokenStream> { | ||
let config = Config::new(context, attribute, variant)?; | ||
|
||
let fields = &variant.fields; | ||
let delimiter = get_delimiter(fields); | ||
|
||
let input = destructure_data_with_types(fields, quote! {}, Delimiter::None, false); | ||
let destruct = destructure_data(fields, quote! {}, quote! {}, delimiter, true); | ||
|
||
let variant_ident = &variant.ident; | ||
let keywords = method_attr.keywords(); | ||
let doc = config.doc; | ||
let method_ident = config.name; | ||
|
||
Ok(quote! { | ||
#[doc = #doc] | ||
#keywords fn #method_ident(&mut self, #input) -> Self { | ||
::core::mem::replace(self, Self:: #variant_ident #destruct ) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.