Skip to content

Commit

Permalink
feat: Ability to register variables on a builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
andoriyu committed Mar 16, 2020
1 parent c768f46 commit a2f3ede
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions uclicious_derive/src/builder.rs
Expand Up @@ -156,7 +156,6 @@ impl< 'a > ToTokens for BuildMethod < 'a > {
let vis = &self.visibility;
let target_ty = &self.target_ty;
let target_ty_generics = &self.target_ty_generics;
let doc_comment = &self.doc_comment;
let default_struct = self.default_struct.as_ref().map(|default_expr| {
let ident = syn::Ident::new(DEFAULT_STRUCT_NAME, Span::call_site());
quote!(let #ident: #target_ty #target_ty_generics = #default_expr;)
Expand All @@ -167,7 +166,7 @@ impl< 'a > ToTokens for BuildMethod < 'a > {
let ucl_obj_error_ty = bindings::ucl_object_error();
let from_obj = bindings::from_object_trait();
tokens.append_all(quote!(
#doc_comment
#[doc = "Build target struct or return first encountered error."]
#vis fn #ident(mut self) -> #result<#target_ty #target_ty_generics, #boxed_error> {
#default_struct
let root = self.__parser.get_object().map_err(|e: #ucl_error_ty| e.boxed() as #boxed_error)?;
Expand Down Expand Up @@ -209,7 +208,7 @@ impl< 'a > ToTokens for Builder < 'a > {
#[allow(dead_code)]
impl #impl_generics #builder_ident #ty_generics #where_clause {
#(#functions)*

/// Create a new builder.
#builder_vis fn new() -> #result_ty<Self #ty_generics #where_clause, #ucl_error_ty> {
#parser
#(#vars)*
Expand Down Expand Up @@ -240,6 +239,7 @@ impl<'a> ToTokens for IntoBuilder<'a> {
.unwrap_or((None, None, None));
tokens.append_all(quote!(
impl #target {
/// Creates a builder struct that can be used to create this struct.
#builder_vis fn builder() -> #result_ty<#builder_ident #ty_generics #where_clause, #ucl_error_ty> {
#builder_ident::new()
}
Expand Down
17 changes: 17 additions & 0 deletions uclicious_derive/src/parser.rs
Expand Up @@ -38,12 +38,29 @@ impl ToTokens for ParserMethods {
let err = bindings::ucl_parser_error();
let path = bindings::path_ty();
tokens.append_all(quote! (
/// Add a chunk of text to the parser. String must:
/// - not have `\0` character;
/// - must be valid UCL object;
#vis fn add_chunk_full<C: #as_ref<str>>(&mut self, chunk: C, priority: #priority, strategy: #dup_strategy) -> #result<(), #err> {
self.__parser.add_chunk_full(chunk, priority, strategy)
}
/// Add a file by a file path to the parser. This function uses mmap call to load file, therefore, it should not be shrunk during parsing.
#vis fn add_file_full<F: #as_ref<#path>>(&mut self, file: F, priority: #priority, strategy: #dup_strategy) -> #result<(), #err> {
self.__parser.add_file_full(file, priority, strategy)
}
/// Register new variable `$var` that should be replaced by the parser to the `value` string.
/// Variables need to be registered _before_ they are referenced.
///
/// #### Panics
/// This function panics if either `var` or `value` has `\0`.
#vis fn register_variable<K: #as_ref<str>, V: #as_ref<str>>(
&mut self,
var: K,
value: V,
) -> &mut Self {
self.__parser.register_variable(var, value);
self
}
))
}
}

0 comments on commit a2f3ede

Please sign in to comment.