<commit_before>// Copyright 2013-2015, The Rust-GNOME Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

//! A button to launch a color selection dialog

use glib::translate::*;
use glib::types;
use ffi;
use gdk;
use gdk_ffi;

use object::{Object, Downcast, Upcast};
use super::widget::Widget;

/**
* ColorButton — A button to launch a color selection dialog
*/
pub type ColorButton = Object<ffi::GtkColorButton>;

unsafe impl Upcast<Widget> for ColorButton { }
unsafe impl Upcast<super::container::Container> for ColorButton { }
unsafe impl Upcast<super::bin::Bin> for ColorButton { }
unsafe impl Upcast<super::button::Button> for ColorButton { }

unsafe impl Upcast<super::actionable::Actionable> for ColorButton { }
unsafe impl Upcast<::builder::Buildable> for ColorButton { }
unsafe impl Upcast<::chooser::color::ColorChooser> for ColorButton {}

impl ColorButton {
    /// Creates a new color button.
    /// This returns a widget in the form of a small button
    /// containing a swatch representing the current selected color.
    /// When the button is clicked, a color-selection dialog will open,
    /// allowing the user to select a color.
    /// The swatch will be updated to reflect the new color when the user finishes.
    pub fn new() -> ColorButton {
        unsafe {
            Widget::from_glib_none(ffi::gtk_color_button_new()).downcast_unchecked()
        }
    }
    /// Creates a new color button.
    pub fn new_with_rgba(rgba: &gdk_ffi::GdkRGBA) -> ColorButton {
        unsafe {
            Widget::from_glib_none(ffi::gtk_color_button_new_with_rgba(rgba))
                .downcast_unchecked()
        }
    }
    /// Sets the title for the color selection dialog.
    pub fn set_title(&self, title: &str) {
        unsafe {
            ffi::gtk_color_button_set_title(self.to_glib_none().0, title.to_glib_none().0);
        }
    }
    /// Gets the title of the color selection dialog.
    pub fn get_title(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::gtk_color_button_get_title(self.to_glib_none().0))
        }
    }
}

impl types::StaticType for ColorButton {
    #[inline]
    fn static_type() -> types::Type {
        unsafe { from_glib(ffi::gtk_color_button_get_type()) }
    }
}
<commit_msg>Remove unused `use gdk` in ColorButton.<commit_after>// Copyright 2013-2015, The Rust-GNOME Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

//! A button to launch a color selection dialog

use glib::translate::*;
use glib::types;
use ffi;
use gdk_ffi;

use object::{Object, Downcast, Upcast};
use super::widget::Widget;

/**
* ColorButton — A button to launch a color selection dialog
*/
pub type ColorButton = Object<ffi::GtkColorButton>;

unsafe impl Upcast<Widget> for ColorButton { }
unsafe impl Upcast<super::container::Container> for ColorButton { }
unsafe impl Upcast<super::bin::Bin> for ColorButton { }
unsafe impl Upcast<super::button::Button> for ColorButton { }

unsafe impl Upcast<super::actionable::Actionable> for ColorButton { }
unsafe impl Upcast<::builder::Buildable> for ColorButton { }
unsafe impl Upcast<::chooser::color::ColorChooser> for ColorButton {}

impl ColorButton {
    /// Creates a new color button.
    /// This returns a widget in the form of a small button
    /// containing a swatch representing the current selected color.
    /// When the button is clicked, a color-selection dialog will open,
    /// allowing the user to select a color.
    /// The swatch will be updated to reflect the new color when the user finishes.
    pub fn new() -> ColorButton {
        unsafe {
            Widget::from_glib_none(ffi::gtk_color_button_new()).downcast_unchecked()
        }
    }
    /// Creates a new color button.
    pub fn new_with_rgba(rgba: &gdk_ffi::GdkRGBA) -> ColorButton {
        unsafe {
            Widget::from_glib_none(ffi::gtk_color_button_new_with_rgba(rgba))
                .downcast_unchecked()
        }
    }
    /// Sets the title for the color selection dialog.
    pub fn set_title(&self, title: &str) {
        unsafe {
            ffi::gtk_color_button_set_title(self.to_glib_none().0, title.to_glib_none().0);
        }
    }
    /// Gets the title of the color selection dialog.
    pub fn get_title(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::gtk_color_button_get_title(self.to_glib_none().0))
        }
    }
}

impl types::StaticType for ColorButton {
    #[inline]
    fn static_type() -> types::Type {
        unsafe { from_glib(ffi::gtk_color_button_get_type()) }
    }
}
<|endoftext|>


<commit_before>use std::libc::*;
use std::kinds::marker::NoSend;

use internal::bitmap_like::*;
use internal::color::*;

use ffi::*;

pub mod external
{
	pub use super::Bitmap;
	pub use super::SubBitmap;
}

pub struct Bitmap
{
	allegro_bitmap: *mut ALLEGRO_BITMAP,
	is_ref: bool,
	no_send_marker: NoSend
}

impl Bitmap
{
	fn new(w: i32, h: i32) -> Option<Bitmap>
	{
		unsafe
		{
			let b =	al_create_bitmap(w as c_int, h as c_int);
			if b.is_null()
			{
				None
			}
			else
			{
				Some(Bitmap{ allegro_bitmap: b, is_ref: false, no_send_marker: NoSend })
			}
		}
	}

	fn load(filename: &str) -> Option<Bitmap>
	{
		let b = filename.with_c_str(|s|
		{
			unsafe
			{
				al_load_bitmap(s)
			}
		});
		if b.is_null()
		{
			None
		}
		else
		{
			Some(Bitmap{ allegro_bitmap: b, is_ref: false, no_send_marker: NoSend })
		}
	}

	pub fn create_sub_bitmap<'l>(&'l self, x: i32, y: i32, w: i32, h: i32) -> Option<SubBitmap<'l>>
	{
		unsafe
		{
			let b = al_create_sub_bitmap(self.allegro_bitmap, x as c_int, y as c_int, w as c_int, h as c_int);
			if b.is_null()
			{
				None
			}
			else
			{
				Some(SubBitmap{ allegro_bitmap: b, parent: self })
			}
		}
	}

	pub fn maybe_clone(&self) -> Option<Bitmap>
	{
		clone_bitmap(self.allegro_bitmap)
	}
}

impl BitmapLike for Bitmap
{
	fn get_bitmap(&self) -> *mut ALLEGRO_BITMAP
	{
		self.allegro_bitmap
	}
}

impl Clone for Bitmap
{
	fn clone(&self) -> Bitmap
	{
		self.maybe_clone().unwrap()
	}
}

// Not Send just because of the marker
#[unsafe_destructor]
impl Drop for Bitmap
{
	fn drop(&mut self)
	{
		if !self.is_ref
		{
			unsafe
			{
				use internal::core::dummy_target;

				/* If this bitmap is the target or the parent of the target
				 * then the target becomes invalid. Set it to the dummy target. */
				let target = al_get_target_bitmap();
				if target.is_null()
				{
					fail!("Null target bitmap!");
				}
				if target == self.allegro_bitmap || al_get_parent_bitmap(target) == self.allegro_bitmap
				{
					al_set_target_bitmap(dummy_target);
				}
				al_destroy_bitmap(self.allegro_bitmap);
			}
		}
	}
}

pub struct SubBitmap<'m>
{
	allegro_bitmap: *mut ALLEGRO_BITMAP,
	parent: &'m Bitmap
}

impl<'m> SubBitmap<'m>
{
	pub fn create_sub_bitmap<'l>(&'l self, x: i32, y: i32, w: i32, h: i32) -> Option<SubBitmap<'l>>
	{
		unsafe
		{
			let b = al_create_sub_bitmap(self.allegro_bitmap, x as c_int, y as c_int, w as c_int, h as c_int);
			if b.is_null()
			{
				None
			}
			else
			{
				Some(SubBitmap{ allegro_bitmap: b, parent: self.parent })
			}
		}
	}

	pub fn get_parent<'l>(&'l self) -> &'l Bitmap
	{
		self.parent
	}

	pub fn to_bitmap(&self) -> Option<Bitmap>
	{
		clone_bitmap(self.allegro_bitmap)
	}
}

impl<'m> BitmapLike for SubBitmap<'m>
{
	fn get_bitmap(&self) -> *mut ALLEGRO_BITMAP
	{
		self.allegro_bitmap
	}
}

/* Should be safe, as the parent doesn't reference the child */
#[unsafe_destructor]
impl<'l> Drop for SubBitmap<'l>
{
	fn drop(&mut self)
	{
		unsafe
		{
			use internal::core::dummy_target;

			/* If this bitmap is the target then the target becomes invalid.
			 * Set it to the dummy target. */
			let target = al_get_target_bitmap();
			if target.is_null()
			{
				fail!("Null target bitmap!");
			}
			if target == self.allegro_bitmap
			{
				al_set_target_bitmap(dummy_target);
			}
			al_destroy_bitmap(self.allegro_bitmap);
		}
	}
}

pub fn new_bitmap_ref(bmp: *mut ALLEGRO_BITMAP) -> Bitmap
{
	Bitmap{ allegro_bitmap: bmp, is_ref: true, no_send_marker: NoSend }
}

pub fn clone_bitmap(bmp: *mut ALLEGRO_BITMAP) -> Option<Bitmap>
{
	unsafe
	{
		let b = al_clone_bitmap(bmp);
		if b.is_null()
		{
			None
		}
		else
		{
			Some(Bitmap{ allegro_bitmap: b, is_ref: false, no_send_marker: NoSend })
		}
	}
}

impl ::internal::core::Core
{
	pub fn set_new_bitmap_flags(&self, flags: BitmapFlags)
	{
		unsafe
		{
			al_set_new_bitmap_flags(flags.get() as c_int);
		}
	}

	pub fn set_new_bitmap_format(&self, format: PixelFormat)
	{
		unsafe
		{
			al_set_new_bitmap_format(format as c_int);
		}
	}

	pub fn create_bitmap(&self, w: i32, h: i32) -> Option<Bitmap>
	{
		Bitmap::new(w, h)
	}

	pub fn load_bitmap(&self, filename: &str) -> Option<Bitmap>
	{
		Bitmap::load(filename)
	}
}
<commit_msg>Add MemoryBitmap.<commit_after>use std::cast;
use std::libc::*;
use std::kinds::marker::NoSend;

use internal::bitmap_like::*;
use internal::color::*;

use ffi::*;

pub mod external
{
	pub use super::{Bitmap, SubBitmap, MemoryBitmap};
}

pub struct Bitmap
{
	allegro_bitmap: *mut ALLEGRO_BITMAP,
	is_ref: bool,
	no_send_marker: NoSend
}

impl Bitmap
{
	fn new(w: i32, h: i32) -> Option<Bitmap>
	{
		unsafe
		{
			let b =	al_create_bitmap(w as c_int, h as c_int);
			if b.is_null()
			{
				None
			}
			else
			{
				Some(Bitmap{ allegro_bitmap: b, is_ref: false, no_send_marker: NoSend })
			}
		}
	}

	fn load(filename: &str) -> Option<Bitmap>
	{
		let b = filename.with_c_str(|s|
		{
			unsafe
			{
				al_load_bitmap(s)
			}
		});
		if b.is_null()
		{
			None
		}
		else
		{
			Some(Bitmap{ allegro_bitmap: b, is_ref: false, no_send_marker: NoSend })
		}
	}

	pub fn create_sub_bitmap<'l>(&'l self, x: i32, y: i32, w: i32, h: i32) -> Option<SubBitmap<'l>>
	{
		unsafe
		{
			let b = al_create_sub_bitmap(self.allegro_bitmap, x as c_int, y as c_int, w as c_int, h as c_int);
			if b.is_null()
			{
				None
			}
			else
			{
				Some(SubBitmap{ allegro_bitmap: b, parent: self })
			}
		}
	}

	pub fn maybe_clone(&self) -> Option<Bitmap>
	{
		clone_bitmap(self.allegro_bitmap)
	}

	pub fn into_memory_bitmap(self) -> Result<MemoryBitmap, Bitmap>
	{
		if self.get_flags() & MEMORY_BITMAP && !self.is_ref
		{
			let bmp = self.allegro_bitmap;
			unsafe
			{
				// Don't run Bitmap's destructor
				cast::forget(self);
			}
			Ok(MemoryBitmap{ allegro_bitmap: bmp })
		}
		else
		{
			Err(self)
		}
	}
}

impl BitmapLike for Bitmap
{
	fn get_bitmap(&self) -> *mut ALLEGRO_BITMAP
	{
		self.allegro_bitmap
	}
}

impl Clone for Bitmap
{
	fn clone(&self) -> Bitmap
	{
		self.maybe_clone().unwrap()
	}
}

// Not Send just because of the marker
#[unsafe_destructor]
impl Drop for Bitmap
{
	fn drop(&mut self)
	{
		if !self.is_ref
		{
			unsafe
			{
				handle_bitmap_destruction(self.allegro_bitmap, false);
			}
		}
	}
}

pub struct MemoryBitmap
{
	allegro_bitmap: *mut ALLEGRO_BITMAP,
}

impl MemoryBitmap
{
	pub fn into_bitmap(self) -> Bitmap
	{
		let bmp = self.allegro_bitmap;
		unsafe
		{
			// Don't run MemoryBitmap's destructor
			cast::forget(self);
		}
		Bitmap{ allegro_bitmap: bmp, is_ref: false, no_send_marker: NoSend }
	}
}

impl Drop for MemoryBitmap
{
	fn drop(&mut self)
	{
		unsafe
		{
			handle_bitmap_destruction(self.allegro_bitmap, false);
		}
	}
}

pub struct SubBitmap<'m>
{
	allegro_bitmap: *mut ALLEGRO_BITMAP,
	parent: &'m Bitmap
}

impl<'m> SubBitmap<'m>
{
	pub fn create_sub_bitmap<'l>(&'l self, x: i32, y: i32, w: i32, h: i32) -> Option<SubBitmap<'l>>
	{
		unsafe
		{
			let b = al_create_sub_bitmap(self.allegro_bitmap, x as c_int, y as c_int, w as c_int, h as c_int);
			if b.is_null()
			{
				None
			}
			else
			{
				Some(SubBitmap{ allegro_bitmap: b, parent: self.parent })
			}
		}
	}

	pub fn get_parent<'l>(&'l self) -> &'l Bitmap
	{
		self.parent
	}

	pub fn to_bitmap(&self) -> Option<Bitmap>
	{
		clone_bitmap(self.allegro_bitmap)
	}
}

impl<'m> BitmapLike for SubBitmap<'m>
{
	fn get_bitmap(&self) -> *mut ALLEGRO_BITMAP
	{
		self.allegro_bitmap
	}
}

/* Should be safe, as the parent doesn't reference the child */
#[unsafe_destructor]
impl<'l> Drop for SubBitmap<'l>
{
	fn drop(&mut self)
	{
		unsafe
		{
			handle_bitmap_destruction(self.allegro_bitmap, true);
		}
	}
}

unsafe fn handle_bitmap_destruction(bmp: *mut ALLEGRO_BITMAP, is_sub_bitmap: bool)
{
	use internal::core::dummy_target;

	/* If this bitmap is the target or the parent of the target
	 * then the target becomes invalid. Set it to the dummy target. */
	let target = al_get_target_bitmap();
	if target.is_null()
	{
		fail!("Null target bitmap!");
	}
	if target == bmp || (!is_sub_bitmap && al_get_parent_bitmap(target) == bmp)
	{
		al_set_target_bitmap(dummy_target);
	}
	al_destroy_bitmap(bmp);
}

pub fn new_bitmap_ref(bmp: *mut ALLEGRO_BITMAP) -> Bitmap
{
	Bitmap{ allegro_bitmap: bmp, is_ref: true, no_send_marker: NoSend }
}

pub fn clone_bitmap(bmp: *mut ALLEGRO_BITMAP) -> Option<Bitmap>
{
	unsafe
	{
		let b = al_clone_bitmap(bmp);
		if b.is_null()
		{
			None
		}
		else
		{
			Some(Bitmap{ allegro_bitmap: b, is_ref: false, no_send_marker: NoSend })
		}
	}
}

impl ::internal::core::Core
{
	pub fn set_new_bitmap_flags(&self, flags: BitmapFlags)
	{
		unsafe
		{
			al_set_new_bitmap_flags(flags.get() as c_int);
		}
	}

	pub fn set_new_bitmap_format(&self, format: PixelFormat)
	{
		unsafe
		{
			al_set_new_bitmap_format(format as c_int);
		}
	}

	pub fn create_bitmap(&self, w: i32, h: i32) -> Option<Bitmap>
	{
		Bitmap::new(w, h)
	}

	pub fn load_bitmap(&self, filename: &str) -> Option<Bitmap>
	{
		Bitmap::load(filename)
	}
}
<|endoftext|>


<commit_before>// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::test_utils::{
    compare_balances,
    libra_swarm_utils::{get_op_tool, load_libra_root_storage},
    setup_swarm_and_client_proxy,
};
use cli::client_proxy::ClientProxy;
use debug_interface::NodeDebugClient;
use libra_trace::trace::trace_node;
use libra_types::{
    account_address::AccountAddress, account_config::testnet_dd_account_address,
    ledger_info::LedgerInfo, waypoint::Waypoint,
};

#[test]
fn test_create_mint_transfer() {
    let (_env, client) = setup_swarm_and_client_proxy(1, 0);
    check_create_mint_transfer(client);
}

// Test if we commit not only user transactions but also block metadata transactions,
// assert committed version > # of user transactions
#[test]
fn test_create_mint_transfer_block_metadata() {
    let (env, mut client) = setup_swarm_and_client_proxy(1, 0);

    // This script does 4 transactions
    check_create_mint_transfer(env.get_validator_client(0, None));

    let address = AccountAddress::from_hex_literal("0xA550C18").unwrap();
    let (_account, version) = client
        .get_latest_account(&["q", &address.to_string()])
        .unwrap();
    assert!(
        version > 4,
        "BlockMetadata txn not produced, current version: {}",
        version
    );
}

#[test]
fn test_basic_fault_tolerance() {
    // A configuration with 4 validators should tolerate single node failure.
    let (mut env, client) = setup_swarm_and_client_proxy(4, 1);
    env.validator_swarm.kill_node(0);
    check_create_mint_transfer(client);
}

#[test]
fn test_basic_restartability() {
    let (mut env, mut client) = setup_swarm_and_client_proxy(4, 0);

    client.create_next_account(false).unwrap();
    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mb", "0", "100", "Coin1"], true)
        .unwrap();
    client
        .mint_coins(&["mb", "1", "10", "Coin1"], true)
        .unwrap();
    client
        .transfer_coins(&["tb", "0", "1", "10", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(90.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(20.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));

    let peer_to_restart = 0;
    env.validator_swarm.kill_node(peer_to_restart);

    assert!(env.validator_swarm.add_node(peer_to_restart).is_ok());
    assert!(compare_balances(
        vec![(90.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(20.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));

    client
        .transfer_coins(&["tb", "0", "1", "10", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(80.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(30.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));
}

#[test]
fn test_client_waypoints() {
    let (env, mut client) = setup_swarm_and_client_proxy(4, 1);

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "0", "10", "Coin1"], true)
        .unwrap();

    // Create the waypoint for the initial epoch
    let genesis_li = client.latest_epoch_change_li().unwrap();
    assert_eq!(genesis_li.ledger_info().epoch(), 0);
    let genesis_waypoint = Waypoint::new_epoch_boundary(genesis_li.ledger_info()).unwrap();

    // Start another client with the genesis waypoint and make sure it successfully connects
    let mut client_with_waypoint = env.get_validator_client(0, Some(genesis_waypoint));
    client_with_waypoint.test_trusted_connection().unwrap();
    assert_eq!(
        client_with_waypoint.latest_epoch_change_li().unwrap(),
        genesis_li
    );

    // Start next epoch

    // This ugly blob is to remove a validator, we can do better...
    let peer_id = env
        .validator_swarm
        .get_validator(0)
        .unwrap()
        .validator_peer_id()
        .unwrap();
    let op_tool = get_op_tool(&env.validator_swarm, 1);
    let libra_root = load_libra_root_storage(&env.validator_swarm, 0);
    let context = op_tool.remove_validator(peer_id, &libra_root).unwrap();
    client
        .wait_for_transaction(context.address, context.sequence_number + 1)
        .unwrap();
    // end ugly blob

    client
        .mint_coins(&["mintb", "0", "10", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(20.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    let epoch_1_li = client
        .latest_epoch_change_li()
        .expect("Failed to retrieve end of epoch 1 LedgerInfo");

    assert_eq!(epoch_1_li.ledger_info().epoch(), 1);
    let epoch_1_waypoint = Waypoint::new_epoch_boundary(epoch_1_li.ledger_info())
        .expect("Failed to generate waypoint from end of epoch 1");

    // Start a client with the waypoint for end of epoch 1 and make sure it successfully connects
    client_with_waypoint = env.get_validator_client(1, Some(epoch_1_waypoint));
    client_with_waypoint.test_trusted_connection().unwrap();
    assert_eq!(
        client_with_waypoint.latest_epoch_change_li().unwrap(),
        epoch_1_li
    );

    // Verify that a client with the wrong waypoint is not going to be able to connect to the chain.
    let bad_li = LedgerInfo::mock_genesis(None);
    let bad_waypoint = Waypoint::new_epoch_boundary(&bad_li).unwrap();
    let mut client_with_bad_waypoint = env.get_validator_client(1, Some(bad_waypoint));
    assert!(client_with_bad_waypoint.test_trusted_connection().is_err());
}

#[test]
fn test_concurrent_transfers_single_node() {
    let (_env, mut client) = setup_swarm_and_client_proxy(1, 0);

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "0", "100", "Coin1"], true)
        .unwrap();

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "1", "10", "Coin1"], true)
        .unwrap();

    for _ in 0..20 {
        client
            .transfer_coins(&["t", "0", "1", "1", "Coin1"], false)
            .unwrap();
    }
    client
        .transfer_coins(&["tb", "0", "1", "1", "Coin1"], true)
        .unwrap();

    assert!(compare_balances(
        vec![(79.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(31.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));
}

#[test]
fn test_trace() {
    let (env, mut client) = setup_swarm_and_client_proxy(1, 0);

    let port = env.validator_swarm.get_validators_debug_ports()[0];
    let mut debug_client = NodeDebugClient::new("localhost", port);

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "0", "100", "Coin1"], true)
        .unwrap();

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "1", "10", "Coin1"], true)
        .unwrap();
    client
        .transfer_coins(&["t", "0", "1", "1", "Coin1"], false)
        .unwrap();

    let events = debug_client.get_events().expect("Failed to get events");
    let txn_node = format!("txn::{}::{}", testnet_dd_account_address(), 1);
    println!("Tracing {}", txn_node);

    trace_node(&events[..], &txn_node);
}

/// This helper function creates 3 new accounts, mints funds, transfers funds
/// between the accounts and verifies that these operations succeed.
fn check_create_mint_transfer(mut client: ClientProxy) {
    // Create account 0, mint 10 coins and check balance
    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "0", "10", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(10.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));

    // Create account 1, mint 1 coin, transfer 3 coins from account 0 to 1, check balances
    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "1", "1", "Coin1"], true)
        .unwrap();
    client
        .transfer_coins(&["tb", "0", "1", "3", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(7.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(4.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));

    // Create account 2, mint 15 coins and check balance
    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "2", "15", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(15.0, "Coin1".to_string())],
        client.get_balances(&["b", "2"]).unwrap(),
    ));
}
<commit_msg>[Smoke Tests] Merge overlapping client tests.<commit_after>// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::test_utils::{
    compare_balances,
    libra_swarm_utils::{get_op_tool, load_libra_root_storage},
    setup_swarm_and_client_proxy,
};
use cli::client_proxy::ClientProxy;
use debug_interface::NodeDebugClient;
use libra_trace::trace::trace_node;
use libra_types::{
    account_config::{libra_root_address, testnet_dd_account_address},
    ledger_info::LedgerInfo,
    waypoint::Waypoint,
};

#[test]
fn test_create_mint_transfer_block_metadata() {
    let (env, client) = setup_swarm_and_client_proxy(1, 0);

    // This script does 4 transactions
    check_create_mint_transfer(client);

    // Test if we commit not only user transactions but also block metadata transactions,
    // assert committed version > # of user transactions
    let (_account, version) = env
        .get_validator_client(0, None)
        .get_latest_account(&["q", &libra_root_address().to_string()])
        .unwrap();
    assert!(
        version > 4,
        "BlockMetadata txn not produced, current version: {}",
        version
    );
}

#[test]
fn test_basic_fault_tolerance() {
    // A configuration with 4 validators should tolerate single node failure.
    let (mut env, client) = setup_swarm_and_client_proxy(4, 1);
    env.validator_swarm.kill_node(0);
    check_create_mint_transfer(client);
}

#[test]
fn test_basic_restartability() {
    let (mut env, mut client) = setup_swarm_and_client_proxy(4, 0);

    client.create_next_account(false).unwrap();
    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mb", "0", "100", "Coin1"], true)
        .unwrap();
    client
        .mint_coins(&["mb", "1", "10", "Coin1"], true)
        .unwrap();
    client
        .transfer_coins(&["tb", "0", "1", "10", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(90.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(20.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));

    let peer_to_restart = 0;
    env.validator_swarm.kill_node(peer_to_restart);

    assert!(env.validator_swarm.add_node(peer_to_restart).is_ok());
    assert!(compare_balances(
        vec![(90.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(20.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));

    client
        .transfer_coins(&["tb", "0", "1", "10", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(80.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(30.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));
}

#[test]
fn test_client_waypoints() {
    let (env, mut client) = setup_swarm_and_client_proxy(4, 1);

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "0", "10", "Coin1"], true)
        .unwrap();

    // Create the waypoint for the initial epoch
    let genesis_li = client.latest_epoch_change_li().unwrap();
    assert_eq!(genesis_li.ledger_info().epoch(), 0);
    let genesis_waypoint = Waypoint::new_epoch_boundary(genesis_li.ledger_info()).unwrap();

    // Start another client with the genesis waypoint and make sure it successfully connects
    let mut client_with_waypoint = env.get_validator_client(0, Some(genesis_waypoint));
    client_with_waypoint.test_trusted_connection().unwrap();
    assert_eq!(
        client_with_waypoint.latest_epoch_change_li().unwrap(),
        genesis_li
    );

    // Start next epoch

    // This ugly blob is to remove a validator, we can do better...
    let peer_id = env
        .validator_swarm
        .get_validator(0)
        .unwrap()
        .validator_peer_id()
        .unwrap();
    let op_tool = get_op_tool(&env.validator_swarm, 1);
    let libra_root = load_libra_root_storage(&env.validator_swarm, 0);
    let context = op_tool.remove_validator(peer_id, &libra_root).unwrap();
    client
        .wait_for_transaction(context.address, context.sequence_number + 1)
        .unwrap();
    // end ugly blob

    client
        .mint_coins(&["mintb", "0", "10", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(20.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    let epoch_1_li = client
        .latest_epoch_change_li()
        .expect("Failed to retrieve end of epoch 1 LedgerInfo");

    assert_eq!(epoch_1_li.ledger_info().epoch(), 1);
    let epoch_1_waypoint = Waypoint::new_epoch_boundary(epoch_1_li.ledger_info())
        .expect("Failed to generate waypoint from end of epoch 1");

    // Start a client with the waypoint for end of epoch 1 and make sure it successfully connects
    client_with_waypoint = env.get_validator_client(1, Some(epoch_1_waypoint));
    client_with_waypoint.test_trusted_connection().unwrap();
    assert_eq!(
        client_with_waypoint.latest_epoch_change_li().unwrap(),
        epoch_1_li
    );

    // Verify that a client with the wrong waypoint is not going to be able to connect to the chain.
    let bad_li = LedgerInfo::mock_genesis(None);
    let bad_waypoint = Waypoint::new_epoch_boundary(&bad_li).unwrap();
    let mut client_with_bad_waypoint = env.get_validator_client(1, Some(bad_waypoint));
    assert!(client_with_bad_waypoint.test_trusted_connection().is_err());
}

#[test]
fn test_concurrent_transfers_single_node() {
    let (_env, mut client) = setup_swarm_and_client_proxy(1, 0);

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "0", "100", "Coin1"], true)
        .unwrap();

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "1", "10", "Coin1"], true)
        .unwrap();

    for _ in 0..20 {
        client
            .transfer_coins(&["t", "0", "1", "1", "Coin1"], false)
            .unwrap();
    }
    client
        .transfer_coins(&["tb", "0", "1", "1", "Coin1"], true)
        .unwrap();

    assert!(compare_balances(
        vec![(79.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(31.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));
}

#[test]
fn test_trace() {
    let (env, mut client) = setup_swarm_and_client_proxy(1, 0);

    let port = env.validator_swarm.get_validators_debug_ports()[0];
    let mut debug_client = NodeDebugClient::new("localhost", port);

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "0", "100", "Coin1"], true)
        .unwrap();

    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "1", "10", "Coin1"], true)
        .unwrap();
    client
        .transfer_coins(&["t", "0", "1", "1", "Coin1"], false)
        .unwrap();

    let events = debug_client.get_events().expect("Failed to get events");
    let txn_node = format!("txn::{}::{}", testnet_dd_account_address(), 1);
    println!("Tracing {}", txn_node);

    trace_node(&events[..], &txn_node);
}

/// This helper function creates 3 new accounts, mints funds, transfers funds
/// between the accounts and verifies that these operations succeed.
fn check_create_mint_transfer(mut client: ClientProxy) {
    // Create account 0, mint 10 coins and check balance
    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "0", "10", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(10.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));

    // Create account 1, mint 1 coin, transfer 3 coins from account 0 to 1, check balances
    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "1", "1", "Coin1"], true)
        .unwrap();
    client
        .transfer_coins(&["tb", "0", "1", "3", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(7.0, "Coin1".to_string())],
        client.get_balances(&["b", "0"]).unwrap(),
    ));
    assert!(compare_balances(
        vec![(4.0, "Coin1".to_string())],
        client.get_balances(&["b", "1"]).unwrap(),
    ));

    // Create account 2, mint 15 coins and check balance
    client.create_next_account(false).unwrap();
    client
        .mint_coins(&["mintb", "2", "15", "Coin1"], true)
        .unwrap();
    assert!(compare_balances(
        vec![(15.0, "Coin1".to_string())],
        client.get_balances(&["b", "2"]).unwrap(),
    ));
}
<|endoftext|>


<commit_before>use std::collections::VecDeque;
use std::mem;

#[cfg(feature = "winit")]
use winit;

use winapi::shared::dxgi1_4;
use winapi::shared::windef::{HWND, RECT};
use winapi::um::winuser::GetClientRect;
use winapi::um::d3d12;
use wio::com::ComPtr;

use hal::{self, format as f, image as i};
use {native as n, Backend, Instance, PhysicalDevice, QueueFamily};

use std::os::raw::c_void;

impl Instance {
    pub fn create_surface_from_hwnd(&self, hwnd: *mut c_void) -> Surface {
        let (width, height) = unsafe {
            let mut rect: RECT = mem::zeroed();
            if GetClientRect(hwnd as *mut _, &mut rect as *mut RECT) == 0 {
                panic!("GetClientRect failed");
            }
            ((rect.right - rect.left) as u32, (rect.bottom - rect.top) as u32)
        };

        Surface {
            factory: self.factory.clone(),
            wnd_handle: hwnd as *mut _,
            width: width,
            height: height,
        }
    }

    #[cfg(feature = "winit")]
    pub fn create_surface(&self, window: &winit::Window) -> Surface {
        use winit::os::windows::WindowExt;
        self.create_surface_from_hwnd(window.get_hwnd() as *mut _)
    }
}

pub struct Surface {
    pub(crate) factory: ComPtr<dxgi1_4::IDXGIFactory4>,
    pub(crate) wnd_handle: HWND,
    pub(crate) width: i::Size,
    pub(crate) height: i::Size,
}

unsafe impl Send for Surface { }
unsafe impl Sync for Surface { }

impl hal::Surface<Backend> for Surface {
    fn supports_queue_family(&self, queue_family: &QueueFamily) -> bool {
        match queue_family {
            &QueueFamily::Present => true,
            _ => false
        }
    }

    fn kind(&self) -> i::Kind {
        i::Kind::D2(self.width, self.height, 1, 1)
    }

    fn compatibility(
        &self, _: &PhysicalDevice,
    ) -> (hal::SurfaceCapabilities, Option<Vec<f::Format>>, Vec<hal::PresentMode>) {
        let extent = hal::window::Extent2D {
            width: self.width,
            height: self.height,
        };

        let capabilities = hal::SurfaceCapabilities {
            image_count: 2..16, // we currently use a flip effect which supports 2..16 buffers
            current_extent: Some(extent),
            extents: extent..extent,
            max_image_layers: 1,
            usage: i::Usage::COLOR_ATTACHMENT | i::Usage::TRANSFER_SRC,
        };

        // Sticking to FLIP swap effects for the moment.
        // We also expose sRGB buffers but they are handled internally as UNORM.
        // Roughly ordered by popularity..
        let formats = vec![
            f::Format::Bgra8Srgb,
            f::Format::Bgra8Unorm,
            f::Format::Rgba8Srgb,
            f::Format::Rgba8Unorm,
            f::Format::A2b10g10r10Unorm,
            f::Format::Rgba16Float,
        ];

        let present_modes = vec![
            hal::PresentMode::Fifo //TODO
        ];

        (capabilities, Some(formats), present_modes)
    }
}

pub struct Swapchain {
    pub(crate) inner: ComPtr<dxgi1_4::IDXGISwapChain3>,
    pub(crate) next_frame: usize,
    pub(crate) frame_queue: VecDeque<usize>,
    #[allow(dead_code)]
    pub(crate) rtv_heap: n::DescriptorHeap,
    // need to associate raw image pointers with the swapchain so they can be properly released
    // when the swapchain is destroyed
    pub(crate) _resources: Vec<ComPtr<d3d12::ID3D12Resource>>,
}

impl hal::Swapchain<Backend> for Swapchain {
    fn acquire_image(
        &mut self, _timout_ns: u64, _sync: hal::FrameSync<Backend>
    ) -> Result<hal::SwapImageIndex, hal::AcquireError> {
        // TODO: sync

        if false {
            // TODO: we need to block this at some point? (running out of backbuffers)
            //let num_images = self.images.len();
            let num_images = 1;
            let index = self.next_frame;
            self.frame_queue.push_back(index);
            self.next_frame = (self.next_frame + 1) % num_images;
        }

        // TODO:
        Ok(unsafe { self.inner.GetCurrentBackBufferIndex() })
    }
}

unsafe impl Send for Swapchain { }
unsafe impl Sync for Swapchain { }
<commit_msg>dx12: Fix window formatting<commit_after>use std::collections::VecDeque;
use std::mem;

#[cfg(feature = "winit")]
use winit;

use winapi::shared::dxgi1_4;
use winapi::shared::windef::{HWND, RECT};
use winapi::um::d3d12;
use winapi::um::winuser::GetClientRect;
use wio::com::ComPtr;

use hal::{self, format as f, image as i};
use {native as n, Backend, Instance, PhysicalDevice, QueueFamily};

use std::os::raw::c_void;

impl Instance {
    pub fn create_surface_from_hwnd(&self, hwnd: *mut c_void) -> Surface {
        let (width, height) = unsafe {
            let mut rect: RECT = mem::zeroed();
            if GetClientRect(hwnd as *mut _, &mut rect as *mut RECT) == 0 {
                panic!("GetClientRect failed");
            }
            (
                (rect.right - rect.left) as u32,
                (rect.bottom - rect.top) as u32,
            )
        };

        Surface {
            factory: self.factory.clone(),
            wnd_handle: hwnd as *mut _,
            width: width,
            height: height,
        }
    }

    #[cfg(feature = "winit")]
    pub fn create_surface(&self, window: &winit::Window) -> Surface {
        use winit::os::windows::WindowExt;
        self.create_surface_from_hwnd(window.get_hwnd() as *mut _)
    }
}

pub struct Surface {
    pub(crate) factory: ComPtr<dxgi1_4::IDXGIFactory4>,
    pub(crate) wnd_handle: HWND,
    pub(crate) width: i::Size,
    pub(crate) height: i::Size,
}

unsafe impl Send for Surface {}
unsafe impl Sync for Surface {}

impl hal::Surface<Backend> for Surface {
    fn supports_queue_family(&self, queue_family: &QueueFamily) -> bool {
        match queue_family {
            &QueueFamily::Present => true,
            _ => false,
        }
    }

    fn kind(&self) -> i::Kind {
        i::Kind::D2(self.width, self.height, 1, 1)
    }

    fn compatibility(
        &self,
        _: &PhysicalDevice,
    ) -> (
        hal::SurfaceCapabilities,
        Option<Vec<f::Format>>,
        Vec<hal::PresentMode>,
    ) {
        let extent = hal::window::Extent2D {
            width: self.width,
            height: self.height,
        };

        let capabilities = hal::SurfaceCapabilities {
            image_count: 2..16, // we currently use a flip effect which supports 2..16 buffers
            current_extent: Some(extent),
            extents: extent..extent,
            max_image_layers: 1,
            usage: i::Usage::COLOR_ATTACHMENT | i::Usage::TRANSFER_SRC,
        };

        // Sticking to FLIP swap effects for the moment.
        // We also expose sRGB buffers but they are handled internally as UNORM.
        // Roughly ordered by popularity..
        let formats = vec![
            f::Format::Bgra8Srgb,
            f::Format::Bgra8Unorm,
            f::Format::Rgba8Srgb,
            f::Format::Rgba8Unorm,
            f::Format::A2b10g10r10Unorm,
            f::Format::Rgba16Float,
        ];

        let present_modes = vec![
            hal::PresentMode::Fifo, //TODO
        ];

        (capabilities, Some(formats), present_modes)
    }
}

pub struct Swapchain {
    pub(crate) inner: ComPtr<dxgi1_4::IDXGISwapChain3>,
    pub(crate) next_frame: usize,
    pub(crate) frame_queue: VecDeque<usize>,
    #[allow(dead_code)]
    pub(crate) rtv_heap: n::DescriptorHeap,
    // need to associate raw image pointers with the swapchain so they can be properly released
    // when the swapchain is destroyed
    pub(crate) _resources: Vec<ComPtr<d3d12::ID3D12Resource>>,
}

impl hal::Swapchain<Backend> for Swapchain {
    fn acquire_image(
        &mut self,
        _timout_ns: u64,
        _sync: hal::FrameSync<Backend>,
    ) -> Result<hal::SwapImageIndex, hal::AcquireError> {
        // TODO: sync

        if false {
            // TODO: we need to block this at some point? (running out of backbuffers)
            //let num_images = self.images.len();
            let num_images = 1;
            let index = self.next_frame;
            self.frame_queue.push_back(index);
            self.next_frame = (self.next_frame + 1) % num_images;
        }

        // TODO:
        Ok(unsafe { self.inner.GetCurrentBackBufferIndex() })
    }
}

unsafe impl Send for Swapchain {}
unsafe impl Sync for Swapchain {}
<|endoftext|>


<commit_before>// Copyright © 2018, Canal TP and/or its affiliates. All rights reserved.
//
// This file is part of Navitia,
//     the software to build cool stuff with public transport.
//
// Hope you'll enjoy and contribute to this project,
//     powered by Canal TP (www.canaltp.fr).
// Help us simplify mobility and open public transport:
//     a non ending quest to the responsive locomotion way of traveling!
//
// LICENCE: This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU Affero General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>.
//
// Stay tuned using
// twitter @navitia
// IRC #navitia on freenode
// https://groups.google.com/d/forum/navitia
// www.navitia.io

extern crate csv;
extern crate geo;
#[macro_use]
extern crate log;
extern crate mimir;
extern crate mimirsbrunn;
#[macro_use]
extern crate serde_derive;
extern crate structopt;
#[macro_use]
extern crate structopt_derive;

use std::path::Path;
use mimir::rubber::Rubber;
use mimir::objects::{Addr, MimirObject};
use mimirsbrunn::admin_geofinder::AdminGeoFinder;
use std::fs;
use structopt::StructOpt;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct OpenAddresse {
    pub id: String,
    pub street: String,
    pub postcode: String,
    pub district: String,
    pub region: String,
    pub city: String,
    pub number: String,
    pub unit: String,
    pub hash: Option<String>,
    pub lat: f64,
    pub lon: f64,
}

impl OpenAddresse {
    pub fn into_addr(self, admins_geofinder: &AdminGeoFinder) -> mimir::Addr {
        let street_name = format!("{} ({})", self.street, self.city);
        let addr_name = format!("{} {}", self.number, self.street);
        let addr_label = format!("{} ({})", addr_name, self.city);
        let street_id = format!("street:{}", self.id); // TODO check if thats ok
        let admins = admins_geofinder.get(&geo::Coordinate {
            x: self.lat,
            y: self.lon,
        });

        let weight = admins
            .iter()
            .find(|a| a.level == 8)
            .map_or(0., |a| a.weight.get());

        let street = mimir::Street {
            id: street_id,
            street_name: self.street,
            label: street_name.to_string(),
            administrative_regions: admins,
            weight: weight,
            zip_codes: vec![self.postcode.clone()],
            coord: mimir::Coord::new(self.lat, self.lon),
        };
        mimir::Addr {
            id: format!("addr:{};{}", self.lon, self.lat),
            house_number: self.number,
            street: street,
            label: addr_label,
            coord: mimir::Coord::new(self.lat, self.lon),
            weight: weight,
            zip_codes: vec![self.postcode.clone()],
        }
    }
}

fn index_oa<I>(cnx_string: &str, dataset: &str, files: I)
where
    I: Iterator<Item = std::path::PathBuf>,
{
    let mut rubber = Rubber::new(cnx_string);

    let admins = rubber
        .get_admins_from_dataset(dataset)
        .unwrap_or_else(|err| {
            info!(
                "Administratives regions not found in es db for dataset {}. (error: {})",
                dataset, err
            );
            vec![]
        });
    let admins_geofinder = admins.iter().cloned().collect();

    let addr_index = rubber.make_index(Addr::doc_type(), dataset).unwrap();
    info!("Add data in elasticsearch db.");
    for f in files {
        info!("importing {:?}...", &f);
        let mut rdr = csv::ReaderBuilder::new()
            .has_headers(true)
            .from_path(&f)
            .unwrap();
        let iter = rdr.deserialize().filter_map(|r| {
            r.map_err(|e| info!("impossible to read line, error: {}", e))
                .ok()
                .map(|v: OpenAddresse| v.into_addr(&admins_geofinder))
        });
        match rubber.bulk_index(&addr_index, iter) {
            Err(e) => panic!("failed to bulk insert file {:?} because: {}", &f, e),
            Ok(nb) => info!("importing {:?}: {} addresses added.", &f, nb),
        }
    }
    rubber
        .publish_index(Addr::doc_type(), dataset, addr_index, Addr::is_geo_data())
        .unwrap();
}

#[derive(StructOpt, Debug)]
struct Args {
    /// openaddresses files. Can be either a directory or a file.
    #[structopt(short = "i", long = "input")]
    input: String,
    /// Elasticsearch parameters.
    #[structopt(short = "c", long = "connection-string",
                default_value = "http://localhost:9200/munin")]
    connection_string: String,
    /// Name of the dataset.
    #[structopt(short = "d", long = "dataset", default_value = "fr")]
    dataset: String,
}

fn main() {
    mimir::logger_init().unwrap();
    info!("importing open addresses into Mimir");

    let args = Args::from_args();

    let file_path = Path::new(&args.input);
    if file_path.is_dir() {
        let paths: std::fs::ReadDir = fs::read_dir(&args.input).unwrap();
        index_oa(
            &args.connection_string,
            &args.dataset,
            paths.map(|p| p.unwrap().path()),
        );
    } else {
        index_oa(
            &args.connection_string,
            &args.dataset,
            std::iter::once(std::path::PathBuf::from(&args.input)),
        );
    }
}
<commit_msg>corrections after review<commit_after>// Copyright © 2018, Canal TP and/or its affiliates. All rights reserved.
//
// This file is part of Navitia,
//     the software to build cool stuff with public transport.
//
// Hope you'll enjoy and contribute to this project,
//     powered by Canal TP (www.canaltp.fr).
// Help us simplify mobility and open public transport:
//     a non ending quest to the responsive locomotion way of traveling!
//
// LICENCE: This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU Affero General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>.
//
// Stay tuned using
// twitter @navitia
// IRC #navitia on freenode
// https://groups.google.com/d/forum/navitia
// www.navitia.io

extern crate csv;
extern crate geo;
#[macro_use]
extern crate log;
extern crate mimir;
extern crate mimirsbrunn;
#[macro_use]
extern crate serde_derive;
extern crate structopt;
#[macro_use]
extern crate structopt_derive;

use std::path::Path;
use mimir::rubber::Rubber;
use mimir::objects::{Addr, MimirObject};
use mimirsbrunn::admin_geofinder::AdminGeoFinder;
use std::fs;
use structopt::StructOpt;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct OpenAddresse {
    pub id: String,
    pub street: String,
    pub postcode: String,
    pub district: String,
    pub region: String,
    pub city: String,
    pub number: String,
    pub unit: String,
    pub lat: f64,
    pub lon: f64,
}

impl OpenAddresse {
    pub fn into_addr(self, admins_geofinder: &AdminGeoFinder, city_level: u32) -> mimir::Addr {
        let street_name = format!("{} ({})", self.street, self.city);
        let addr_name = format!("{} {}", self.number, self.street);
        let addr_label = format!("{} ({})", addr_name, self.city);
        let street_id = format!("street:{}", self.id); // TODO check if thats ok
        let admins = admins_geofinder.get(&geo::Coordinate {
            x: self.lat,
            y: self.lon,
        });

        let weight = admins
            .iter()
            .find(|a| a.level == city_level)
            .map_or(0., |a| a.weight.get());

        let street = mimir::Street {
            id: street_id,
            street_name: self.street,
            label: street_name.to_string(),
            administrative_regions: admins,
            weight: weight,
            zip_codes: vec![self.postcode.clone()],
            coord: mimir::Coord::new(self.lat, self.lon),
        };
        mimir::Addr {
            id: format!("addr:{};{}", self.lon, self.lat),
            house_number: self.number,
            street: street,
            label: addr_label,
            coord: mimir::Coord::new(self.lat, self.lon),
            weight: weight,
            zip_codes: vec![self.postcode.clone()],
        }
    }
}

fn index_oa<I>(cnx_string: &str, dataset: &str, city_level: u32, files: I)
where
    I: Iterator<Item = std::path::PathBuf>,
{
    let mut rubber = Rubber::new(cnx_string);

    let admins = rubber
        .get_admins_from_dataset(dataset)
        .unwrap_or_else(|err| {
            info!(
                "Administratives regions not found in es db for dataset {}. (error: {})",
                dataset, err
            );
            vec![]
        });
    let admins_geofinder = admins.into_iter().collect();

    let addr_index = rubber.make_index(Addr::doc_type(), dataset).unwrap();
    info!("Add data in elasticsearch db.");
    for f in files {
        info!("importing {:?}...", &f);
        let mut rdr = csv::ReaderBuilder::new()
            .has_headers(true)
            .from_path(&f)
            .unwrap();
        let iter = rdr.deserialize().filter_map(|r| {
            r.map_err(|e| info!("impossible to read line, error: {}", e))
                .ok()
                .map(|v: OpenAddresse| v.into_addr(&admins_geofinder, city_level))
        });
        match rubber.bulk_index(&addr_index, iter) {
            Err(e) => panic!("failed to bulk insert file {:?} because: {}", &f, e),
            Ok(nb) => info!("importing {:?}: {} addresses added.", &f, nb),
        }
    }
    rubber
        .publish_index(Addr::doc_type(), dataset, addr_index, Addr::is_geo_data())
        .unwrap();
}

#[derive(StructOpt, Debug)]
struct Args {
    /// openaddresses files. Can be either a directory or a file.
    #[structopt(short = "i", long = "input")]
    input: String,
    /// Elasticsearch parameters.
    #[structopt(short = "c", long = "connection-string",
                default_value = "http://localhost:9200/munin")]
    connection_string: String,
    /// Name of the dataset.
    #[structopt(short = "d", long = "dataset", default_value = "fr")]
    dataset: String,
    /// City level to  calculate weight.
    #[structopt(short = "C", long = "city-level", default_value = "8")]
    city_level: u32,
}

fn main() {
    mimir::logger_init().unwrap();
    info!("importing open addresses into Mimir");

    let args = Args::from_args();

    let file_path = Path::new(&args.input);
    if file_path.is_dir() {
        let paths: std::fs::ReadDir = fs::read_dir(&args.input).unwrap();
        index_oa(
            &args.connection_string,
            &args.dataset,
            args.city_level,
            paths.map(|p| p.unwrap().path()),
        );
    } else {
        index_oa(
            &args.connection_string,
            &args.dataset,
            args.city_level,
            std::iter::once(std::path::PathBuf::from(&args.input)),
        );
    }
}
<|endoftext|>


<commit_before>
extern mod sqlite;

use core::result;
use core::io;
use core::path;
use core::clone;
use core::to_str;

struct Price {
    date: ~str,
    close: ~str
}

impl clone::Clone for Price {
    fn clone(&self) -> Price {
        Price{date: self.date.clone(), close: self.close.clone()}
    }
}
impl to_str::ToStr for Price {
    fn to_str(&self) -> ~str {
        fmt!("%s: %s", self.date, self.close)
    }
}

fn load(filename: &str) -> ~[~str] {

    let fres = io::file_reader(~path::Path(filename));
    let f = result::unwrap(fres);
    return f.read_lines();
}

fn parse(lines: &[~str]) -> ~[Price] {

    let mut date = ~"";
    let mut close = ~"";
    let mut i = 0;
    let mut prices: ~[Price] = ~[];

    for lines.each |&line| {

        i = 0;
        for line.each_split_char(',') |piece| {
            if i == 0 {
                date = piece.to_str();
            } else if i == 4 {
                close = piece.to_str();
            }
            i += 1;
        }
        prices += [Price{date: date.clone(), close: close.clone()}]
    }

    return prices;
}

struct DB {
    filename: ~str,
    database: sqlite::Database
}

impl DB {

    fn open(name: &str) -> Result<DB, ~str> {

        match sqlite::open(name) {
            // Here 'name' is borrowed. Calling .to_str() makes a new
            // owned string, which the object will hold.
            Ok(db) => Ok(DB{filename: name.to_str(), database: db}),
            Err(e) => {
                println(fmt!("Error opening test.db: %?", e));
                Err(e.to_str())
            }
        }
    }

    fn create(&self) -> Result<sqlite::ResultCode, sqlite::ResultCode>  {
        self.database.exec("CREATE TABLE prices (date text, price real)")
    }

    fn write_price(&self, date: ~str, price: float) {
        let res = self.database.exec(fmt!(
            "INSERT INTO prices VALUES ('%s', %f)", date, price));
        if ! res.is_ok() {
            println(fmt!("INSERT error: %s", res.unwrap_err().to_str()));
        }
    }

    fn _min_max(&self, min_max: &str) -> ~Price {

        let extra =
            match min_max {
                "MAX" => "DESC",
                _ => ""
            };
        let sql = fmt!(
            "SELECT date, price FROM prices ORDER BY price %s LIMIT 1",
            extra);

        let st: sqlite::Cursor = self.database.prepare(sql, &None).unwrap();
        st.step();
        let date = st.get_text(0);
        let price = st.get_num(1);
        return ~Price{date:date, close:fmt!("%f", price)}
    }

    fn max_price(&self) -> ~Price {
        self._min_max("MAX")
    }

    fn min_price(&self) -> ~Price {
        self._min_max("MIN")
    }
}

fn save(prices: ~[Price]) {
    println("Start save");

    let res = DB::open("test.db");
    if ! res.is_ok() {
        return;
    }
    let db = res.unwrap();

    println("Opened");

    match db.create() {
        Err(code) => { println(fmt!("Error creating tables: %s",
                                    code.to_str())); return; }
        _ => {}
    };

    println("Created");

    let mut val: float = 0.0;
    for prices.each |&price| {
        println(price.to_str());

        val = float::from_str(price.close).unwrap();
        db.write_price(price.date.to_str(), val);
    }

}

fn cmd_init() {
    let all_lines: ~[~str] = load("vt.csv");

    // 'tail' because the first line is the column headers, which we
    // don't want.
    let lines: &[~str] = all_lines.tail();

    let prices: ~[Price] = parse(lines);
    println(fmt!("%?", prices));

    save(prices);
}

fn cmd_max() {

    let res = DB::open("test.db");
    if ! res.is_ok() {
        return;
    }
    let db = res.unwrap();

    println(db.max_price().to_str());
}

fn cmd_min() {
    let res = DB::open("test.db");
    if ! res.is_ok() {
        return;
    }
    let db = res.unwrap();

    println(db.min_price().to_str());
}

fn main() {

    static USAGE: &'static str = "Usage: highlow init|max|min";

    let args: &[~str] = os::args();
    if args.len() != 2 {
        println(USAGE);
        return;
    }

    let cmd: ~str = args[1].clone();

    match cmd {
        ~"init" => cmd_init(),
        ~"max" => cmd_max(),
        ~"min" => cmd_min(),
        other => println(fmt!("Invalid cmd: %s", other))
    }
}

<commit_msg>Added symbol to prices table<commit_after>
extern mod sqlite;

use core::result;
use core::io;
use core::path;
use core::clone;
use core::to_str;

struct Price {
    symbol: ~str,
    date: ~str,
    price: ~str
}

impl clone::Clone for Price {
    fn clone(&self) -> Price {
        Price{
            symbol: self.symbol.clone(),
            date: self.date.clone(),
            price: self.price.clone()}
    }
}
impl to_str::ToStr for Price {
    fn to_str(&self) -> ~str {
        fmt!("%s %s: %s", self.symbol, self.date, self.price)
    }
}

struct DB {
    filename: ~str,
    database: sqlite::Database
}

impl DB {

    fn open(name: &str) -> Result<DB, ~str> {

        match sqlite::open(name) {
            // Here 'name' is borrowed. Calling .to_str() makes a new
            // owned string, which the object will hold.
            Ok(db) => Ok(DB{filename: name.to_str(), database: db}),
            Err(e) => {
                println(fmt!("Error opening test.db: %?", e));
                Err(e.to_str())
            }
        }
    }

    fn create(&self) -> Result<sqlite::ResultCode, sqlite::ResultCode>  {
        self.database.exec(
            "CREATE TABLE prices (symbol text, date text, price real)")
    }

    fn write_price(&self, symbol: &str, date: &str, price: float) {
        let res = self.database.exec(fmt!(
            "INSERT INTO prices VALUES ('%s', '%s', %f)", symbol, date, price));
        if ! res.is_ok() {
            println(fmt!("INSERT error: %s", res.unwrap_err().to_str()));
        }
    }

    fn _min_max(&self, min_max: &str, symbol: &str) -> ~Price {

        let extra =
            match min_max {
                "MAX" => "DESC",
                _ => ""
            };
        let sql = fmt!(
            "SELECT symbol, date, price FROM prices WHERE symbol = '%s' ORDER BY price %s LIMIT 1",
            symbol,
            extra);

        let st: sqlite::Cursor = self.database.prepare(sql, &None).unwrap();
        st.step();
        let symbol = st.get_text(0);
        let date = st.get_text(1);
        let price = st.get_num(2);
        return ~Price{symbol: symbol, date:date, price:fmt!("%f", price)}
    }

    fn max_price(&self, symbol: &str) -> ~Price {
        self._min_max("MAX", symbol)
    }

    fn min_price(&self, symbol: &str) -> ~Price {
        self._min_max("MIN", symbol)
    }
}

fn load(filename: &str) -> ~[~str] {

    let fres = io::file_reader(~path::Path(filename));
    let f = result::unwrap(fres);
    return f.read_lines();
}

fn parse(symbol: &str, lines: &[~str]) -> ~[Price] {

    let mut date = ~"";
    let mut close = ~"";
    let mut i = 0;
    let mut prices: ~[Price] = ~[];

    for lines.each |&line| {

        i = 0;
        for line.each_split_char(',') |piece| {
            if i == 0 {
                date = piece.to_str();
            } else if i == 4 {
                close = piece.to_str();
            }
            i += 1;
        }
        prices += [Price{
            symbol: symbol.to_str(),
            date: date.clone(),
            price: close.clone()}
        ]
    }

    return prices;
}

fn save(prices: ~[Price]) {
    println("Start save");

    let res = DB::open("test.db");
    if ! res.is_ok() {
        return;
    }
    let db = res.unwrap();

    println("Opened");

    match db.create() {
        Err(code) => { println(fmt!("Error creating tables: %s",
                                    code.to_str())); return; }
        _ => {}
    };

    println("Created");

    let mut val: float = 0.0;
    for prices.each |&price| {
        println(price.to_str());

        val = float::from_str(price.price).unwrap();
        db.write_price(price.symbol.to_str(), price.date.to_str(), val);
    }

}

fn cmd_load(symbol: &str, filename: &str) {
    let all_lines: ~[~str] = load(filename);

    // 'tail' because the first line is the column headers, which we
    // don't want.
    let lines: &[~str] = all_lines.tail();

    let prices: ~[Price] = parse(symbol, lines);
    println(fmt!("%?", prices));

    save(prices);
}

fn cmd_max(symbol: &str) {

    let res = DB::open("test.db");
    if ! res.is_ok() {
        return;
    }
    let db = res.unwrap();

    println(db.max_price(symbol).to_str());
}

fn cmd_min(symbol: &str) {
    let res = DB::open("test.db");
    if ! res.is_ok() {
        return;
    }
    let db = res.unwrap();

    println(db.min_price(symbol).to_str());
}

fn main() {

    static USAGE: &'static str = "Usage: highlow load <symbol> <filename>|max <symbol>|min <symbol>";

    let args: &[~str] = os::args();
    if args.len() < 3 {
        println(USAGE);
        return;
    }

    let cmd: ~str = args[1].clone();
    let symbol: ~str = args[2].clone();

    match cmd {
        ~"load" => cmd_load(symbol, args[3]),
        ~"max" => cmd_max(symbol),
        ~"min" => cmd_min(symbol),
        other => println(fmt!("Invalid cmd: %s", other))
    }
}

<|endoftext|>


<commit_before>use std::fmt;
use std::str;
use std::fmt::{Result};

static NO_NAME : &'static str = "<none>";

// NOTE: Uncomment when https://github.com/mozilla/rust/issues/13703 is fixed.
// pub struct PreOrderNodes<'a, 'b> {
//   queue: Vec<&'a Node<'b>>
// }
//
// impl<'a, 'b> Iterator<&'a Node<'b>> for PreOrderNodes<'a, 'b> {
//   fn next( &mut self ) -> Option<&'a Node<'b>> {
//     match self.queue.pop() {
//       ex @ Some( node ) => {
//         match node.contents {
//           Children( ref x ) => {
//             for child in x.as_slice().iter().rev() {
//               self.queue.push( child )
//             }
//           }
//           _ => ()
//         };
//         ex
//       }
//       _ => None
//     }
//   }
// }

#[deriving(Show, PartialEq)]
pub enum NodeContents<'a> {
  /// A `&[u8]` byte slice this node matched in the parse input. Only leaf nodes
  /// have `Data` contents.
  Data( &'a [u8] ),

  /// Children of the node, if any. Only non-leaf nodes have `Children`
  /// contents.
  Children( Vec<Node<'a>> )
}

#[deriving(PartialEq)]
pub struct Node<'a> {
  /// The name of the node.
  pub name: &'static str,

  /// The (inclusive) start index of the range this node matches. It's the byte
  /// (NOT char) offset of the parse input.
  pub start: uint,

  /// The (exclusive) end index of the range this node matches. It's the byte
  /// (NOT char) offset of the parse input.
  pub end: uint,

  /// The contents of the node; this can be either children nodes or a matched
  /// `&[u8]` slice.
  pub contents: NodeContents<'a>
}

fn indent( formatter: &mut fmt::Formatter, indent_spaces: int )
    -> fmt::Result {
  for _ in range( 0, indent_spaces ) {
    try!( write!( formatter, " " ) )
  }
  Ok(())
}

impl<'a> Node<'a> {
  fn format( &self, formatter: &mut fmt::Formatter, indent_spaces: int )
      -> fmt::Result {
    try!( indent( formatter, indent_spaces ) );
    try!( write!( formatter,
                  "Node {{name: {0}, start: {1}, end: {2}",
                  self.displayName(), self.start, self.end ) );

    match self.contents {
      Data( data ) => {
        match str::from_utf8( data ) {
          Some( string ) => {
            try!( writeln!( formatter,
                            ", contents: \"{0}\" }}",
                            string ) );
          }
          _ => {
            try!( writeln!( formatter,
                            ", contents: \"{0}\" }}",
                            data ) );
          }
        }
      }
      Children( ref children ) => {
        try!( writeln!( formatter, " }}" ) );
        for child in children.iter() {
          try!( child.format( formatter, indent_spaces + 1) )
        }
      }
    };

    Ok(())
  }

  /// The node name if set, or "<none>" if unset.
  pub fn displayName( &self ) -> &'static str {
    if !self.name.is_empty() {
      self.name
    } else {
      NO_NAME
    }
  }

  /// Creates a `Node` with an empty name.
  pub fn withoutName( start: uint, end: uint, contents: NodeContents<'a> )
      -> Node<'a> {
    Node { name: "", start: start, end: end, contents: contents }
  }

  /// Creates a `Node` with the provided `name` and makes it a parent of the
  /// provided `children`.
  pub fn withChildren( name: &'static str, mut children: Vec<Node<'a>> )
      -> Node<'a> {
    // In case 'children' has only one node with an empty name, our new Node
    // will take the guts of the child with the new 'name'.
    if children.len() == 1 && children.get( 0 ).name.is_empty() {
      match children.pop() {
        Some( mut child ) => {
          child.name = name;
          return child;
        }
        _ => ()
      }
    }

    let start = if children.len() != 0 {
      children.get( 0 ).start
    } else {
      0
    };

    let end = match children.last() {
      Some( ref node ) => node.end,
      _ => 0
    };

    Node { name: name,
           start: start,
           end: end,
           contents: Children( children ) }
  }

  /// Traverses the tree rooted at the node with pre-order traversal. `visitor`
  /// is called on every node and traversal stops when `visitor` returns `false`.
  ///
  /// Normally this function would return an iterator instead of taking a
  /// visitor function, but a `rustc` bug is preventing that implementation.
  #[allow(dead_code)]
  pub fn preOrder( &self, visitor: |&Node| -> bool ) {
    fn inner( node: &Node, visitor: |&Node| -> bool ) -> bool {
      if !visitor( node ) {
        return false;
      }

      match node.contents {
        Children( ref x ) => {
          for node in x.iter() {
            if !inner( node, |x| visitor( x ) ) {
              return false;
            }
          }
        }
        _ => ()
      };

      true
    }
    inner( self, visitor );
  }

  // NOTE: Uncomment when https://github.com/mozilla/rust/issues/13703 is fixed.
  // pub fn preOrder<'b>( &'b self ) -> PreOrderNodes<'b, 'a> {
  //   PreOrderNodes { queue: vec!( self ) }
  // }

  /// Concatenates and returns all `&[u8]` data in the leaf nodes beneath
  /// the current node.
  #[allow(dead_code)]
  fn matchedData( &self ) -> Vec<u8> {
    match self.contents {
      Data( x ) => Vec::from_slice( x ),
      Children( ref children ) => {
        let mut out : Vec<u8> = vec!();
        for child in children.iter() {
          out.push_all( child.matchedData().as_slice() );
        }
        out
      }
    }
  }

  // TODO: methods in_order/pre_order/post_order that yield
  // iterators for walking the node tree structure
}

impl<'a> fmt::Show for Node<'a> {
  fn fmt( &self, formatter: &mut fmt::Formatter ) -> fmt::Result {
    self.format( formatter, 0 )
  }
}

#[cfg(test)]
mod tests {
  use super::{Node, Data};

  fn nameOnly( name: &'static str ) -> Node {
    Node { name: name, start: 0, end: 0, contents: Data( b"" ) }
  }

  fn contentsOnly( contents: &'static [u8] ) -> Node {
    Node { name: "", start: 0, end: 0, contents: Data( contents ) }
  }

  fn testTree() -> Node {
    // Tree looks like the following:
    //        a
    //   b    c    d
    //  e f   g
    Node::withChildren( "a", vec!(
        Node::withChildren( "b", vec!( nameOnly( "e" ), nameOnly( "f" ) ) ),
        Node::withChildren( "c", vec!( nameOnly( "g" ) ) ),
        nameOnly( "d" ) ) )
  }

  fn testTreeWithContents() -> Node {
    // Tree looks like the following (nodes with ' have contents):
    //          a
    //    b     c     'd
    // 'e  'f  'g
    Node::withChildren( "a", vec!(
        Node::withChildren(
          "b", vec!( contentsOnly( b"e" ), contentsOnly( b"f" ) ) ),
        Node::withChildren( "c", vec!( contentsOnly( b"g" ) ) ),
        contentsOnly( b"d" ) ) )
  }

  #[test]
  fn preOrder_FullIteration() {
    let root = testTree();
    let mut names : Vec<char> = vec!();
    root.preOrder( |ref x| {
      names.push( x.name.char_at( 0 ) );
      true
    });

    assert_eq!( names, vec!( 'a', 'b', 'e', 'f', 'c', 'g', 'd' ) )
  }

  #[test]
  fn preOrder_PartialIteration() {
    let root = testTree();
    let mut names : Vec<char> = vec!();
    root.preOrder( |ref x| {
      let ch =  x.name.char_at( 0 );
      names.push( ch );
      if ch == 'f' { false } else { true }
    });

    assert_eq!( names, vec!( 'a', 'b', 'e', 'f' ) )
  }

  #[test]
  fn matchedData_FullTree() {
    let root = testTreeWithContents();
    assert_eq!( b"efgd", root.matchedData().as_slice() )
  }
}
<commit_msg>Node iterator implemented!<commit_after>use std::fmt;
use std::str;
use std::fmt::{Result};

static NO_NAME : &'static str = "<none>";

pub struct PreOrderNodes<'a, 'b> {
  queue: Vec<&'a Node<'b>>
}

impl<'a> Iterator<&'a Node<'a>> for PreOrderNodes<'a, 'a> {
  fn next( &mut self ) -> Option<&'a Node<'a>> {
    match self.queue.pop() {
      ex @ Some( node ) => {
        match node.contents {
          Children( ref x ) => {
            for child in x.as_slice().iter().rev() {
              self.queue.push( child )
            }
          }
          _ => ()
        };
        ex
      }
      _ => None
    }
  }
}

#[deriving(Show, PartialEq)]
pub enum NodeContents<'a> {
  /// A `&[u8]` byte slice this node matched in the parse input. Only leaf nodes
  /// have `Data` contents.
  Data( &'a [u8] ),

  /// Children of the node, if any. Only non-leaf nodes have `Children`
  /// contents.
  Children( Vec<Node<'a>> )
}

#[deriving(PartialEq)]
pub struct Node<'a> {
  /// The name of the node.
  pub name: &'static str,

  /// The (inclusive) start index of the range this node matches. It's the byte
  /// (NOT char) offset of the parse input.
  pub start: uint,

  /// The (exclusive) end index of the range this node matches. It's the byte
  /// (NOT char) offset of the parse input.
  pub end: uint,

  /// The contents of the node; this can be either children nodes or a matched
  /// `&[u8]` slice.
  pub contents: NodeContents<'a>
}

fn indent( formatter: &mut fmt::Formatter, indent_spaces: int )
    -> fmt::Result {
  for _ in range( 0, indent_spaces ) {
    try!( write!( formatter, " " ) )
  }
  Ok(())
}

impl<'a> Node<'a> {
  fn format( &self, formatter: &mut fmt::Formatter, indent_spaces: int )
      -> fmt::Result {
    try!( indent( formatter, indent_spaces ) );
    try!( write!( formatter,
                  "Node {{name: {0}, start: {1}, end: {2}",
                  self.displayName(), self.start, self.end ) );

    match self.contents {
      Data( data ) => {
        match str::from_utf8( data ) {
          Some( string ) => {
            try!( writeln!( formatter,
                            ", contents: \"{0}\" }}",
                            string ) );
          }
          _ => {
            try!( writeln!( formatter,
                            ", contents: \"{0}\" }}",
                            data ) );
          }
        }
      }
      Children( ref children ) => {
        try!( writeln!( formatter, " }}" ) );
        for child in children.iter() {
          try!( child.format( formatter, indent_spaces + 1) )
        }
      }
    };

    Ok(())
  }

  /// The node name if set, or "<none>" if unset.
  pub fn displayName( &self ) -> &'static str {
    if !self.name.is_empty() {
      self.name
    } else {
      NO_NAME
    }
  }

  /// Creates a `Node` with an empty name.
  pub fn withoutName( start: uint, end: uint, contents: NodeContents<'a> )
      -> Node<'a> {
    Node { name: "", start: start, end: end, contents: contents }
  }

  /// Creates a `Node` with the provided `name` and makes it a parent of the
  /// provided `children`.
  pub fn withChildren( name: &'static str, mut children: Vec<Node<'a>> )
      -> Node<'a> {
    // In case 'children' has only one node with an empty name, our new Node
    // will take the guts of the child with the new 'name'.
    if children.len() == 1 && children.get( 0 ).name.is_empty() {
      match children.pop() {
        Some( mut child ) => {
          child.name = name;
          return child;
        }
        _ => ()
      }
    }

    let start = if children.len() != 0 {
      children.get( 0 ).start
    } else {
      0
    };

    let end = match children.last() {
      Some( ref node ) => node.end,
      _ => 0
    };

    Node { name: name,
           start: start,
           end: end,
           contents: Children( children ) }
  }

  /// Traverses the tree rooted at the node with pre-order traversal. Includes
  /// the `self` node as the first node.
  #[allow(dead_code)]
  pub fn preOrder<'b>( &'b self ) -> PreOrderNodes<'b, 'a> {
    PreOrderNodes { queue: vec!( self ) }
  }

  /// Concatenates and returns all `&[u8]` data in the leaf nodes beneath
  /// the current node.
  #[allow(dead_code)]
  fn matchedData( &self ) -> Vec<u8> {
    match self.contents {
      Data( x ) => Vec::from_slice( x ),
      Children( ref children ) => {
        let mut out : Vec<u8> = vec!();
        for child in children.iter() {
          out.push_all( child.matchedData().as_slice() );
        }
        out
      }
    }
  }

  // TODO: methods in_order/pre_order/post_order that yield
  // iterators for walking the node tree structure
}

impl<'a> fmt::Show for Node<'a> {
  fn fmt( &self, formatter: &mut fmt::Formatter ) -> fmt::Result {
    self.format( formatter, 0 )
  }
}

#[cfg(test)]
mod tests {
  use super::{Node, Data};

  fn nameOnly( name: &'static str ) -> Node {
    Node { name: name, start: 0, end: 0, contents: Data( b"" ) }
  }

  fn contentsOnly( contents: &'static [u8] ) -> Node {
    Node { name: "", start: 0, end: 0, contents: Data( contents ) }
  }

  fn testTree() -> Node {
    // Tree looks like the following:
    //        a
    //   b    c    d
    //  e f   g
    Node::withChildren( "a", vec!(
        Node::withChildren( "b", vec!( nameOnly( "e" ), nameOnly( "f" ) ) ),
        Node::withChildren( "c", vec!( nameOnly( "g" ) ) ),
        nameOnly( "d" ) ) )
  }

  fn testTreeWithContents() -> Node {
    // Tree looks like the following (nodes with ' have contents):
    //          a
    //    b     c     'd
    // 'e  'f  'g
    Node::withChildren( "a", vec!(
        Node::withChildren(
          "b", vec!( contentsOnly( b"e" ), contentsOnly( b"f" ) ) ),
        Node::withChildren( "c", vec!( contentsOnly( b"g" ) ) ),
        contentsOnly( b"d" ) ) )
  }

  #[test]
  fn preOrder_FullIteration() {
    let root = testTree();
    let names =
      root.preOrder().map( |x| x.name.char_at( 0 ) ).collect::<Vec<_>>();
    assert_eq!( names, vec!( 'a', 'b', 'e', 'f', 'c', 'g', 'd' ) )
  }

  #[test]
  fn matchedData_FullTree() {
    let root = testTreeWithContents();
    assert_eq!( b"efgd", root.matchedData().as_slice() )
  }
}
<|endoftext|>


<commit_before>#[macro_use]
extern crate serde_derive;

extern crate serde;

extern crate toml;
extern crate clap;

use clap::{App, Arg, ArgMatches};
use std::path::Path;
use std::fs::{create_dir, File};
use std::io::prelude::*;

#[derive(Serialize, Deserialize)]
struct Config {
    data_dir: Option<String>,
}

const CONFIG_PATH: &'static str = "/etc/blochs/";
const CONFIG_FILE_NAME: &'static str = "server.toml";
const DEFAULT_DATA_DIR: &'static str = "/var/lib/blochs/";

fn main() {
    let options = get_provided_options();

    let config_path = Path::new(CONFIG_PATH);
    ensure_dir_exists(&config_path);

    let config_file_path = config_path.join(CONFIG_FILE_NAME);

    let actual_content = get_file_content(&config_file_path);

    let mut config_values: Config = toml::from_str(&actual_content).unwrap();

    let config_or_default_data_dir = config_values.data_dir.unwrap_or(DEFAULT_DATA_DIR.to_string());

    let new_data_dir = options.value_of("data.dir").unwrap_or(&config_or_default_data_dir);

    config_values.data_dir = Some(new_data_dir.to_string());

    let new_config_content = toml::to_string(&config_values).unwrap();

    let mut write_config_file = match File::create(config_file_path.as_path()) {
        Ok(file) => file,
        Err(err) => panic!("Could not open config file {:?}: {}", config_file_path.display(), err),
    };

    match write_config_file.write_all(new_config_content.as_bytes()) {
        Ok(_) => println!("New config saved at {}:\n\n{}", config_file_path.display(), new_config_content),
        Err(why) => panic!("Could not write config file {:?}: {}", config_file_path.display(), why),
    };
}

fn get_file_content(path: &Path) -> String {
    let mut content = String::new();
    if path.exists() {
        let mut file = match File::open(&path) {
            Ok(file) => file,
            Err(err) => panic!("Could not open file {:?}: {}", path.display(), err),
        };

        match file.read_to_string(&mut content) {
            Ok(_) => {},
            Err(err) => panic!("Could not read file {:?}: {}", path.display(), err),
        };
    }
    return content;
}

fn ensure_dir_exists(path: &Path) {
    if !path.exists() {
        match create_dir(path) {
            Err(err) => panic!("Could not create directory under {}: {}", path.display(), err),
            Ok(_) => {}
        };
    }
}

fn get_provided_options<'a>() -> ArgMatches<'a> {
    return App::new(env!("CARGO_PKG_NAME"))
        .version(env!("CARGO_PKG_VERSION"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .about(env!("CARGO_PKG_DESCRIPTION"))
        .arg(Arg::with_name("data.dir")
            .long("data-dir")
            .value_name("DIR")
            .help(&format!("Sets where database data will be stored (default {})", DEFAULT_DATA_DIR))
        ).get_matches();
}
<commit_msg>extract file writing to a new function<commit_after>#[macro_use]
extern crate serde_derive;

extern crate serde;

extern crate toml;
extern crate clap;

use clap::{App, Arg, ArgMatches};
use std::path::Path;
use std::fs::{create_dir, File};
use std::io::prelude::*;
use std::io::Result;

#[derive(Serialize, Deserialize)]
struct Config {
    data_dir: Option<String>,
}

const CONFIG_PATH: &'static str = "/etc/blochs/";
const CONFIG_FILE_NAME: &'static str = "server.toml";
const DEFAULT_DATA_DIR: &'static str = "/var/lib/blochs/";

fn main() {
    let options = get_provided_options();

    let config_path = Path::new(CONFIG_PATH);
    ensure_dir_exists(&config_path);

    let config_file_path = config_path.join(CONFIG_FILE_NAME);

    let actual_content = get_file_content(&config_file_path);

    let mut config_values: Config = toml::from_str(&actual_content).unwrap();

    let config_or_default_data_dir = config_values.data_dir.unwrap_or(DEFAULT_DATA_DIR.to_string());

    let new_data_dir = options.value_of("data.dir").unwrap_or(&config_or_default_data_dir);

    config_values.data_dir = Some(new_data_dir.to_string());

    let new_config_content = toml::to_string(&config_values).unwrap();

    match rewrite_file(&config_file_path, &new_config_content) {
        Ok(_) => println!("New config saved at {}:\n\n{}", config_file_path.display(), new_config_content),
        Err(why) => panic!("Could not write config file {:?}: {}", config_file_path.display(), why),
    };
}

fn rewrite_file(path: &Path, new_content: &String) -> Result<()> {
    let mut file = match File::create(path) {
        Ok(file) => file,
        Err(err) => panic!("Could not open config file {:?}: {}", path.display(), err),
    };

    return file.write_all(new_content.as_bytes());
}

fn get_file_content(path: &Path) -> String {
    let mut content = String::new();
    if path.exists() {
        let mut file = match File::open(&path) {
            Ok(file) => file,
            Err(err) => panic!("Could not open file {:?}: {}", path.display(), err),
        };

        match file.read_to_string(&mut content) {
            Ok(_) => {},
            Err(err) => panic!("Could not read file {:?}: {}", path.display(), err),
        };
    }
    return content;
}

fn ensure_dir_exists(path: &Path) {
    if !path.exists() {
        match create_dir(path) {
            Err(err) => panic!("Could not create directory under {}: {}", path.display(), err),
            Ok(_) => {}
        };
    }
}

fn get_provided_options<'a>() -> ArgMatches<'a> {
    return App::new(env!("CARGO_PKG_NAME"))
        .version(env!("CARGO_PKG_VERSION"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .about(env!("CARGO_PKG_DESCRIPTION"))
        .arg(Arg::with_name("data.dir")
            .long("data-dir")
            .value_name("DIR")
            .help(&format!("Sets where database data will be stored (default {})", DEFAULT_DATA_DIR))
        ).get_matches();
}
<|endoftext|>


<commit_before>extern crate libc;
extern crate termbox_simple;

use termbox_simple::*;

fn main() {
    let mut termbox = Termbox::init().unwrap();

    // Set up the descriptors for select()
    let mut fd_set : libc::fd_set = unsafe { std::mem::zeroed() };

    unsafe { libc::FD_SET(libc::STDIN_FILENO, &mut fd_set); }

    loop {
        let mut fd_set_ = fd_set.clone();
        let ret =
            unsafe {
                libc::select(1,
                             &mut fd_set_,           // read fds
                             std::ptr::null_mut(),   // write fds
                             std::ptr::null_mut(),   // error fds
                             std::ptr::null_mut())   // timeval
            };

        if ret == -1 {
            termbox.resize();
        } else if unsafe { libc::FD_ISSET(0, &mut fd_set_) } {
            let mut buf : Vec<u8> = vec![];
            if read_input_events(&mut buf) {
                let string = format!("{:?}", buf);
                termbox.clear();
                if buf == vec![27] { break; }
                for (char_idx, char) in string.chars().enumerate() {
                    termbox.change_cell(char_idx as libc::c_int, 0, char, TB_WHITE, TB_DEFAULT);
                }
                termbox.present();
            }
        }
    }
}

/// Read stdin contents if it's ready for reading. Returns true when it was able
/// to read. Buffer is not modified when return value is 0.
fn read_input_events(buf : &mut Vec<u8>) -> bool {
    let mut bytes_available : i32 = 0; // this really needs to be a 32-bit value
    let ioctl_ret = unsafe { libc::ioctl(libc::STDIN_FILENO, libc::FIONREAD, &mut bytes_available) };
    // println!("ioctl_ret: {}", ioctl_ret);
    // println!("bytes_available: {}", bytes_available);
    if ioctl_ret < 0 || bytes_available == 0 {
        false
    } else {
        buf.clear();
        buf.reserve(bytes_available as usize);

        let buf_ptr : *mut libc::c_void = buf.as_ptr() as *mut libc::c_void;
        let bytes_read = unsafe { libc::read(libc::STDIN_FILENO, buf_ptr, bytes_available as usize) };
        debug_assert!(bytes_read == bytes_available as isize);

        unsafe { buf.set_len(bytes_read as usize); }
        true
    }
}
<commit_msg>Port termbox example to ev_loop<commit_after>extern crate ev_loop;
extern crate libc;
extern crate termbox_simple;

use termbox_simple::*;
use ev_loop::{EvLoop, READ_EV};

fn main() {
    let mut ev_loop: EvLoop<Termbox> = EvLoop::new();

    ev_loop.add_fd(libc::STDIN_FILENO, READ_EV, Box::new(|_, ctrl, termbox| {
        let mut buf : Vec<u8> = vec![];
        if read_input_events(&mut buf) {
            let string = format!("{:?}", buf);
            termbox.clear();
            if buf == vec![27] {
                ctrl.stop();
            }
            for (char_idx, char) in string.chars().enumerate() {
                termbox.change_cell(char_idx as libc::c_int, 0, char, TB_WHITE, TB_DEFAULT);
            }
            termbox.present();
        }
    }));

    {
        let mut sig_mask: libc::sigset_t = unsafe { std::mem::zeroed() };
        unsafe {
            libc::sigemptyset(&mut sig_mask as *mut libc::sigset_t);
            libc::sigaddset(&mut sig_mask as *mut libc::sigset_t, libc::SIGWINCH);
        };

        ev_loop.add_signal(&sig_mask, Box::new(|_, termbox| {
            termbox.resize();
        }));
    }

    ev_loop.run(Termbox::init().unwrap());
}

/// Read stdin contents if it's ready for reading. Returns true when it was able
/// to read. Buffer is not modified when return value is 0.
fn read_input_events(buf : &mut Vec<u8>) -> bool {
    let mut bytes_available : i32 = 0; // this really needs to be a 32-bit value
    let ioctl_ret = unsafe { libc::ioctl(libc::STDIN_FILENO, libc::FIONREAD, &mut bytes_available) };
    // println!("ioctl_ret: {}", ioctl_ret);
    // println!("bytes_available: {}", bytes_available);
    if ioctl_ret < 0 || bytes_available == 0 {
        false
    } else {
        buf.clear();
        buf.reserve(bytes_available as usize);

        let buf_ptr : *mut libc::c_void = buf.as_ptr() as *mut libc::c_void;
        let bytes_read = unsafe { libc::read(libc::STDIN_FILENO, buf_ptr, bytes_available as usize) };
        debug_assert!(bytes_read == bytes_available as isize);

        unsafe { buf.set_len(bytes_read as usize); }
        true
    }
}
<|endoftext|>


<commit_before>// compile-flags: -Zmiri-seed=0000000000000000

use std::collections::{self, HashMap};
use std::hash::{BuildHasherDefault, BuildHasher};

fn test_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
    map.insert(0, 0);
    assert_eq!(map.values().fold(0, |x, y| x+y), 0);

    let table_base = map.get(&0).unwrap() as *const _;

    let num = 22; // large enough to trigger a resize
    for i in 1..num {
        map.insert(i, i);
    }
    assert!(table_base != map.get(&0).unwrap() as *const _); // make sure relocation happened
    assert_eq!(map.values().fold(0, |x, y| x+y), num*(num-1)/2); // check the right things are in the table now

    // Inserting again replaces the existing entries
    for i in 0..num {
        map.insert(i, num-1-i);
    }
    assert_eq!(map.values().fold(0, |x, y| x+y), num*(num-1)/2);

    // TODO: Test Entry API, Iterators, ...

}

fn main() {
    let _map : HashMap<i32, i32, BuildHasherDefault<collections::hash_map::DefaultHasher>> = Default::default();
    let map_normal: HashMap<i32, i32> = HashMap::new();

    test_map(map_normal);
}
<commit_msg>Disable normal HashMap test on OS X<commit_after>// compile-flags: -Zmiri-seed=0000000000000000

use std::collections::{self, HashMap};
use std::hash::{BuildHasherDefault, BuildHasher};

fn test_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
    map.insert(0, 0);
    assert_eq!(map.values().fold(0, |x, y| x+y), 0);

    let table_base = map.get(&0).unwrap() as *const _;

    let num = 22; // large enough to trigger a resize
    for i in 1..num {
        map.insert(i, i);
    }
    assert!(table_base != map.get(&0).unwrap() as *const _); // make sure relocation happened
    assert_eq!(map.values().fold(0, |x, y| x+y), num*(num-1)/2); // check the right things are in the table now

    // Inserting again replaces the existing entries
    for i in 0..num {
        map.insert(i, num-1-i);
    }
    assert_eq!(map.values().fold(0, |x, y| x+y), num*(num-1)/2);

    // TODO: Test Entry API, Iterators, ...

}

fn main() {
    let _map : HashMap<i32, i32, BuildHasherDefault<collections::hash_map::DefaultHasher>> = Default::default();

    // TODO: Implement random number generation on OS X
    if cfg!(not(target_os = "darwin")) {
        let map_normal: HashMap<i32, i32> = HashMap::new();
        test_map(map_normal);
    } else {
        test_map(_map);
    }
}
<|endoftext|>


<commit_before>/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_ecs::{Error, Region, PKG_VERSION};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

// List your clusters.
// snippet-start:[ecs.rust.describe-clusters]
async fn show_clusters(client: &aws_sdk_ecs::Client) -> Result<(), aws_sdk_ecs::Error> {
    let resp = client.describe_clusters().send().await?;

    let clusters = resp.clusters.unwrap_or_default();
    println!("Found {} clusters:", clusters.len());

    for cluster in clusters {
        println!("  ARN:  {}", cluster.cluster_arn.unwrap());
        println!("  Name: {}", cluster.cluster_name.unwrap());
    }
    Ok(())
}
// snippet-end:[ecs.rust.describe-clusters]

/// Lists your Amazon Elastic Container Service cluster in the Region.
/// # Arguments
///
/// * `[-r REGION]` - The Region in which the client is created.
///   If not supplied, uses the value of the **AWS_REGION** environment variable.
///   If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    let Opt { region, verbose } = Opt::from_args();

    if verbose {
        tracing_subscriber::fmt::init();
    }

    let region_provider = RegionProviderChain::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    if verbose {
        println!();
        println!("ECS client version: {}", PKG_VERSION);
        println!(
            "Region:             {}",
            region_provider.region().await.unwrap().as_ref()
        );
        println!();
    }

    let shared_config = aws_config::from_env().region(region_provider).load().await;
    let client = aws_sdk_ecs::Client::new(&shared_config);

    show_clusters(&client).await
}
<commit_msg>Updated Rust code example to use accessor functions instead of public fields<commit_after>/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_ecs::{Error, Region, PKG_VERSION};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

// List your clusters.
// snippet-start:[ecs.rust.describe-clusters]
async fn show_clusters(client: &aws_sdk_ecs::Client) -> Result<(), aws_sdk_ecs::Error> {
    let resp = client.describe_clusters().send().await?;

    let clusters = resp.clusters().unwrap_or_default();
    println!("Found {} clusters:", clusters.len());

    for cluster in clusters {
        println!("  ARN:  {}", cluster.cluster_arn().unwrap());
        println!("  Name: {}", cluster.cluster_name().unwrap());
    }
    Ok(())
}
// snippet-end:[ecs.rust.describe-clusters]

/// Lists your Amazon Elastic Container Service cluster in the Region.
/// # Arguments
///
/// * `[-r REGION]` - The Region in which the client is created.
///   If not supplied, uses the value of the **AWS_REGION** environment variable.
///   If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    let Opt { region, verbose } = Opt::from_args();

    if verbose {
        tracing_subscriber::fmt::init();
    }

    let region_provider = RegionProviderChain::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    if verbose {
        println!();
        println!("ECS client version: {}", PKG_VERSION);
        println!(
            "Region:             {}",
            region_provider.region().await.unwrap().as_ref()
        );
        println!();
    }

    let shared_config = aws_config::from_env().region(region_provider).load().await;
    let client = aws_sdk_ecs::Client::new(&shared_config);

    show_clusters(&client).await
}
<|endoftext|>