-
Notifications
You must be signed in to change notification settings - Fork 98
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
Showing
3 changed files
with
58 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod row; | ||
pub use row::Row; |
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,43 @@ | ||
use quote::ToTokens; | ||
|
||
pub const ATTRIBUTE_NAME: &str = "row"; | ||
pub const ATTRIBUTE_SYNTAX: &str = "#[row(crate = ...)]"; | ||
|
||
pub const CRATE_PATH: &str = "crate"; | ||
pub const DEFAULT_CRATE_PATH: &str = "::clickhouse"; | ||
|
||
pub struct Row { | ||
pub crate_path: syn::Path, | ||
} | ||
|
||
impl Default for Row { | ||
fn default() -> Self { | ||
let default_crate_path = syn::parse_str::<syn::Path>(DEFAULT_CRATE_PATH).unwrap(); | ||
Self { | ||
crate_path: default_crate_path, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> TryFrom<&'a syn::Attribute> for Row { | ||
type Error = &'a syn::Attribute; | ||
|
||
fn try_from(attr: &'a syn::Attribute) -> Result<Self, Self::Error> { | ||
if attr.path().is_ident(ATTRIBUTE_NAME) { | ||
let row = attr.parse_args::<syn::Expr>().unwrap(); | ||
let syn::Expr::Assign(syn::ExprAssign { left, right, .. }) = row else { | ||
panic!("expected `{}`", ATTRIBUTE_SYNTAX); | ||
}; | ||
if left.to_token_stream().to_string() != CRATE_PATH { | ||
panic!("expected `{}`", ATTRIBUTE_SYNTAX); | ||
} | ||
let syn::Expr::Path(syn::ExprPath { path, .. }) = *right else { | ||
panic!("expected `{}`", ATTRIBUTE_SYNTAX); | ||
}; | ||
Ok(Self { crate_path: path }) | ||
} else { | ||
return Err(attr); | ||
} | ||
} | ||
} | ||
|
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