<commit_before>// Zinc, the bare metal stack for rust.
// Copyright 2014 Vladimir "farcaller" Pouzanov <<EMAIL>>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::rc::Rc;
use syntax::ext::base::ExtCtxt;

use builder::{Builder, TokenString, add_node_dependency};
use node;

pub fn attach(builder: &mut Builder, _: &mut ExtCtxt, node: Rc<node::Node>) {
  node.materializer.set(Some(build_dht22));
  node.mutator.set(Some(mutate_pin));

  let pin_node_name = node.get_ref_attr("pin").unwrap();
  let pin_node = builder.pt.get_by_name(pin_node_name.as_slice()).unwrap();
  add_node_dependency(&node, &pin_node);

  let timer_node_name = node.get_ref_attr("timer").unwrap();
  let timer_node = builder.pt.get_by_name(timer_node_name.as_slice()).unwrap();
  add_node_dependency(&node, &timer_node);
}

fn mutate_pin(builder: &mut Builder, _: &mut ExtCtxt, node: Rc<node::Node>) {
  let pin_node_name = node.get_ref_attr("pin").unwrap();
  let pin_node = builder.pt.get_by_name(pin_node_name.as_slice()).unwrap();
  pin_node.attributes.borrow_mut().insert("direction".to_str(),
        Rc::new(node::Attribute::new_nosp(node::StrValue("out".to_str()))));
}

fn build_dht22(builder: &mut Builder, cx: &mut ExtCtxt, node: Rc<node::Node>) {
  if !node.expect_no_subnodes(cx) {return}

  if !node.expect_attributes(cx,
      [("pin", node::RefAttribute), ("timer", node::RefAttribute)]) {
    return
  }

  let pin_node_name = node.get_ref_attr("pin").unwrap();
  let timer_node_name = node.get_ref_attr("timer").unwrap();
  let pin_node = builder.pt.get_by_name(pin_node_name.as_slice()).unwrap();
  let timer_node = builder.pt.get_by_name(timer_node_name.as_slice()).unwrap();

  let pin = TokenString(pin_node_name);
  let timer = TokenString(timer_node_name);
  let name = TokenString(node.name.clone().unwrap());

  let typename = format!(
      "zinc::drivers::dht22::DHT22<'a, {}, {}>",
      timer_node.type_name().unwrap(),
      pin_node.type_name().unwrap());
  node.set_type_name(typename);

  let st = quote_stmt!(&*cx,
      let $name = zinc::drivers::dht22::DHT22::new(&$timer, &$pin);
  );
  builder.add_main_statement(st);
}

#[cfg(test)]
mod test {
  use builder::Builder;
  use test_helpers::{assert_equal_source, with_parsed};

  #[test]
  fn builds_lpc17xx_pt() {
    with_parsed("
      timer@timer;
      pin@pin;
      dht@dht22 {
        pin = &pin;
        timer = &timer;
      }", |cx, failed, pt| {
      let mut builder = Builder::new(pt.clone());
      super::mutate_pin(&mut builder, cx, pt.get_by_name("dht").unwrap());
      super::build_dht22(&mut builder, cx, pt.get_by_name("dht").unwrap());
      assert!(unsafe{*failed} == false);
      assert!(builder.main_stmts.len() == 1);

      assert_equal_source(builder.main_stmts.get(0),
          "let dht = zinc::drivers::dht22::DHT22::new(&timer, &pin);");

      let pin_node = pt.get_by_name("pin").unwrap();
      assert!(pin_node.get_string_attr("direction").unwrap() == "out".to_str());
    });
  }
}<commit_msg>dht22pt kind of works<commit_after>// Zinc, the bare metal stack for rust.
// Copyright 2014 Vladimir "farcaller" Pouzanov <<EMAIL>>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::rc::Rc;
use syntax::ext::base::ExtCtxt;

use builder::{Builder, TokenString, add_node_dependency};
use node;

pub fn attach(builder: &mut Builder, _: &mut ExtCtxt, node: Rc<node::Node>) {
  node.materializer.set(Some(build_dht22));
  node.mutator.set(Some(mutate_pin));

  let pin_node_name = node.get_ref_attr("pin").unwrap();
  let pin_node = builder.pt.get_by_name(pin_node_name.as_slice()).unwrap();
  add_node_dependency(&node, &pin_node);

  let timer_node_name = node.get_ref_attr("timer").unwrap();
  let timer_node = builder.pt.get_by_name(timer_node_name.as_slice()).unwrap();
  add_node_dependency(&node, &timer_node);
}

fn mutate_pin(builder: &mut Builder, _: &mut ExtCtxt, node: Rc<node::Node>) {
  let pin_node_name = node.get_ref_attr("pin").unwrap();
  let pin_node = builder.pt.get_by_name(pin_node_name.as_slice()).unwrap();
  pin_node.attributes.borrow_mut().insert("direction".to_str(),
        Rc::new(node::Attribute::new_nosp(node::StrValue("out".to_str()))));
}

fn build_dht22(builder: &mut Builder, cx: &mut ExtCtxt, node: Rc<node::Node>) {
  if !node.expect_no_subnodes(cx) {return}

  if !node.expect_attributes(cx,
      [("pin", node::RefAttribute), ("timer", node::RefAttribute)]) {
    return
  }

  let pin_node_name = node.get_ref_attr("pin").unwrap();
  let timer_node_name = node.get_ref_attr("timer").unwrap();
  let pin_node = builder.pt.get_by_name(pin_node_name.as_slice()).unwrap();
  let timer_node = builder.pt.get_by_name(timer_node_name.as_slice()).unwrap();

  let pin = TokenString(pin_node_name);
  let timer = TokenString(timer_node_name);
  let name = TokenString(node.name.clone().unwrap());

  let typename = format!(
      "zinc::drivers::dht22::DHT22<'a, {}, {}>",
      timer_node.type_name().unwrap(),
      pin_node.type_name().unwrap());
  node.set_type_name(typename);

  let st = quote_stmt!(&*cx,
      let $name = zinc::drivers::dht22::DHT22::new(&$timer, &$pin);
  );
  builder.add_main_statement(st);
}

#[cfg(test)]
mod test {
  use builder::Builder;
  use test_helpers::{assert_equal_source, with_parsed, equal_to, equal_to_s};
  use hamcrest::{assert_that,is};

  #[test]
  fn builds_lpc17xx_pt() {
    with_parsed("
      timer@timer;
      pin@pin;
      dht@dht22 {
        pin = &pin;
        timer = &timer;
      }", |cx, failed, pt| {
      let mut builder = Builder::new(pt.clone());
      pt.get_by_name("timer").unwrap().set_type_name("T".to_str());
      pt.get_by_name("pin").unwrap().set_type_name("P".to_str());
      super::mutate_pin(&mut builder, cx, pt.get_by_name("dht").unwrap());
      super::build_dht22(&mut builder, cx, pt.get_by_name("dht").unwrap());
      assert_that(unsafe{*failed}, is(equal_to(false)));
      assert_that(builder.main_stmts.len(), is(equal_to(1u)));

      assert_equal_source(builder.main_stmts.get(0),
          "let dht = zinc::drivers::dht22::DHT22::new(&timer, &pin);");

      let pin_node = pt.get_by_name("pin").unwrap();
      assert_that(pin_node.get_string_attr("direction").unwrap(),
          is(equal_to_s("out")));

      assert_that(pt.get_by_name("dht").unwrap().type_name().unwrap(),
          is(equal_to_s("zinc::drivers::dht22::DHT22<'a, T, P>")));
    });
  }
}
{'fraction_non_alphanumeric': 0.16684665226781858, 'fraction_numerical': 0.009449244060475162, 'mean_word_length': 4.516753536857781, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5684900', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2613, 'n_words': 509}