mirror of
https://github.com/Steffo99/micronfig.git
synced 2024-11-22 08:04:20 +00:00
Finish testing
This commit is contained in:
parent
8e4eedf91f
commit
e8e17647cb
17 changed files with 141 additions and 45 deletions
|
@ -29,8 +29,6 @@ enum Conversion {
|
|||
}
|
||||
|
||||
|
||||
const VALID_INITIAL_TYPES: [&'static str; 2] = ["String", "std::string::String"];
|
||||
|
||||
impl Parse for ConfigItem {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
let identifier = input.parse::<Ident>()?;
|
||||
|
@ -47,8 +45,13 @@ impl Parse for ConfigItem {
|
|||
.expect("this token to be parsed correctly, as it has been previously peeked");
|
||||
|
||||
let string_type = input.parse::<TypePath>()?;
|
||||
if !VALID_INITIAL_TYPES.contains(&&*string_type.to_token_stream().to_string()) {
|
||||
return Err(syn::Error::new_spanned(string_type, "first type of a conversion chain should always be `String` or `std::string::String`, type aliases are not allowed"))
|
||||
if &*string_type.to_token_stream().to_string() != "String" {
|
||||
return Err(
|
||||
syn::Error::new_spanned(
|
||||
string_type,
|
||||
"first type of a conversion chain should always be literally `String`, other aliases are not allowed"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
let mut types = Vec::new();
|
||||
|
@ -130,19 +133,17 @@ pub fn config(input: TokenStream) -> TokenStream {
|
|||
match (conversion, item.optional) {
|
||||
(Conversion::From, true) => quote! {
|
||||
let value: Option<#typ> = value
|
||||
.map(std::convert::Into::into);
|
||||
.map(|v| v.into());
|
||||
},
|
||||
(Conversion::TryFrom, true) => quote! {
|
||||
let value: Option<#typ> = value
|
||||
.map(std::convert::TryInto::try_into)
|
||||
.map(|v| v.expect(&format!("to be able to convert {}", #identifier_string))
|
||||
);
|
||||
.map(|v| v.try_into())
|
||||
.map(|v| v.expect(&format!("to be able to convert {}", #identifier_string)));
|
||||
},
|
||||
(Conversion::FromStr, true) => quote! {
|
||||
let value: Option<#typ> = value
|
||||
.map(str::parse)
|
||||
.map(|v| v.expect(&format!("to be able to parse {}", #identifier_string))
|
||||
);
|
||||
.map(|v| v.parse())
|
||||
.map(|v| v.expect(&format!("to be able to parse {}", #identifier_string)));
|
||||
},
|
||||
(Conversion::From, false) => quote! {
|
||||
let value: #typ = value
|
||||
|
@ -182,7 +183,7 @@ pub fn config(input: TokenStream) -> TokenStream {
|
|||
pub(crate) fn #identifier() -> &'static #type_final_option {
|
||||
#identifier::_lock.get_or_init(|| {
|
||||
let key: std::ffi::OsString = #identifier_string.into();
|
||||
let value: Option<String> = _cache().get(&key);
|
||||
let value: Option<std::string::String> = _cache().get(&key);
|
||||
|
||||
#require_code
|
||||
#conversion_code
|
||||
|
|
53
micronfig_macros/tests/sources/example_angybot.rs
Normal file
53
micronfig_macros/tests/sources/example_angybot.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use std::env;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct GuildId(u64);
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct UserId(u64);
|
||||
|
||||
impl From<u64> for GuildId {
|
||||
fn from(value: u64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for UserId {
|
||||
fn from(value: u64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
micronfig::config! {
|
||||
ANGY_TOKEN: String,
|
||||
ANGY_APPID: String > u64,
|
||||
ANGY_PLEX_SERVER: String,
|
||||
ANGY_PLEX_TOKEN: String,
|
||||
ANGY_PLEX_LIBRARY: String,
|
||||
ANGY_PLEX_REPLACE_FROM: String,
|
||||
ANGY_PLEX_REPLACE_TO: String,
|
||||
ANGY_DEV_GUILD_ID?: String > u64 -> crate::GuildId,
|
||||
ANGY_DEV_USER_ID?: String > u64 -> crate::UserId,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env::set_var("ANGY_TOKEN", "abcdef");
|
||||
env::set_var("ANGY_APPID", "1234");
|
||||
env::set_var("ANGY_PLEX_SERVER", "example.org");
|
||||
env::set_var("ANGY_PLEX_TOKEN", "123456dada");
|
||||
env::set_var("ANGY_PLEX_LIBRARY", "beta");
|
||||
env::set_var("ANGY_PLEX_REPLACE_FROM", "sus");
|
||||
env::set_var("ANGY_PLEX_REPLACE_TO", "sos");
|
||||
env::set_var("ANGY_DEV_GUILD_ID", "4567");
|
||||
env::set_var("ANGY_DEV_USER_ID", "5678");
|
||||
|
||||
assert_eq!(ANGY_TOKEN(), "abcdef");
|
||||
assert_eq!(ANGY_APPID(), &1234);
|
||||
assert_eq!(ANGY_PLEX_SERVER(), "example.org");
|
||||
assert_eq!(ANGY_PLEX_TOKEN(), "123456dada");
|
||||
assert_eq!(ANGY_PLEX_LIBRARY(), "beta");
|
||||
assert_eq!(ANGY_PLEX_REPLACE_FROM(), "sus");
|
||||
assert_eq!(ANGY_PLEX_REPLACE_TO(), "sos");
|
||||
assert_eq!(ANGY_DEV_GUILD_ID(), &Some(GuildId(4567)));
|
||||
assert_eq!(ANGY_DEV_USER_ID(), &Some(UserId(5678)));
|
||||
}
|
17
micronfig_macros/tests/sources/example_distributedarcade.rs
Normal file
17
micronfig_macros/tests/sources/example_distributedarcade.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr};
|
||||
|
||||
micronfig::config! {
|
||||
REDIS_CONN: String,
|
||||
AXUM_HOST: String > std::net::SocketAddr,
|
||||
CREATE_TOKEN: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
std::env::set_var("REDIS_CONN", "redis://garas");
|
||||
std::env::set_var("AXUM_HOST", "127.0.0.1:12345");
|
||||
std::env::set_var("CREATE_TOKEN", "tokennnnn");
|
||||
|
||||
assert_eq!(REDIS_CONN(), "redis://garas");
|
||||
assert_eq!(AXUM_HOST(), &SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 12345)), "127.0.0.1:12345");
|
||||
assert_eq!(CREATE_TOKEN(), "tokennnnn");
|
||||
}
|
|
@ -1,11 +1,13 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct CommaSeparatedStrings(Vec<String>);
|
||||
|
||||
impl FromStr for CommaSeparatedStrings {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Self(s.split(',').collect())
|
||||
Ok(Self(s.split(',').map(|v| v.to_string()).collect()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,8 +27,8 @@ fn main() {
|
|||
std::env::remove_var("SERENITY_DEV_GUILD_ID");
|
||||
|
||||
assert_eq!(DATA_DRAGON_LOCALE(), "it_IT");
|
||||
assert_eq!(DATA_DRAGON_SET_CODES(), CommaSeparatedStrings(vec!["set1", "set2abc"]));
|
||||
assert_eq!(POROXY_KEY, "abcdef");
|
||||
assert_eq!(POROXY_SALT, "abcdef");
|
||||
assert_eq!(SERENITY_DEV_GUILD_ID, &None);
|
||||
assert_eq!(DATA_DRAGON_SET_CODES(), &CommaSeparatedStrings(vec!["set1".to_string(), "set2abc".to_string()]));
|
||||
assert_eq!(POROXY_KEY(), "abcdef");
|
||||
assert_eq!(POROXY_SALT(), "abcdef");
|
||||
assert_eq!(SERENITY_DEV_GUILD_ID(), &None);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[derive(std::fmt::Debug, Eq)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct MyCustomStruct(String);
|
||||
|
||||
impl From<String> for MyCustomStruct {
|
||||
|
@ -13,5 +13,5 @@ micronfig::config! {
|
|||
|
||||
fn main() {
|
||||
std::env::set_var("GARASAUTO", "baba");
|
||||
assert_eq!(GARASAUTO(), &MyCustomStruct("baba"));
|
||||
assert_eq!(GARASAUTO(), &MyCustomStruct("baba".to_string()));
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[derive(std::fmt::Debug, Eq)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct MyCustomStruct(String);
|
||||
|
||||
impl std::str::FromStr for MyCustomStruct {
|
||||
|
@ -15,5 +15,5 @@ micronfig::config! {
|
|||
|
||||
fn main() {
|
||||
std::env::set_var("GARASAUTO", "keke");
|
||||
assert_eq!(GARASAUTO(), &MyCustomStruct("keke"));
|
||||
assert_eq!(GARASAUTO(), &MyCustomStruct("keke".to_string()));
|
||||
}
|
||||
|
|
|
@ -4,5 +4,5 @@ micronfig::config! {
|
|||
|
||||
fn main() {
|
||||
std::env::set_var("GARASAUTO", "./garas");
|
||||
assert_eq!(GARASAUTO(), std::path::PathBuf::from("./garas"));
|
||||
assert_eq!(GARASAUTO(), &std::path::PathBuf::from("./garas"));
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO?: String > u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
std::env::remove_var("GARASAUTO");
|
||||
assert_eq!(GARASAUTO(), &None);
|
||||
}
|
14
micronfig_macros/tests/sources/string_multi_explicit.rs
Normal file
14
micronfig_macros/tests/sources/string_multi_explicit.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
micronfig::config! {
|
||||
GARAS: String,
|
||||
AUTO: String,
|
||||
BUS: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
std::env::set_var("GARAS", "garas");
|
||||
std::env::set_var("AUTO", "auto");
|
||||
std::env::set_var("BUS", "bus");
|
||||
assert_eq!(GARAS(), "garas");
|
||||
assert_eq!(AUTO(), "auto");
|
||||
assert_eq!(BUS(), "bus");
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
#[derive(std::fmt::Debug, Eq)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct MyCustomStruct(String);
|
||||
|
||||
impl std::convert::TryFrom<String> for MyCustomStruct {
|
||||
|
@ -15,5 +15,5 @@ micronfig::config! {
|
|||
|
||||
fn main() {
|
||||
std::env::set_var("GARASAUTO", "me");
|
||||
assert_eq!(GARASAUTO(), &MyCustomStruct("me"));
|
||||
assert_eq!(GARASAUTO(), &MyCustomStruct("me".to_string()));
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ error: expected `,`
|
|||
| ^
|
||||
|
||||
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
|
||||
--> tests/sources/wrong_conversion_longfatarrow.rs:7:19
|
||||
--> tests/sources/wrong_conversion_longfatarrow.rs:7:20
|
||||
|
|
||||
7 | println!("{:?}", GARASAUTO())
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
7 | println!("{:#?}", GARASAUTO());
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
|
|
|
@ -5,7 +5,7 @@ error: expected `,`
|
|||
| ^
|
||||
|
||||
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
|
||||
--> tests/sources/wrong_conversion_longthinarrow.rs:7:19
|
||||
--> tests/sources/wrong_conversion_longthinarrow.rs:7:20
|
||||
|
|
||||
7 | println!("{:?}", GARASAUTO())
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
7 | println!("{:#?}", GARASAUTO());
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
|
|
|
@ -5,7 +5,7 @@ error: expected `,`
|
|||
| ^
|
||||
|
||||
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
|
||||
--> tests/sources/wrong_conversion_tildearrow.rs:7:19
|
||||
--> tests/sources/wrong_conversion_tildearrow.rs:7:20
|
||||
|
|
||||
7 | println!("{:?}", GARASAUTO())
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
7 | println!("{:#?}", GARASAUTO());
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
error: first type of a conversion chain should always be `String` or `std::string::String`, type aliases are not allowed
|
||||
error: first type of a conversion chain should always be literally `String`, other aliases are not allowed
|
||||
--> tests/sources/wrong_start.rs:2:13
|
||||
|
|
||||
2 | GARASAUTO: i64,
|
||||
| ^^^
|
||||
|
||||
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
|
||||
--> tests/sources/wrong_start.rs:7:19
|
||||
--> tests/sources/wrong_start.rs:7:20
|
||||
|
|
||||
7 | println!("{:?}", GARASAUTO())
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
7 | println!("{:#?}", GARASAUTO());
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
|
|
|
@ -5,7 +5,7 @@ error: expected identifier
|
|||
| ^
|
||||
|
||||
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
|
||||
--> tests/sources/wrong_syntax_colon.rs:7:19
|
||||
--> tests/sources/wrong_syntax_colon.rs:7:20
|
||||
|
|
||||
7 | println!("{:?}", GARASAUTO())
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
7 | println!("{:#?}", GARASAUTO());
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
|
|
|
@ -5,7 +5,7 @@ error: expected `,`
|
|||
| ^^^^^^
|
||||
|
||||
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
|
||||
--> tests/sources/wrong_syntax_type.rs:7:19
|
||||
--> tests/sources/wrong_syntax_type.rs:7:20
|
||||
|
|
||||
7 | println!("{:?}", GARASAUTO())
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
7 | println!("{:#?}", GARASAUTO());
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
|
|
|
@ -19,6 +19,8 @@ macro_rules! fail {
|
|||
pass!(chain_single_down);
|
||||
pass!(chain_single_up);
|
||||
pass!(empty);
|
||||
pass!(example_angybot);
|
||||
pass!(example_distributedarcade);
|
||||
pass!(example_patchedporobot);
|
||||
pass!(from_single_custom);
|
||||
pass!(parse_single_custom);
|
||||
|
@ -30,7 +32,6 @@ pass!(string_multi_explicit);
|
|||
pass!(string_multi_implicit);
|
||||
pass!(string_multi_mixed);
|
||||
pass!(string_single_explicit);
|
||||
pass!(string_single_expliciter);
|
||||
pass!(string_single_implicit);
|
||||
pass!(tryfrom_single_custom);
|
||||
|
||||
|
|
Loading…
Reference in a new issue