Skip to content

Commit

Permalink
Adding universal attributes to all components (excluding term) (#379)
Browse files Browse the repository at this point in the history
* added universal arguments to all components

* added check on redundant declaration of universal arguments

* fixed all tests after adding universal arguments

* added a helper func to get a optional default value from kind

* commented out the redundant declarations of id in components

* example file added
  • Loading branch information
Heulitig committed Aug 30, 2022
1 parent 1a28285 commit 9dff90d
Show file tree
Hide file tree
Showing 5 changed files with 774 additions and 407 deletions.
15 changes: 15 additions & 0 deletions examples/universal-attributes.ftd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; This previously worked when the arguments are locally defined
-- ftd.text foo: $c
optional string c:
optional integer d:

;; This works
-- foo:
c: xyz

;; This works now since id is now a universal argument
-- ftd.text t1: $id

;; This works now (shows term and id)
-- t1:
id: some-id
31 changes: 21 additions & 10 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,14 +1704,17 @@ impl Component {
let name = var_data.name;
let root = doc.resolve_name(p1.line_number, var_data.kind.as_str())?;
let root_component = doc.get_component(p1.line_number, root.as_str())?;
let (arguments, inherits) = read_arguments(
let (mut arguments, inherits) = read_arguments(
&p1.header,
root.as_str(),
&root_component.arguments,
&Default::default(),
doc,
)?;

// Extend the local arguments with universal arguments
arguments.extend(universal_arguments());

assert_no_extra_properties(
p1.line_number,
&p1.header,
Expand Down Expand Up @@ -2434,14 +2437,6 @@ pub fn read_properties(
is_reference: bool,
) -> ftd::p1::Result<ftd::Map<Property>> {
let mut properties: ftd::Map<Property> = Default::default();
let root_arguments = {
let mut root_arguments = root_arguments.clone();
let universal_argument = universal_arguments();
for (key, arg) in universal_argument {
root_arguments.entry(key).or_insert(arg);
}
root_arguments
};

for (name, kind) in root_arguments.iter() {
if let Some(prop) = root_properties.get(name) {
Expand Down Expand Up @@ -2549,7 +2544,7 @@ pub fn read_properties(
value.as_str(),
Some(kind.to_owned()),
doc,
&root_arguments,
root_arguments,
Some(source.clone()),
)?,
Err(e) => return Err(e),
Expand Down Expand Up @@ -3083,6 +3078,10 @@ fn read_arguments(
// contains parent arguments and current arguments
let mut all_args = arguments.clone();

// Set of all universal arguments available to all components
let universal_arguments_set: std::collections::HashSet<String> =
universal_arguments().keys().cloned().collect();

// Set of root arguments which are invoked once
let mut root_args_set: std::collections::HashSet<String> = std::collections::HashSet::new();
for (idx, (i, k, v)) in p1.0.iter().enumerate() {
Expand Down Expand Up @@ -3184,6 +3183,18 @@ fn read_arguments(
});
}

// checking if any universal argument is declared by the user (forbidden)
if universal_arguments_set.contains(&var_data.name) {
return Err(ftd::p1::Error::ForbiddenUsage {
message: format!(
"redundant declaration of universal argument \'{}\' !!",
&var_data.name
),
doc_id: doc.name.to_string(),
line_number: *i,
});
}

args.insert(var_data.name.to_string(), kind.clone());
all_args.insert(var_data.name.to_string(), kind);
}
Expand Down
Loading

0 comments on commit 9dff90d

Please sign in to comment.