<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! [@supports rules](https://drafts.csswg.org/css-conditional-3/#at-supports)

use cssparser::{Delimiter, parse_important, Parser, SourceLocation, Token};
use cssparser::{ParseError as CssParseError, ParserInput};
#[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
use parser::ParserContext;
use properties::{PropertyId, PropertyDeclaration, SourcePropertyDeclaration};
use selectors::parser::SelectorParseErrorKind;
use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::{ToCss, ParseError};
use stylesheets::{CssRuleType, CssRules};

/// An [`@supports`][supports] rule.
///
/// [supports]: https://drafts.csswg.org/css-conditional-3/#at-supports
#[derive(Debug)]
pub struct SupportsRule {
    /// The parsed condition
    pub condition: SupportsCondition,
    /// Child rules
    pub rules: Arc<Locked<CssRules>>,
    /// The result of evaluating the condition
    pub enabled: bool,
    /// The line and column of the rule's source code.
    pub source_location: SourceLocation,
}

impl SupportsRule {
    /// Measure heap usage.
    #[cfg(feature = "gecko")]
    pub fn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize {
        // Measurement of other fields may be added later.
        self.rules.unconditional_shallow_size_of(ops) +
            self.rules.read_with(guard).size_of(guard, ops)
    }
}

impl ToCssWithGuard for SupportsRule {
    fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
    where W: fmt::Write {
        dest.write_str("@supports ")?;
        self.condition.to_css(dest)?;
        self.rules.read_with(guard).to_css_block(guard, dest)
    }
}

impl DeepCloneWithLock for SupportsRule {
    fn deep_clone_with_lock(
        &self,
        lock: &SharedRwLock,
        guard: &SharedRwLockReadGuard,
        params: &DeepCloneParams,
    ) -> Self {
        let rules = self.rules.read_with(guard);
        SupportsRule {
            condition: self.condition.clone(),
            rules: Arc::new(lock.wrap(rules.deep_clone_with_lock(lock, guard, params))),
            enabled: self.enabled,
            source_location: self.source_location.clone(),
        }
    }
}

/// An @supports condition
///
/// <https://drafts.csswg.org/css-conditional-3/#at-supports>
#[derive(Clone, Debug)]
pub enum SupportsCondition {
    /// `not (condition)`
    Not(Box<SupportsCondition>),
    /// `(condition)`
    Parenthesized(Box<SupportsCondition>),
    /// `(condition) and (condition) and (condition) ..`
    And(Vec<SupportsCondition>),
    /// `(condition) or (condition) or (condition) ..`
    Or(Vec<SupportsCondition>),
    /// `property-ident: value` (value can be any tokens)
    Declaration(Declaration),
    /// `(any tokens)` or `func(any tokens)`
    FutureSyntax(String),
}

impl SupportsCondition {
    /// Parse a condition
    ///
    /// <https://drafts.csswg.org/css-conditional/#supports_condition>
    pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<SupportsCondition, ParseError<'i>> {
        if let Ok(_) = input.try(|i| i.expect_ident_matching("not")) {
            let inner = SupportsCondition::parse_in_parens(input)?;
            return Ok(SupportsCondition::Not(Box::new(inner)));
        }

        let in_parens = SupportsCondition::parse_in_parens(input)?;

        let location = input.current_source_location();
        let (keyword, wrapper) = match input.next() {
            Err(_) => {
                // End of input
                return Ok(in_parens)
            }
            Ok(&Token::Ident(ref ident)) => {
                match_ignore_ascii_case! { &ident,
                    "and" => ("and", SupportsCondition::And as fn(_) -> _),
                    "or" => ("or", SupportsCondition::Or as fn(_) -> _),
                    _ => return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone())))
                }
            }
            Ok(t) => return Err(location.new_unexpected_token_error(t.clone()))
        };

        let mut conditions = Vec::with_capacity(2);
        conditions.push(in_parens);
        loop {
            conditions.push(SupportsCondition::parse_in_parens(input)?);
            if input.try(|input| input.expect_ident_matching(keyword)).is_err() {
                // Did not find the expected keyword.
                // If we found some other token,
                // it will be rejected by `Parser::parse_entirely` somewhere up the stack.
                return Ok(wrapper(conditions))
            }
        }
    }

    /// <https://drafts.csswg.org/css-conditional-3/#supports_condition_in_parens>
    fn parse_in_parens<'i, 't>(input: &mut Parser<'i, 't>) -> Result<SupportsCondition, ParseError<'i>> {
        // Whitespace is normally taken care of in `Parser::next`,
        // but we want to not include it in `pos` for the SupportsCondition::FutureSyntax cases.
        while input.try(Parser::expect_whitespace).is_ok() {}
        let pos = input.position();
        let location = input.current_source_location();
        // FIXME: remove clone() when lifetimes are non-lexical
        match input.next()?.clone() {
            Token::ParenthesisBlock => {
                let nested = input.try(|input| {
                    input.parse_nested_block(|i| parse_condition_or_declaration(i))
                });
                if nested.is_ok() {
                    return nested;
                }
            }
            Token::Function(_) => {}
            t => return Err(location.new_unexpected_token_error(t)),
        }
        input.parse_nested_block(|i| consume_any_value(i))?;
        Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
    }

    /// Evaluate a supports condition
    pub fn eval(&self, cx: &ParserContext) -> bool {
        match *self {
            SupportsCondition::Not(ref cond) => !cond.eval(cx),
            SupportsCondition::Parenthesized(ref cond) => cond.eval(cx),
            SupportsCondition::And(ref vec) => vec.iter().all(|c| c.eval(cx)),
            SupportsCondition::Or(ref vec) => vec.iter().any(|c| c.eval(cx)),
            SupportsCondition::Declaration(ref decl) => decl.eval(cx),
            SupportsCondition::FutureSyntax(_) => false
        }
    }
}

/// supports_condition | declaration
/// <https://drafts.csswg.org/css-conditional/#dom-css-supports-conditiontext-conditiontext>
pub fn parse_condition_or_declaration<'i, 't>(input: &mut Parser<'i, 't>)
                                              -> Result<SupportsCondition, ParseError<'i>> {
    if let Ok(condition) = input.try(SupportsCondition::parse) {
        Ok(SupportsCondition::Parenthesized(Box::new(condition)))
    } else {
        Declaration::parse(input).map(SupportsCondition::Declaration)
    }
}

impl ToCss for SupportsCondition {
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
        where W: fmt::Write,
    {
        match *self {
            SupportsCondition::Not(ref cond) => {
                dest.write_str("not ")?;
                cond.to_css(dest)
            }
            SupportsCondition::Parenthesized(ref cond) => {
                dest.write_str("(")?;
                cond.to_css(dest)?;
                dest.write_str(")")
            }
            SupportsCondition::And(ref vec) => {
                let mut first = true;
                for cond in vec {
                    if !first {
                        dest.write_str(" and ")?;
                    }
                    first = false;
                    cond.to_css(dest)?;
                }
                Ok(())
            }
            SupportsCondition::Or(ref vec) => {
                let mut first = true;
                for cond in vec {
                    if !first {
                        dest.write_str(" or ")?;
                    }
                    first = false;
                    cond.to_css(dest)?;
                }
                Ok(())
            }
            SupportsCondition::Declaration(ref decl) => {
                dest.write_str("(")?;
                decl.to_css(dest)?;
                dest.write_str(")")
            }
            SupportsCondition::FutureSyntax(ref s) => dest.write_str(&s),
        }
    }
}

#[derive(Clone, Debug)]
/// A possibly-invalid property declaration
pub struct Declaration(pub String);

impl ToCss for Declaration {
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
        dest.write_str(&self.0)
    }
}

/// <https://drafts.csswg.org/css-syntax-3/#typedef-any-value>
fn consume_any_value<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), ParseError<'i>> {
    input.expect_no_error_token().map_err(|err| err.into())
}

impl Declaration {
    /// Parse a declaration
    pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Declaration, ParseError<'i>> {
        let pos = input.position();
        input.expect_ident()?;
        input.expect_colon()?;
        consume_any_value(input)?;
        Ok(Declaration(input.slice_from(pos).to_owned()))
    }

    /// Determine if a declaration parses
    ///
    /// <https://drafts.csswg.org/css-conditional-3/#support-definition>
    pub fn eval(&self, context: &ParserContext) -> bool {
        debug_assert_eq!(context.rule_type(), CssRuleType::Style);

        let mut input = ParserInput::new(&self.0);
        let mut input = Parser::new(&mut input);
        input.parse_entirely(|input| -> Result<(), CssParseError<()>> {
            let prop = input.expect_ident().unwrap().as_ref().to_owned();
            input.expect_colon().unwrap();

            let id = PropertyId::parse(&prop)
                .map_err(|_| input.new_custom_error(()))?;

            let mut declarations = SourcePropertyDeclaration::new();
            input.parse_until_before(Delimiter::Bang, |input| {
                PropertyDeclaration::parse_into(&mut declarations, id, prop.into(), &context, input)
                    .map_err(|_| input.new_custom_error(()))
            })?;
            let _ = input.try(parse_important);
            Ok(())
        }).is_ok()
    }
}
<commit_msg>Auto merge of #19321 - emilio:supports-dumb, r=SimonSapin<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! [@supports rules](https://drafts.csswg.org/css-conditional-3/#at-supports)

use cssparser::{Delimiter, parse_important, Parser, SourceLocation, Token};
use cssparser::{ParseError as CssParseError, ParserInput};
#[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
use parser::ParserContext;
use properties::{PropertyId, PropertyDeclaration, SourcePropertyDeclaration};
use selectors::parser::SelectorParseErrorKind;
use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::{ToCss, ParseError};
use stylesheets::{CssRuleType, CssRules};

/// An [`@supports`][supports] rule.
///
/// [supports]: https://drafts.csswg.org/css-conditional-3/#at-supports
#[derive(Debug)]
pub struct SupportsRule {
    /// The parsed condition
    pub condition: SupportsCondition,
    /// Child rules
    pub rules: Arc<Locked<CssRules>>,
    /// The result of evaluating the condition
    pub enabled: bool,
    /// The line and column of the rule's source code.
    pub source_location: SourceLocation,
}

impl SupportsRule {
    /// Measure heap usage.
    #[cfg(feature = "gecko")]
    pub fn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize {
        // Measurement of other fields may be added later.
        self.rules.unconditional_shallow_size_of(ops) +
            self.rules.read_with(guard).size_of(guard, ops)
    }
}

impl ToCssWithGuard for SupportsRule {
    fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
    where W: fmt::Write {
        dest.write_str("@supports ")?;
        self.condition.to_css(dest)?;
        self.rules.read_with(guard).to_css_block(guard, dest)
    }
}

impl DeepCloneWithLock for SupportsRule {
    fn deep_clone_with_lock(
        &self,
        lock: &SharedRwLock,
        guard: &SharedRwLockReadGuard,
        params: &DeepCloneParams,
    ) -> Self {
        let rules = self.rules.read_with(guard);
        SupportsRule {
            condition: self.condition.clone(),
            rules: Arc::new(lock.wrap(rules.deep_clone_with_lock(lock, guard, params))),
            enabled: self.enabled,
            source_location: self.source_location.clone(),
        }
    }
}

/// An @supports condition
///
/// <https://drafts.csswg.org/css-conditional-3/#at-supports>
#[derive(Clone, Debug)]
pub enum SupportsCondition {
    /// `not (condition)`
    Not(Box<SupportsCondition>),
    /// `(condition)`
    Parenthesized(Box<SupportsCondition>),
    /// `(condition) and (condition) and (condition) ..`
    And(Vec<SupportsCondition>),
    /// `(condition) or (condition) or (condition) ..`
    Or(Vec<SupportsCondition>),
    /// `property-ident: value` (value can be any tokens)
    Declaration(Declaration),
    /// `(any tokens)` or `func(any tokens)`
    FutureSyntax(String),
}

impl SupportsCondition {
    /// Parse a condition
    ///
    /// <https://drafts.csswg.org/css-conditional/#supports_condition>
    pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<SupportsCondition, ParseError<'i>> {
        if let Ok(_) = input.try(|i| i.expect_ident_matching("not")) {
            let inner = SupportsCondition::parse_in_parens(input)?;
            return Ok(SupportsCondition::Not(Box::new(inner)));
        }

        let in_parens = SupportsCondition::parse_in_parens(input)?;

        let location = input.current_source_location();
        let (keyword, wrapper) = match input.next() {
            Err(_) => {
                // End of input
                return Ok(in_parens)
            }
            Ok(&Token::Ident(ref ident)) => {
                match_ignore_ascii_case! { &ident,
                    "and" => ("and", SupportsCondition::And as fn(_) -> _),
                    "or" => ("or", SupportsCondition::Or as fn(_) -> _),
                    _ => return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone())))
                }
            }
            Ok(t) => return Err(location.new_unexpected_token_error(t.clone()))
        };

        let mut conditions = Vec::with_capacity(2);
        conditions.push(in_parens);
        loop {
            conditions.push(SupportsCondition::parse_in_parens(input)?);
            if input.try(|input| input.expect_ident_matching(keyword)).is_err() {
                // Did not find the expected keyword.
                // If we found some other token,
                // it will be rejected by `Parser::parse_entirely` somewhere up the stack.
                return Ok(wrapper(conditions))
            }
        }
    }

    /// <https://drafts.csswg.org/css-conditional-3/#supports_condition_in_parens>
    fn parse_in_parens<'i, 't>(input: &mut Parser<'i, 't>) -> Result<SupportsCondition, ParseError<'i>> {
        // Whitespace is normally taken care of in `Parser::next`,
        // but we want to not include it in `pos` for the SupportsCondition::FutureSyntax cases.
        while input.try(Parser::expect_whitespace).is_ok() {}
        let pos = input.position();
        let location = input.current_source_location();
        // FIXME: remove clone() when lifetimes are non-lexical
        match input.next()?.clone() {
            Token::ParenthesisBlock => {
                let nested = input.try(|input| {
                    input.parse_nested_block(|i| parse_condition_or_declaration(i))
                });
                if nested.is_ok() {
                    return nested;
                }
            }
            Token::Function(_) => {}
            t => return Err(location.new_unexpected_token_error(t)),
        }
        input.parse_nested_block(|i| consume_any_value(i))?;
        Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
    }

    /// Evaluate a supports condition
    pub fn eval(&self, cx: &ParserContext) -> bool {
        match *self {
            SupportsCondition::Not(ref cond) => !cond.eval(cx),
            SupportsCondition::Parenthesized(ref cond) => cond.eval(cx),
            SupportsCondition::And(ref vec) => vec.iter().all(|c| c.eval(cx)),
            SupportsCondition::Or(ref vec) => vec.iter().any(|c| c.eval(cx)),
            SupportsCondition::Declaration(ref decl) => decl.eval(cx),
            SupportsCondition::FutureSyntax(_) => false
        }
    }
}

/// supports_condition | declaration
/// <https://drafts.csswg.org/css-conditional/#dom-css-supports-conditiontext-conditiontext>
pub fn parse_condition_or_declaration<'i, 't>(input: &mut Parser<'i, 't>)
                                              -> Result<SupportsCondition, ParseError<'i>> {
    if let Ok(condition) = input.try(SupportsCondition::parse) {
        Ok(SupportsCondition::Parenthesized(Box::new(condition)))
    } else {
        Declaration::parse(input).map(SupportsCondition::Declaration)
    }
}

impl ToCss for SupportsCondition {
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
        where W: fmt::Write,
    {
        match *self {
            SupportsCondition::Not(ref cond) => {
                dest.write_str("not ")?;
                cond.to_css(dest)
            }
            SupportsCondition::Parenthesized(ref cond) => {
                dest.write_str("(")?;
                cond.to_css(dest)?;
                dest.write_str(")")
            }
            SupportsCondition::And(ref vec) => {
                let mut first = true;
                for cond in vec {
                    if !first {
                        dest.write_str(" and ")?;
                    }
                    first = false;
                    cond.to_css(dest)?;
                }
                Ok(())
            }
            SupportsCondition::Or(ref vec) => {
                let mut first = true;
                for cond in vec {
                    if !first {
                        dest.write_str(" or ")?;
                    }
                    first = false;
                    cond.to_css(dest)?;
                }
                Ok(())
            }
            SupportsCondition::Declaration(ref decl) => {
                dest.write_str("(")?;
                decl.to_css(dest)?;
                dest.write_str(")")
            }
            SupportsCondition::FutureSyntax(ref s) => dest.write_str(&s),
        }
    }
}

#[derive(Clone, Debug)]
/// A possibly-invalid property declaration
pub struct Declaration(pub String);

impl ToCss for Declaration {
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
        dest.write_str(&self.0)
    }
}

/// <https://drafts.csswg.org/css-syntax-3/#typedef-any-value>
fn consume_any_value<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), ParseError<'i>> {
    input.expect_no_error_token().map_err(|err| err.into())
}

impl Declaration {
    /// Parse a declaration
    pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Declaration, ParseError<'i>> {
        let pos = input.position();
        input.expect_ident()?;
        input.expect_colon()?;
        consume_any_value(input)?;
        Ok(Declaration(input.slice_from(pos).to_owned()))
    }

    /// Determine if a declaration parses
    ///
    /// <https://drafts.csswg.org/css-conditional-3/#support-definition>
    pub fn eval(&self, context: &ParserContext) -> bool {
        debug_assert_eq!(context.rule_type(), CssRuleType::Style);

        let mut input = ParserInput::new(&self.0);
        let mut input = Parser::new(&mut input);
        input.parse_entirely(|input| -> Result<(), CssParseError<()>> {
            let prop = input.expect_ident_cloned().unwrap();
            input.expect_colon().unwrap();

            let id = PropertyId::parse(&prop)
                .map_err(|_| input.new_custom_error(()))?;

            let mut declarations = SourcePropertyDeclaration::new();
            input.parse_until_before(Delimiter::Bang, |input| {
                PropertyDeclaration::parse_into(&mut declarations, id, prop, &context, input)
                    .map_err(|_| input.new_custom_error(()))
            })?;
            let _ = input.try(parse_important);
            Ok(())
        }).is_ok()
    }
}
<|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_s3::{Client, Error, Region, PKG_VERSION};
use structopt::StructOpt;

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

    /// The name of the source bucket.
    #[structopt(short, long)]
    source: String,

    /// The name of the destination bucket.
    #[structopt(short, long)]
    destination: String,

    /// The object to delete.
    #[structopt(short, long)]
    key: String,

    /// The new name of the object in the destination bucket.
    #[structopt(short, long)]
    name: Option<String>,

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

// Copy an object from one bucket to another.
// snippet-start:[bin.rust.copy-object]
async fn cp_object(
    client: &Client,
    source: &str,
    destination: &str,
    key: &str,
    new_name: &str,
) -> Result<(), Error> {
    let mut src: String = "".to_owned();
    src.push_str(source);
    src.push('/');
    src.push_str(key);

    client
        .copy_object()
        .bucket(destination)
        .copy_source(src)
        .key(new_name)
        .send()
        .await?;

    println!("Object copied.");

    Ok(())
}
// snippet-end:[bin.rust.copy-object]

/// Copies an object from one Amazon S3 bucket to another.
/// # Arguments
///
/// * `-s SOURCE` - The name of the source bucket.
/// * `-d DESTINATION` - The name of the destination bucket.
/// * `-k KEY` - The name of the object to copy.
/// * `-n NAME` - The new name of the object in the destination bucket.
///   If not supplied, the name remains the same.
/// * `[-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> {
    tracing_subscriber::fmt::init();

    let Opt {
        region,
        source,
        destination,
        key,
        name,
        verbose,
    } = Opt::from_args();

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

    println!();

    let new_name: String;

    if name == None {
        new_name = key.clone();
    } else {
        new_name = name.unwrap().clone();
    }

    if verbose {
        println!("S3 client version:  {}", PKG_VERSION);
        println!(
            "Region:             {}",
            region_provider.region().await.unwrap().as_ref()
        );
        println!("Source bucket:      {}", &source);
        println!("Destination bucket: {}", &destination);
        println!("Source key:         {}", &key);
        println!("Destination key:    {}", &new_name);
        println!();
    }

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

    cp_object(&client, &source, &destination, &key, &new_name).await
}
<commit_msg>Updated CopyObject code example in Rust based of feedback.<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_s3::{Client, Error, Region, PKG_VERSION};
use structopt::StructOpt;

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

    /// The name of the source bucket.
    #[structopt(short, long)]
    source: String,

    /// The name of the destination bucket.
    #[structopt(short, long)]
    destination: String,

    /// The object to delete.
    #[structopt(short, long)]
    key: String,

    /// The new name of the object in the destination bucket.
    #[structopt(short, long)]
    name: Option<String>,

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

// Copy an object from one bucket to another.
// snippet-start:[bin.rust.copy-object]
async fn cp_object(
    client: &Client,
    source_bucket: &str,
    destination_bucket: &str,
    source_object: &str,
    destination_object: &str,
) -> Result<(), Error> {
    // Create source of object:
    //   source-bucket-name/source-object-name
    let mut source_bucket_and_object: String = "".to_owned();
    source_bucket_and_object.push_str(source_bucket);
    source_bucket_and_object.push('/');
    source_bucket_and_object.push_str(source_object);

    client
        .copy_object()
        .copy_source(source_bucket_and_object)
        .bucket(destination_bucket)
        .key(destination_object)
        .send()
        .await?;

    println!("Object copied.");

    Ok(())
}
// snippet-end:[bin.rust.copy-object]

/// Copies an object from one Amazon S3 bucket to another.
/// # Arguments
///
/// * `-s SOURCE` - The name of the source bucket.
/// * `-d DESTINATION` - The name of the destination bucket.
/// * `-k KEY` - The name of the object to copy.
/// * `-n NAME` - The new name of the object in the destination bucket.
///   If not supplied, the name remains the same.
/// * `[-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> {
    tracing_subscriber::fmt::init();

    let Opt {
        region,
        source,
        destination,
        key,
        name,
        verbose,
    } = Opt::from_args();

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

    println!();

    let new_name: String;

    if name == None {
        new_name = key.clone();
    } else {
        new_name = name.unwrap();
    }

    if verbose {
        println!("S3 client version:  {}", PKG_VERSION);
        println!(
            "Region:             {}",
            region_provider.region().await.unwrap().as_ref()
        );
        println!("Source bucket:      {}", &source);
        println!("Source key:         {}", &key);
        println!("Destination bucket: {}", &destination);
        println!("Destination key:    {}", &new_name);
        println!();
    }

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

    cp_object(&client, &source, &destination, &key, &new_name).await
}
<|endoftext|>


<commit_before>extern crate futures;
extern crate rusoto_core;
extern crate rusoto_dynamodb;
extern crate tokio_core;

use futures::future::Future;
use tokio_core::reactor::Core;
use rusoto_core::Region;
use rusoto_dynamodb::{
    AttributeDefinition, AttributeValue, CreateTableInput, DynamoDb, DynamoDbClient, GetItemInput,
    KeySchemaElement, ProvisionedThroughput, UpdateItemInput,
};
use std::collections::HashMap;

// Use local dynamodb for testing this out
fn main() {
    // Create custom Region
    let region = Region::Custom {
        name: "us-east-1".to_owned(),
        endpoint: "http://localhost:8000".to_owned(),
    };

    let client = DynamoDbClient::new(region);

    // future for creating a table
    let attribute_def = AttributeDefinition {
        attribute_name: "foo_name".to_string(),
        attribute_type: "S".to_string(),
    };
    let k_schema = KeySchemaElement {
        attribute_name: "foo_name".to_string(),
        key_type: "HASH".to_string(), // case sensitive
    };
    let p_throughput = ProvisionedThroughput {
        read_capacity_units: 1,
        write_capacity_units: 1,
    };
    let make_table_request = CreateTableInput {
        table_name: "a-testing-table".to_string(),
        attribute_definitions: vec![attribute_def],
        key_schema: vec![k_schema],
        provisioned_throughput: p_throughput,
        ..Default::default()
    };

    let create_table_future = client
        .create_table(make_table_request)
        .map(|_r| ())
        .map_err(|e| panic!("no: {}", e));

    // future for upserting an entry
    let mut item = HashMap::new();
    item.insert(
        "foo_name".to_string(),
        AttributeValue {
            s: Some("baz".to_string()),
            ..Default::default()
        },
    );
    let add_item = UpdateItemInput {
        key: item.clone(), // cloning so we can reuse the item we're looking for in the GET part
        table_name: "a-testing-table".to_string(),
        ..Default::default()
    };

    let upsert_item_future = client
        .update_item(add_item)
        .map(|_| ())
        .map_err(|e| panic!("Couldn't upsert item: {}", e));

    // future for getting the entry
    let get_item_request = GetItemInput {
        key: item,
        table_name: "a-testing-table".to_string(),
        ..Default::default()
    };
    let item_from_dynamo_future = client.get_item(get_item_request);

    // tie them together
    let chained_futures = create_table_future.and_then(|_| {
        upsert_item_future.and_then(|_| {
            item_from_dynamo_future.map(|i| println!("Got item back: {:?}", i)).map_err(|e| panic!("Couldn't get item: {}", e))
        })
    });

    // This tokio core will run our futures to completion:
    let mut core = Core::new().unwrap();

    // run 'em
    core.run(chained_futures).unwrap();
}
<commit_msg>Works again<commit_after>extern crate futures;
extern crate rusoto_core;
extern crate rusoto_dynamodb;
extern crate tokio_core;

use futures::future::Future;
use tokio_core::reactor::Core;
use rusoto_core::Region;
use rusoto_dynamodb::{
    AttributeDefinition, AttributeValue, CreateTableInput, DynamoDb, DynamoDbClient, GetItemInput,
    KeySchemaElement, ProvisionedThroughput, UpdateItemInput, CreateTableOutput, CreateTableError,
    UpdateItemOutput, UpdateItemError, GetItemOutput, GetItemError
};
use std::collections::HashMap;

fn get_dynamodb_local_client() -> DynamoDbClient {
    // Create custom Region
    let region = Region::Custom {
        name: "us-east-1".to_owned(),
        endpoint: "http://localhost:8000".to_owned(),
    };

    DynamoDbClient::new(region)
}

fn make_create_table_future(client: &DynamoDbClient) -> impl Future<Item = CreateTableOutput, Error = CreateTableError> {
    let attribute_def = AttributeDefinition {
        attribute_name: "foo_name".to_string(),
        attribute_type: "S".to_string(),
    };
    let k_schema = KeySchemaElement {
        attribute_name: "foo_name".to_string(),
        key_type: "HASH".to_string(), // case sensitive
    };
    let p_throughput = ProvisionedThroughput {
        read_capacity_units: 1,
        write_capacity_units: 1,
    };
    let make_table_request = CreateTableInput {
        table_name: "a-testing-table".to_string(),
        attribute_definitions: vec![attribute_def],
        key_schema: vec![k_schema],
        provisioned_throughput: p_throughput,
        ..Default::default()
    };

    client
        .create_table(make_table_request)
        .map(|r| r)
        .map_err(|e| e)
}

fn make_upsert_item_future(client: &DynamoDbClient, item: &HashMap<String, AttributeValue>) -> impl Future<Item = UpdateItemOutput, Error = UpdateItemError> {
    let add_item = UpdateItemInput {
        key: item.clone(),
        table_name: "a-testing-table".to_string(),
        ..Default::default()
    };

    client
        .update_item(add_item)
        .map(|r| r)
        .map_err(|e| e)
}

fn make_get_item_future(client: &DynamoDbClient, item: &HashMap<String, AttributeValue>) -> impl Future<Item = GetItemOutput, Error = GetItemError> {
    // future for getting the entry
    let get_item_request = GetItemInput {
        key: item.clone(),
        table_name: "a-testing-table".to_string(),
        ..Default::default()
    };
    client.get_item(get_item_request)
}

fn make_item() -> HashMap<String, AttributeValue> {
    let item_key = "foo_name";
    let mut item = HashMap::new();
    item.insert(
        item_key.to_string(),
        AttributeValue {
            s: Some("baz".to_string()),
            ..Default::default()
        },
    );

    item
}

// Use local dynamodb for testing this out
fn main() {
    let item = make_item();

    let client = get_dynamodb_local_client();

    let create_table_future = make_create_table_future(&client);

    let upsert_item_future = make_upsert_item_future(&client, &item);

    let item_from_dynamo_future = make_get_item_future(&client, &item);

    // tie them together
    let chained_futures = create_table_future
        .map(|_| {
            upsert_item_future.and_then(|_| {
                item_from_dynamo_future.map(|i| println!("Got item back: {:?}", i)).map_err(|e| panic!("Couldn't get item: {}", e))
            })
            })
        .map_err(|e| panic!("noooo {}", e));

    // This tokio core will run our futures to completion:
    let mut core = Core::new().unwrap();

    // run 'em
    match core.run(chained_futures).unwrap().wait() {
        Ok(_) => println!("Everything worked!"),
        Err(e) => println!("Error completing futures: {}", e),
    }
}
<|endoftext|>


<commit_before>#![feature(test)]

extern crate test;
extern crate ratelimit_meter;

use ratelimit_meter::{Allower, GCRA, Threadsafe, Limiter, Decider};
use std::time::{Instant, Duration};

#[bench]
fn bench_gcra(b: &mut test::Bencher) {
    let mut gcra = Limiter::new().capacity(50).weight(1).build::<GCRA>().unwrap();
    let now = Instant::now();
    let ms = Duration::from_millis(20);
    let mut i = 0;
    b.iter(|| {
        i += 1;
        gcra.test_and_update(now + (ms * i)).unwrap();
    });
}

#[bench]
fn bench_allower(b: &mut test::Bencher) {
    let mut allower = Limiter::new().capacity(50).weight(1).build::<Allower>().unwrap();
    b.iter(|| allower.check().unwrap());
}

#[bench]
fn bench_threadsafe_gcra(b: &mut test::Bencher) {
    let mut gcra = Limiter::new().capacity(50).weight(1).build::<Threadsafe<GCRA>>().unwrap();
    let now = Instant::now();
    let ms = Duration::from_millis(20);
    let mut i = 0;
    b.iter(|| {
        i += 1;
        gcra.test_and_update(now + (ms * i)).unwrap();
    });
}

#[bench]
fn bench_threadsafe_allower(b: &mut test::Bencher) {
    let mut allower = Limiter::new().capacity(50).weight(1).build::<Threadsafe<Allower>>().unwrap();
    b.iter(|| allower.check());
}
<commit_msg>Fix benchmarks, add a (non-functional) one for multithreading<commit_after>#![feature(test)]

extern crate test;
extern crate ratelimit_meter;

use ratelimit_meter::{GCRA, Threadsafe, Limiter, Decider};
use ratelimit_meter::example_algorithms::Allower;
use std::time::{Instant, Duration};
use std::thread;

#[bench]
fn bench_gcra(b: &mut test::Bencher) {
    let mut gcra = Limiter::new().capacity(50).weight(1).build::<GCRA>().unwrap();
    let now = Instant::now();
    let ms = Duration::from_millis(20);
    let mut i = 0;
    b.iter(|| {
        i += 1;
        gcra.check_at(now + (ms * i)).unwrap();
    });
}

#[bench]
fn bench_allower(b: &mut test::Bencher) {
    let mut allower = Limiter::new().capacity(50).weight(1).build::<Allower>().unwrap();
    b.iter(|| allower.check().unwrap());
}

#[bench]
fn bench_threadsafe_gcra(b: &mut test::Bencher) {
    let mut gcra = Limiter::new().capacity(50).weight(1).build::<Threadsafe<GCRA>>().unwrap();
    let now = Instant::now();
    let ms = Duration::from_millis(20);
    let mut i = 0;
    b.iter(|| {
        i += 1;
        gcra.check_at(now + (ms * i)).unwrap();
    });
}

#[bench]
fn bench_threadsafe_allower(b: &mut test::Bencher) {
    let mut allower = Limiter::new().capacity(50).weight(1).build::<Threadsafe<Allower>>().unwrap();
    b.iter(|| allower.check());
}

// This one doesn't seem to actually do a thing & I can't quite figure out why /:
#[bench]
fn bench_multithreading_potentially_buggy(b: &mut test::Bencher) {
    let mut lim = Limiter::new()
        .capacity(20)
        .weight(1)
        .build::<Threadsafe<GCRA>>()
        .unwrap();
    let now = Instant::now();
    let ms = Duration::from_millis(20);
    let mut children = vec![];

    lim.check_at(now).unwrap();
    for _i in 0..20 {
        let mut lim = lim.clone();
        let mut b = b.clone();
        children.push(thread::spawn(move || {
            let mut i = 0;
            b.iter(|| {
                i += 1;
                lim.check_at(now + (ms * i)).unwrap();
            });
        }));
    }
    for child in children {
        child.join().unwrap();
    }
}
<|endoftext|>


<commit_before>//! Serializer and deserializer structure for a data.
//!
//! This module is intended for transforming incoming data (which are
//! `tungstenite::Message` objects) to JSON objects and preparing responses
//! for client before sending through transmitters.
//!

use std::sync::{Arc};

use error::{Result, PathfinderError};

use json::{parse as parse_json, JsonValue};
use tungstenite::protocol::{Message};

/// Type alias for JSON object
pub type JsonMessage = Arc<Box<JsonValue>>;

/// A specialized struct for deserializing incoming messages into JSON and
/// serializing responses into `tungstenite::Message` objects, so, that they
/// could be send to a client.
///
/// # Examples
///
/// Serializing a JSON object into message:
///
/// ```
/// use engine::{Serializer};
///
/// let instance = Serializer::new();
/// let json = object!{"test" => "serialize"};
/// let response = json.dump();
/// println!("{:?}", instance.serialize(response))
/// ```
///
/// Deserializing a message to JSON object:
///
/// ```
/// use engine::{Serializer};
/// use tungstenite::{Message};
///
/// let json = object!{"test" => "serialize"};
/// let message = Message::Text(json.dump());
/// let instance = Serializer::new();
/// println!("{:?}", instance.deserialize(&message))
/// ```
///
pub struct Serializer;

impl Serializer {
    /// Returns a new instance of `Serializer`.
    pub fn new() -> Serializer {
        Serializer {}
    }

    /// Converts a UTF-8 encoded `std::string::String` into an instance of
    /// the `tungstenite::Message` type, so that this message can be send to
    /// a client.
    pub fn serialize(&self, message: String) -> Result<Message> {
        Ok(Message::Text(message))
    }

    /// Transforms an instance of the `tungstenite::Message` type into JSON object.
    pub fn deserialize(&self, message: &Message) -> Result<JsonMessage> {
         let text_message = self.parse_into_text(message)?;
         let mut json_message = self.parse_into_json(text_message.as_str())?;
         json_message = self.validate_json(json_message)?;
         Ok(json_message)
    }

    /// Parses an instance of the `tungstenite::Message` type and returns a UTF-8
    /// encoded string of the `std::string::String` type.
    fn parse_into_text(&self, message: &Message) -> Result<String> {
        match message.clone().into_text() {
            Ok(text_message) => Ok(text_message),
            Err(err) => {
                let formatted_message = format!("{}", err);
                return Err(PathfinderError::DecodingError(formatted_message))
            }
        }
    }

    /// Parses a UTF-8 string and converts it into JSON object.
    fn parse_into_json(&self, message: &str) -> Result<JsonMessage> {
        match parse_json(message) {
            Ok(message) => Ok(Arc::new(Box::new(message))),
            Err(err) => {
                let formatted_message = format!("{}", err);
                return Err(PathfinderError::DecodingError(formatted_message))
            }
        }
    }

    /// Validates a JSON object on required fields and values.
    fn validate_json(&self, json: JsonMessage) -> Result<JsonMessage> {
        if json["url"].is_null() {
            let error_message = String::from("Key `url` is missing or value is `null`");
            return Err(PathfinderError::DecodingError(error_message));
        }

        if json.has_key("microservice") {
            let error_message = String::from("Key `microservice` must be not specified");
            return Err(PathfinderError::DecodingError(error_message));
        }

        Ok(json)
    }
}

#[cfg(test)]
mod tests {
    use super::{Serializer};
    use super::super::json::{Null};
    use super::super::tungstenite::{Message};

    #[test]
    fn test_serialize_returns_a_new_message_instance() {
        let instance = Serializer::new();
        let dictionary = object!{"test" => "value"};
        let test_string = dictionary.dump();
        let result = instance.serialize(test_string);

        assert_eq!(result.is_ok(), true);
        assert_eq!(result.unwrap().is_text(), true)
    }

    #[test]
    fn test_deserialize_returns_valid_json_object() {
        let dictionary = object!{"url" => "test"};
        let message = Message::Text(dictionary.dump());
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_ok(), true);
        let unwrapped_result = result.unwrap();
        assert_eq!(unwrapped_result.has_key("url"), true);
        assert_eq!(unwrapped_result["url"], dictionary["url"]);
    }

    #[test]
    fn test_deserialize_returns_decoding_error_while_parsed_into_text() {
        let data = vec![0, 159, 146, 150];
        let message = Message::Binary(data);
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: UTF-8 encoding error"
        )
    }

    #[test]
    fn test_deserialize_returns_decoding_error_while_parsed_into_json() {
        let invalid_json = String::from(r#"{"url": "test""#);
        let message = Message::Text(invalid_json);
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: Unexpected end of JSON"
        )
    }

    #[test]
    fn test_deserialize_returns_validation_error_for_missing_url_key_in_json() {
        let dictionary = object!{"test" => "value"};
        let message = Message::Text(dictionary.dump());
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: Key `url` is missing or value is `null`"
        )
    }

    #[test]
    fn test_deserialize_returns_validation_error_for_invalid_url_value_in_json() {
        let dictionary = object!{"url" => Null};
        let message = Message::Text(dictionary.dump());
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: Key `url` is missing or value is `null`"
        )
    }

    #[test]
    fn test_deserialize_returns_validation_error_for_the_specified_matchmaking_key_in_json() {
        let dictionary = object!{"url" => "value", "microservice" => "some microservice"};
        let message = Message::Text(dictionary.dump());
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: Key `microservice` must be not specified"
        )
    }
}
<commit_msg>Fixed tests for the Serializer struct<commit_after>//! Serializer and deserializer structure for a data.
//!
//! This module is intended for transforming incoming data (which are
//! `tungstenite::Message` objects) to JSON objects and preparing responses
//! for client before sending through transmitters.
//!

use std::sync::{Arc};

use error::{Result, PathfinderError};

use json::{parse as parse_json, JsonValue};
use tungstenite::protocol::{Message};

/// Type alias for JSON object
pub type JsonMessage = Arc<Box<JsonValue>>;

/// A specialized struct for deserializing incoming messages into JSON and
/// serializing responses into `tungstenite::Message` objects, so, that they
/// could be send to a client.
///
/// # Examples
///
/// Serializing a JSON object into message:
///
/// ```
/// use engine::{Serializer};
///
/// let instance = Serializer::new();
/// let json = object!{"test" => "serialize"};
/// let response = json.dump();
/// println!("{:?}", instance.serialize(response))
/// ```
///
/// Deserializing a message to JSON object:
///
/// ```
/// use engine::{Serializer};
/// use tungstenite::{Message};
///
/// let json = object!{"test" => "serialize"};
/// let message = Message::Text(json.dump());
/// let instance = Serializer::new();
/// println!("{:?}", instance.deserialize(&message))
/// ```
///
pub struct Serializer;

impl Serializer {
    /// Returns a new instance of `Serializer`.
    pub fn new() -> Serializer {
        Serializer {}
    }

    /// Converts a UTF-8 encoded `std::string::String` into an instance of
    /// the `tungstenite::Message` type, so that this message can be send to
    /// a client.
    pub fn serialize(&self, message: String) -> Result<Message> {
        Ok(Message::Text(message))
    }

    /// Transforms an instance of the `tungstenite::Message` type into JSON object.
    pub fn deserialize(&self, message: &Message) -> Result<JsonMessage> {
         let text_message = self.parse_into_text(message)?;
         let mut json_message = self.parse_into_json(text_message.as_str())?;
         json_message = self.validate_json(json_message)?;
         Ok(json_message)
    }

    /// Parses an instance of the `tungstenite::Message` type and returns a UTF-8
    /// encoded string of the `std::string::String` type.
    fn parse_into_text(&self, message: &Message) -> Result<String> {
        match message.clone().into_text() {
            Ok(text_message) => Ok(text_message),
            Err(err) => {
                let formatted_message = format!("{}", err);
                return Err(PathfinderError::DecodingError(formatted_message))
            }
        }
    }

    /// Parses a UTF-8 string and converts it into JSON object.
    fn parse_into_json(&self, message: &str) -> Result<JsonMessage> {
        match parse_json(message) {
            Ok(message) => Ok(Arc::new(Box::new(message))),
            Err(err) => {
                let formatted_message = format!("{}", err);
                return Err(PathfinderError::DecodingError(formatted_message))
            }
        }
    }

    /// Validates a JSON object on required fields and values.
    fn validate_json(&self, json: JsonMessage) -> Result<JsonMessage> {
        if json["url"].is_null() {
            let error_message = String::from("The `url` field is missing or value is `null`");
            return Err(PathfinderError::DecodingError(error_message));
        }

        if json.has_key("microservice") {
            let error_message = String::from("The `microservice` field must not be specified");
            return Err(PathfinderError::DecodingError(error_message));
        }

        Ok(json)
    }
}

#[cfg(test)]
mod tests {
    use super::{Serializer};
    use super::super::json::{Null};
    use super::super::tungstenite::{Message};

    #[test]
    fn test_serialize_returns_a_new_message_instance() {
        let instance = Serializer::new();
        let dictionary = object!{"test" => "value"};
        let test_string = dictionary.dump();
        let result = instance.serialize(test_string);

        assert_eq!(result.is_ok(), true);
        assert_eq!(result.unwrap().is_text(), true)
    }

    #[test]
    fn test_deserialize_returns_valid_json_object() {
        let dictionary = object!{"url" => "test"};
        let message = Message::Text(dictionary.dump());
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_ok(), true);
        let unwrapped_result = result.unwrap();
        assert_eq!(unwrapped_result.has_key("url"), true);
        assert_eq!(unwrapped_result["url"], dictionary["url"]);
    }

    #[test]
    fn test_deserialize_returns_decoding_error_while_parsed_into_text() {
        let data = vec![0, 159, 146, 150];
        let message = Message::Binary(data);
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: UTF-8 encoding error"
        )
    }

    #[test]
    fn test_deserialize_returns_decoding_error_while_parsed_into_json() {
        let invalid_json = String::from(r#"{"url": "test""#);
        let message = Message::Text(invalid_json);
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: Unexpected end of JSON"
        )
    }

    #[test]
    fn test_deserialize_returns_validation_error_for_missing_url_key_in_json() {
        let dictionary = object!{"test" => "value"};
        let message = Message::Text(dictionary.dump());
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: The `url` field is missing or value is `null`"
        )
    }

    #[test]
    fn test_deserialize_returns_validation_error_for_invalid_url_value_in_json() {
        let dictionary = object!{"url" => Null};
        let message = Message::Text(dictionary.dump());
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: The `url` field is missing or value is `null`"
        )
    }

    #[test]
    fn test_deserialize_returns_validation_error_for_the_specified_matchmaking_key_in_json() {
        let dictionary = object!{"url" => "value", "microservice" => "some microservice"};
        let message = Message::Text(dictionary.dump());
        let instance = Serializer::new();
        let result = instance.deserialize(&message);

        assert_eq!(result.is_err(), true);
        assert_eq!(
            format!("{}", result.unwrap_err()),
            "Decoding error: The `microservice` field must not be specified"
        )
    }
}
<|endoftext|>


<commit_before>// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! # pest. The Elegant Parser
//!
//! pest is a general purpose parser written in Rust with a focus on accessibility, correctness,
//! and performance. It uses parsing expression grammars (or [PEG]) as input, which are similar in
//! spirit to regular expressions, but which offer the enhanced expressivity needed to parse
//! complex languages.
//!
//! [PEG]: https://en.wikipedia.org/wiki/Parsing_expression_grammar
//!
//! ## Getting started
//!
//! The recommended way to start parsing with pest is to read the official [book].
//!
//! Other helpful resources:
//!
//! * API reference on [docs.rs]
//! * play with grammars and share them on our [fiddle]
//! * leave feedback, ask questions, or greet us on [Gitter]
//!
//! [book]: https://pest-parser.github.io/book
//! [docs.rs]: https://docs.rs/pest
//! [fiddle]: https://pest-parser.github.io/#editor
//! [Gitter]: https://gitter.im/dragostis/pest
//!
//! ## `.pest` files
//!
//! Grammar definitions reside in custom `.pest` files located in the `src` directory. Their path is
//! relative to `src` and is specified between the `derive` attribute and empty `struct` that
//! `Parser` will be derived on.
//!
//! ```ignore
//! #[derive(Parser)]
//! #[grammar = "path/to/my_grammar.pest"] // relative to src
//! struct MyParser;
//! ```
//!
//! ## Grammar
//!
//! A grammar is a series of rules separated by whitespace, possibly containing comments.
//!
//! ### Comments
//!
//! Comments start with `//` and end at the end of the line.
//!
//! ```ignore
//! // a comment
//! ```
//!
//! ### Rules
//!
//! Rules have the following form:
//!
//! ```ignore
//! name = optional_modifier { expression }
//! ```
//!
//! The name of the rule is formed from alphanumeric characters or `_` with the condition that the
//! first character is not a digit and is used to create token pairs. When the rule starts being
//! parsed, the starting part of the token is being produced, with the ending part being produced
//! when the rule finishes parsing.
//!
//! The following token pair notation `a(b(), c())` denotes the tokens: start `a`, start `b`, end
//! `b`, start `c`, end `c`, end `a`.
//!
//! #### Modifiers
//!
//! Modifiers are optional and can be one of `_`, `@`, `$`, or `!`. These modifiers change the
//! behavior of the rules.
//!
//! 1. Silent (`_`)
//!
//!     Silent rules do not create token pairs during parsing, nor are they error-reported.
//!
//!     ```ignore
//!     a = _{ "a" }
//!     b =  { a ~ "b" }
//!     ```
//!
//!     Parsing `"ab"` produces the token pair `b()`.
//!
//! 2. Atomic (`@`)
//!
//!     Atomic rules do not accept whitespace or comments within their expressions and have a
//!     cascading effect on any rule they call. I.e. rules that are not atomic but are called by atomic
//!     rules behave atomically.
//!
//!     Any rules called by atomic rules do not generate token pairs.
//!
//!     ```ignore
//!     a =  { "a" }
//!     b = @{ a ~ "b" }
//!
//!     WHITESPACE = _{ " " }
//!     ```
//!
//!     Parsing `"ab"` produces the token pair `b()`, while `"a   b"` produces an error.
//!
//! 3. Compound-atomic (`$`)
//!
//!     Compound-atomic are identical to atomic rules with the exception that rules called by them are
//!     not forbidden from generating token pairs.
//!
//!     ```ignore
//!     a =  { "a" }
//!     b = ${ a ~ "b" }
//!
//!     WHITESPACE = _{ " " }
//!     ```
//!
//!     Parsing `"ab"` produces the token pairs `b(a())`, while `"a   b"` produces an error.
//!
//! 4. Non-atomic (`!`)
//!
//!     Non-atomic are identical to normal rules with the exception that they stop the cascading effect
//!     of atomic and compound-atomic rules.
//!
//!     ```ignore
//!     a =  { "a" }
//!     b = !{ a ~ "b" }
//!     c = @{ b }
//!
//!     WHITESPACE = _{ " " }
//!     ```
//!
//!     Parsing both `"ab"` and `"a   b"` produce the token pairs `c(a())`.
//!
//! #### Expressions
//!
//! Expressions can be either terminals or non-terminals.
//!
//! 1. Terminals
//!
//!     | Terminal   | Usage                                                          |
//!     |------------|----------------------------------------------------------------|
//!     | `"a"`      | matches the exact string `"a"`                                 |
//!     | `^"a"`     | matches the exact string `"a"` case insensitively (ASCII only) |
//!     | `'a'..'z'` | matches one character between `'a'` and `'z'`                  |
//!     | `a`        | matches rule `a`                                               |
//!
//! Strings and characters follow
//! [Rust's escape mechanisms](https://doc.rust-lang.org/reference/tokens.html#byte-escapes), while
//! identifiers can contain alpha-numeric characters and underscores (`_`), as long as they do not
//! start with a digit.
//!
//! 2. Non-terminals
//!
//!     | Non-terminal          | Usage                                                      |
//!     |-----------------------|------------------------------------------------------------|
//!     | `(e)`                 | matches `e`                                                |
//!     | `e1 ~ e2`             | matches the sequence `e1` `e2`                             |
//!     | e1 \| e2 | matches either `e1` or `e2`                                |
//!     | `e*`                  | matches `e` zero or more times                             |
//!     | `e+`                  | matches `e` one or more times                              |
//!     | `e{n}`                | matches `e` exactly `n` times                              |
//!     | `e{, n}`              | matches `e` at most `n` times                              |
//!     | `e{n,} `              | matches `e` at least `n` times                             |
//!     | `e{m, n}`             | matches `e` between `m` and `n` times inclusively          |
//!     | `e?`                  | optionally matches `e`                                     |
//!     | `&e`                  | matches `e` without making progress                        |
//!     | `!e`                  | matches if `e` doesn't match without making progress       |
//!     | `PUSH(e)`             | matches `e` and pushes it's captured string down the stack |
//!
//!     where `e`, `e1`, and `e2` are expressions.
//!
//! Expressions can modify the stack only if they match the input. For example,
//! if `e1` in the compound expression `e1 | e2` does not match the input, then
//! it does not modify the stack, so `e2` sees the stack in the same state as
//! `e1` did. Repetitions and optionals (`e*`, `e+`, `e{, n}`, `e{n,}`,
//! `e{m,n}`, `e?`) can modify the stack each time `e` matches. The `!e` and `&e`
//! expressions are a special case; they never modify the stack.
//!
//! ## Special rules
//!
//! Special rules can be called within the grammar. They are:
//!
//! * `WHITESPACE` - runs between rules and sub-rules
//! * `COMMENT` - runs between rules and sub-rules
//! * `ANY` - matches exactly one `char`
//! * `SOI` - (start-of-input) matches only when a `Parser` is still at the starting position
//! * `EOI` - (end-of-input) matches only when a `Parser` has reached its end
//! * `POP` - pops a string from the stack and matches it
//! * `POP_ALL` - pops the entire state of the stack and matches it
//! * `PEEK` - peeks a string from the stack and matches it
//! * `PEEK_ALL` - peeks the entire state of the stack and matches it
//! * `DROP` - drops the top of the stack (fails to match if the stack is empty)
//!
//! `WHITESPACE` and `COMMENT` should be defined manually if needed. All other rules cannot be
//! overridden.
//!
//! ## `WHITESPACE` and `COMMENT`
//!
//! When defined, these rules get matched automatically in sequences (`~`) and repetitions
//! (`*`, `+`) between expressions. Atomic rules and those rules called by atomic rules are exempt
//! from this behavior.
//!
//! These rules should be defined so as to match one whitespace character and one comment only since
//! they are run in repetitions.
//!
//! If both `WHITESPACE` and `COMMENT` are defined, this grammar:
//!
//! ```ignore
//! a = { b ~ c }
//! ```
//!
//! is effectively transformed into this one behind the scenes:
//!
//! ```ignore
//! a = { b ~ WHITESPACE* ~ (COMMENT ~ WHITESPACE*)* ~ c }
//! ```
//!
//! ## `PUSH`, `POP`, `DROP`, and `PEEK`
//!
//! `PUSH(e)` simply pushes the captured string of the expression `e` down a stack. This stack can
//! then later be used to match grammar based on its content with `POP` and `PEEK`.
//!
//! `PEEK` always matches the string at the top of stack. So, if the stack contains `["a", "b"]`,
//! the this grammar:
//!
//! ```ignore
//! a = { PEEK }
//! ```
//!
//! is effectively transformed into at parse time:
//!
//! ```ignore
//! a = { "a" }
//! ```
//!
//! `POP` works the same way with the exception that it pops the string off of the stack if the
//! the match worked. With the stack from above, if `POP` matches `"a"`, the stack will be mutated
//! to `["b"]`.
//!
//! `DROP` makes it possible to remove the string at the top of the stack
//! without matching it. If the stack is nonempty, `DROP` drops the top of the
//! stack. If the stack is empty, then `DROP` fails to match.
//!
//! ## `Rule`
//!
//! All rules defined or used in the grammar populate a generated `enum` called `Rule`. This
//! implements `pest`'s `RuleType` and can be used throughout the API.
//!
//! ## `Built-in rules`
//!
//! Pest also comes with a number of built-in rules for convenience. They are:
//!
//! * `ASCII_DIGIT` - matches a numeric character from 0..9
//! * `ASCII_NONZERO_DIGIT` - matches a numeric character from 1..9
//! * `ASCII_BIN_DIGIT` - matches a numeric character from 0..1
//! * `ASCII_OCT_DIGIT` - matches a numeric character from 0..7
//! * `ASCII_HEX_DIGIT` - matches a numeric character from 0..9 or a..f or A..F
//! * `ASCII_ALPHA_LOWER` - matches a character from a..z
//! * `ASCII_ALPHA_UPPER` - matches a character from A..Z
//! * `ASCII_ALPHA` - matches a character from a..z or A..Z
//! * `ASCII_ALPHANUMERIC` - matches a character from a..z or A..Z or 0..9
//! * `ASCII` - matches a character from \x00..\x7f
//! * `NEWLINE` - matches either "\n" or "\r\n" or "\r"

#![doc(html_root_url = "https://docs.rs/pest_derive")]

extern crate pest_generator;
extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_derive(Parser, attributes(grammar))]
pub fn derive_parser(input: TokenStream) -> TokenStream {
    pest_generator::derive_parser(input.into(), true).into()
}
<commit_msg>Fix a small typo in the documentation.<commit_after>// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! # pest. The Elegant Parser
//!
//! pest is a general purpose parser written in Rust with a focus on accessibility, correctness,
//! and performance. It uses parsing expression grammars (or [PEG]) as input, which are similar in
//! spirit to regular expressions, but which offer the enhanced expressivity needed to parse
//! complex languages.
//!
//! [PEG]: https://en.wikipedia.org/wiki/Parsing_expression_grammar
//!
//! ## Getting started
//!
//! The recommended way to start parsing with pest is to read the official [book].
//!
//! Other helpful resources:
//!
//! * API reference on [docs.rs]
//! * play with grammars and share them on our [fiddle]
//! * leave feedback, ask questions, or greet us on [Gitter]
//!
//! [book]: https://pest-parser.github.io/book
//! [docs.rs]: https://docs.rs/pest
//! [fiddle]: https://pest-parser.github.io/#editor
//! [Gitter]: https://gitter.im/dragostis/pest
//!
//! ## `.pest` files
//!
//! Grammar definitions reside in custom `.pest` files located in the `src` directory. Their path is
//! relative to `src` and is specified between the `derive` attribute and empty `struct` that
//! `Parser` will be derived on.
//!
//! ```ignore
//! #[derive(Parser)]
//! #[grammar = "path/to/my_grammar.pest"] // relative to src
//! struct MyParser;
//! ```
//!
//! ## Grammar
//!
//! A grammar is a series of rules separated by whitespace, possibly containing comments.
//!
//! ### Comments
//!
//! Comments start with `//` and end at the end of the line.
//!
//! ```ignore
//! // a comment
//! ```
//!
//! ### Rules
//!
//! Rules have the following form:
//!
//! ```ignore
//! name = optional_modifier { expression }
//! ```
//!
//! The name of the rule is formed from alphanumeric characters or `_` with the condition that the
//! first character is not a digit and is used to create token pairs. When the rule starts being
//! parsed, the starting part of the token is being produced, with the ending part being produced
//! when the rule finishes parsing.
//!
//! The following token pair notation `a(b(), c())` denotes the tokens: start `a`, start `b`, end
//! `b`, start `c`, end `c`, end `a`.
//!
//! #### Modifiers
//!
//! Modifiers are optional and can be one of `_`, `@`, `$`, or `!`. These modifiers change the
//! behavior of the rules.
//!
//! 1. Silent (`_`)
//!
//!     Silent rules do not create token pairs during parsing, nor are they error-reported.
//!
//!     ```ignore
//!     a = _{ "a" }
//!     b =  { a ~ "b" }
//!     ```
//!
//!     Parsing `"ab"` produces the token pair `b()`.
//!
//! 2. Atomic (`@`)
//!
//!     Atomic rules do not accept whitespace or comments within their expressions and have a
//!     cascading effect on any rule they call. I.e. rules that are not atomic but are called by atomic
//!     rules behave atomically.
//!
//!     Any rules called by atomic rules do not generate token pairs.
//!
//!     ```ignore
//!     a =  { "a" }
//!     b = @{ a ~ "b" }
//!
//!     WHITESPACE = _{ " " }
//!     ```
//!
//!     Parsing `"ab"` produces the token pair `b()`, while `"a   b"` produces an error.
//!
//! 3. Compound-atomic (`$`)
//!
//!     Compound-atomic are identical to atomic rules with the exception that rules called by them are
//!     not forbidden from generating token pairs.
//!
//!     ```ignore
//!     a =  { "a" }
//!     b = ${ a ~ "b" }
//!
//!     WHITESPACE = _{ " " }
//!     ```
//!
//!     Parsing `"ab"` produces the token pairs `b(a())`, while `"a   b"` produces an error.
//!
//! 4. Non-atomic (`!`)
//!
//!     Non-atomic are identical to normal rules with the exception that they stop the cascading effect
//!     of atomic and compound-atomic rules.
//!
//!     ```ignore
//!     a =  { "a" }
//!     b = !{ a ~ "b" }
//!     c = @{ b }
//!
//!     WHITESPACE = _{ " " }
//!     ```
//!
//!     Parsing both `"ab"` and `"a   b"` produce the token pairs `c(a())`.
//!
//! #### Expressions
//!
//! Expressions can be either terminals or non-terminals.
//!
//! 1. Terminals
//!
//!     | Terminal   | Usage                                                          |
//!     |------------|----------------------------------------------------------------|
//!     | `"a"`      | matches the exact string `"a"`                                 |
//!     | `^"a"`     | matches the exact string `"a"` case insensitively (ASCII only) |
//!     | `'a'..'z'` | matches one character between `'a'` and `'z'`                  |
//!     | `a`        | matches rule `a`                                               |
//!
//! Strings and characters follow
//! [Rust's escape mechanisms](https://doc.rust-lang.org/reference/tokens.html#byte-escapes), while
//! identifiers can contain alpha-numeric characters and underscores (`_`), as long as they do not
//! start with a digit.
//!
//! 2. Non-terminals
//!
//!     | Non-terminal          | Usage                                                      |
//!     |-----------------------|------------------------------------------------------------|
//!     | `(e)`                 | matches `e`                                                |
//!     | `e1 ~ e2`             | matches the sequence `e1` `e2`                             |
//!     | e1 \| e2 | matches either `e1` or `e2`                                |
//!     | `e*`                  | matches `e` zero or more times                             |
//!     | `e+`                  | matches `e` one or more times                              |
//!     | `e{n}`                | matches `e` exactly `n` times                              |
//!     | `e{, n}`              | matches `e` at most `n` times                              |
//!     | `e{n,} `              | matches `e` at least `n` times                             |
//!     | `e{m, n}`             | matches `e` between `m` and `n` times inclusively          |
//!     | `e?`                  | optionally matches `e`                                     |
//!     | `&e`                  | matches `e` without making progress                        |
//!     | `!e`                  | matches if `e` doesn't match without making progress       |
//!     | `PUSH(e)`             | matches `e` and pushes it's captured string down the stack |
//!
//!     where `e`, `e1`, and `e2` are expressions.
//!
//! Expressions can modify the stack only if they match the input. For example,
//! if `e1` in the compound expression `e1 | e2` does not match the input, then
//! it does not modify the stack, so `e2` sees the stack in the same state as
//! `e1` did. Repetitions and optionals (`e*`, `e+`, `e{, n}`, `e{n,}`,
//! `e{m,n}`, `e?`) can modify the stack each time `e` matches. The `!e` and `&e`
//! expressions are a special case; they never modify the stack.
//!
//! ## Special rules
//!
//! Special rules can be called within the grammar. They are:
//!
//! * `WHITESPACE` - runs between rules and sub-rules
//! * `COMMENT` - runs between rules and sub-rules
//! * `ANY` - matches exactly one `char`
//! * `SOI` - (start-of-input) matches only when a `Parser` is still at the starting position
//! * `EOI` - (end-of-input) matches only when a `Parser` has reached its end
//! * `POP` - pops a string from the stack and matches it
//! * `POP_ALL` - pops the entire state of the stack and matches it
//! * `PEEK` - peeks a string from the stack and matches it
//! * `PEEK_ALL` - peeks the entire state of the stack and matches it
//! * `DROP` - drops the top of the stack (fails to match if the stack is empty)
//!
//! `WHITESPACE` and `COMMENT` should be defined manually if needed. All other rules cannot be
//! overridden.
//!
//! ## `WHITESPACE` and `COMMENT`
//!
//! When defined, these rules get matched automatically in sequences (`~`) and repetitions
//! (`*`, `+`) between expressions. Atomic rules and those rules called by atomic rules are exempt
//! from this behavior.
//!
//! These rules should be defined so as to match one whitespace character and one comment only since
//! they are run in repetitions.
//!
//! If both `WHITESPACE` and `COMMENT` are defined, this grammar:
//!
//! ```ignore
//! a = { b ~ c }
//! ```
//!
//! is effectively transformed into this one behind the scenes:
//!
//! ```ignore
//! a = { b ~ WHITESPACE* ~ (COMMENT ~ WHITESPACE*)* ~ c }
//! ```
//!
//! ## `PUSH`, `POP`, `DROP`, and `PEEK`
//!
//! `PUSH(e)` simply pushes the captured string of the expression `e` down a stack. This stack can
//! then later be used to match grammar based on its content with `POP` and `PEEK`.
//!
//! `PEEK` always matches the string at the top of stack. So, if the stack contains `["a", "b"]`,
//! then this grammar:
//!
//! ```ignore
//! a = { PEEK }
//! ```
//!
//! is effectively transformed into at parse time:
//!
//! ```ignore
//! a = { "a" }
//! ```
//!
//! `POP` works the same way with the exception that it pops the string off of the stack if the
//! the match worked. With the stack from above, if `POP` matches `"a"`, the stack will be mutated
//! to `["b"]`.
//!
//! `DROP` makes it possible to remove the string at the top of the stack
//! without matching it. If the stack is nonempty, `DROP` drops the top of the
//! stack. If the stack is empty, then `DROP` fails to match.
//!
//! ## `Rule`
//!
//! All rules defined or used in the grammar populate a generated `enum` called `Rule`. This
//! implements `pest`'s `RuleType` and can be used throughout the API.
//!
//! ## `Built-in rules`
//!
//! Pest also comes with a number of built-in rules for convenience. They are:
//!
//! * `ASCII_DIGIT` - matches a numeric character from 0..9
//! * `ASCII_NONZERO_DIGIT` - matches a numeric character from 1..9
//! * `ASCII_BIN_DIGIT` - matches a numeric character from 0..1
//! * `ASCII_OCT_DIGIT` - matches a numeric character from 0..7
//! * `ASCII_HEX_DIGIT` - matches a numeric character from 0..9 or a..f or A..F
//! * `ASCII_ALPHA_LOWER` - matches a character from a..z
//! * `ASCII_ALPHA_UPPER` - matches a character from A..Z
//! * `ASCII_ALPHA` - matches a character from a..z or A..Z
//! * `ASCII_ALPHANUMERIC` - matches a character from a..z or A..Z or 0..9
//! * `ASCII` - matches a character from \x00..\x7f
//! * `NEWLINE` - matches either "\n" or "\r\n" or "\r"

#![doc(html_root_url = "https://docs.rs/pest_derive")]

extern crate pest_generator;
extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_derive(Parser, attributes(grammar))]
pub fn derive_parser(input: TokenStream) -> TokenStream {
    pest_generator::derive_parser(input.into(), true).into()
}
<|endoftext|>


<commit_before>#![feature(test)]

extern crate compiletest_rs as compiletest;
extern crate test;

use std::path::{PathBuf, Path};
use std::env::{set_var, var};

fn clippy_driver_path() -> PathBuf {
    if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
        PathBuf::from(path)
    } else {
        PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
    }
}

fn host_libs() -> PathBuf {
    if let Some(path) = option_env!("HOST_LIBS") {
        PathBuf::from(path)
    } else {
        Path::new("target").join(env!("PROFILE"))
    }
}

fn rustc_test_suite() -> Option<PathBuf> {
    option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
}

fn rustc_lib_path() -> PathBuf {
    option_env!("RUSTC_LIB_PATH").unwrap().into()
}

fn config(dir: &'static str, mode: &'static str) -> compiletest::Config {
    let mut config = compiletest::Config::default();

    let cfg_mode = mode.parse().expect("Invalid mode");
    if let Ok(name) = var::<&str>("TESTNAME") {
        let s: String = name.to_owned();
        config.filter = Some(s)
    }

    if rustc_test_suite().is_some() {
        config.run_lib_path = rustc_lib_path();
        config.compile_lib_path = rustc_lib_path();
    }
    config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));

    config.mode = cfg_mode;
    config.build_base = if rustc_test_suite().is_some() {
        PathBuf::from("/tmp/clippy_test_build_base")
    } else {
        let mut path = std::env::current_dir().unwrap();
        path.push("target/debug/test_build_base");
        path
    };
    config.src_base = PathBuf::from(format!("tests/{}", dir));
    config.rustc_path = clippy_driver_path();
    config
}

fn run_mode(dir: &'static str, mode: &'static str) {
    compiletest::run_tests(&config(dir, mode));
}

fn prepare_env() {
    set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
    set_var("CLIPPY_TESTS", "true");
    set_var("RUST_BACKTRACE", "0");
}

#[test]
fn compile_test() {
    prepare_env();
    run_mode("run-pass", "run-pass");
    run_mode("ui", "ui");
}
<commit_msg>Give travis a guaranteed existing directory for ui test output<commit_after>#![feature(test)]

extern crate compiletest_rs as compiletest;
extern crate test;

use std::path::{PathBuf, Path};
use std::env::{set_var, var};

fn clippy_driver_path() -> PathBuf {
    if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
        PathBuf::from(path)
    } else {
        PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
    }
}

fn host_libs() -> PathBuf {
    if let Some(path) = option_env!("HOST_LIBS") {
        PathBuf::from(path)
    } else {
        Path::new("target").join(env!("PROFILE"))
    }
}

fn rustc_test_suite() -> Option<PathBuf> {
    option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
}

fn rustc_lib_path() -> PathBuf {
    option_env!("RUSTC_LIB_PATH").unwrap().into()
}

fn config(dir: &'static str, mode: &'static str) -> compiletest::Config {
    let mut config = compiletest::Config::default();

    let cfg_mode = mode.parse().expect("Invalid mode");
    if let Ok(name) = var::<&str>("TESTNAME") {
        let s: String = name.to_owned();
        config.filter = Some(s)
    }

    if rustc_test_suite().is_some() {
        config.run_lib_path = rustc_lib_path();
        config.compile_lib_path = rustc_lib_path();
    }
    config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));

    config.mode = cfg_mode;
    config.build_base = if rustc_test_suite().is_some() {
        // we don't need access to the stderr files on travis
        let mut path = PathBuf::from(env!("OUT_DIR"));
        path.push("test_build_base");
        path
    } else {
        let mut path = std::env::current_dir().unwrap();
        path.push("target/debug/test_build_base");
        path
    };
    config.src_base = PathBuf::from(format!("tests/{}", dir));
    config.rustc_path = clippy_driver_path();
    config
}

fn run_mode(dir: &'static str, mode: &'static str) {
    compiletest::run_tests(&config(dir, mode));
}

fn prepare_env() {
    set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
    set_var("CLIPPY_TESTS", "true");
    set_var("RUST_BACKTRACE", "0");
}

#[test]
fn compile_test() {
    prepare_env();
    run_mode("run-pass", "run-pass");
    run_mode("ui", "ui");
}
<|endoftext|>


<commit_before>import geom::matrix::{Matrix4, identity};
import opengles::gl2::{GLuint, delete_textures};

import std::cmp::FuzzyEq;
import dvec::DVec;

enum Format {
    ARGB32Format,
    RGB24Format
}

enum Layer {
    ContainerLayerKind(@ContainerLayer),
    ImageLayerKind(@ImageLayer),
    TiledImageLayerKind(@TiledImageLayer)
}

struct CommonLayer {
    mut parent: Option<Layer>,
    mut prev_sibling: Option<Layer>,
    mut next_sibling: Option<Layer>,

    mut transform: Matrix4<f32>,

    // FIXME: Workaround for cross-crate bug regarding mutability of class fields
    fn set_transform(new_transform: Matrix4<f32>) {
        self.transform = new_transform;
    }
}

fn CommonLayer() -> CommonLayer {
    CommonLayer {
        parent : None,
        prev_sibling : None,
        next_sibling : None,
        transform : identity(0.0f32),
    }
}

struct ContainerLayer {
    mut common: CommonLayer,
    mut first_child: Option<Layer>,
    mut last_child: Option<Layer>,
}

fn ContainerLayer() -> ContainerLayer {
    ContainerLayer {
        common : CommonLayer(),
        first_child : None,
        last_child : None,
    }
}

struct Image {
    width: uint,
    height: uint,
    format: Format,
    data: ~[u8],
    mut texture: Option<GLuint>,

    drop {
        match copy self.texture {
            None => {
                // Nothing to do.
            }
            Some(texture) => {
                delete_textures(&[texture]);
            }
        }
    }
}

fn Image_(width: uint, height: uint, format: Format, +data: ~[u8]) -> Image {
    Image {
        width : width,
        height : height,
        format : format,
        data : data,
        texture : None,
    }
}

struct ImageLayer {
    mut common: CommonLayer,
    mut image: @layers::Image,

    // FIXME: Workaround for cross-crate bug
    fn set_image(new_image: @layers::Image) {
        self.image = new_image;
    }
}

fn ImageLayer_(image: @layers::Image) -> ImageLayer {
    ImageLayer {
        common : CommonLayer(),
        image : image,
    }
}

struct TiledImageLayer {
    mut common: CommonLayer,
    tiles: DVec<@layers::Image>,
    mut tiles_across: uint,
}

fn TiledImageLayer_(in_tiles: &[@layers::Image], tiles_across: uint) -> TiledImageLayer {
    let tiles = DVec();
    for in_tiles.each |tile| {
        tiles.push(tile);
    }

    TiledImageLayer {
        common: CommonLayer(),
        tiles: tiles,
        tiles_across: tiles_across
    }
}

<commit_msg>Fix build break<commit_after>import geom::matrix::{Matrix4, identity};
import opengles::gl2::{GLuint, delete_textures};

import std::cmp::FuzzyEq;
import dvec::DVec;

enum Format {
    ARGB32Format,
    RGB24Format
}

enum Layer {
    ContainerLayerKind(@ContainerLayer),
    ImageLayerKind(@ImageLayer),
    TiledImageLayerKind(@TiledImageLayer)
}

struct CommonLayer {
    mut parent: Option<Layer>,
    mut prev_sibling: Option<Layer>,
    mut next_sibling: Option<Layer>,

    mut transform: Matrix4<f32>,

    // FIXME: Workaround for cross-crate bug regarding mutability of class fields
    fn set_transform(new_transform: Matrix4<f32>) {
        self.transform = new_transform;
    }
}

fn CommonLayer() -> CommonLayer {
    CommonLayer {
        parent : None,
        prev_sibling : None,
        next_sibling : None,
        transform : identity(0.0f32),
    }
}

struct ContainerLayer {
    mut common: CommonLayer,
    mut first_child: Option<Layer>,
    mut last_child: Option<Layer>,
}

fn ContainerLayer() -> ContainerLayer {
    ContainerLayer {
        common : CommonLayer(),
        first_child : None,
        last_child : None,
    }
}

struct Image {
    width: uint,
    height: uint,
    format: Format,
    data: ~[u8],
    mut texture: Option<GLuint>,

    drop {
        match copy self.texture {
            None => {
                // Nothing to do.
            }
            Some(texture) => {
                delete_textures(&[texture]);
            }
        }
    }
}

fn Image(width: uint, height: uint, format: Format, +data: ~[u8]) -> Image {
    Image {
        width : width,
        height : height,
        format : format,
        data : data,
        texture : None,
    }
}

struct ImageLayer {
    mut common: CommonLayer,
    mut image: @layers::Image,

    // FIXME: Workaround for cross-crate bug
    fn set_image(new_image: @layers::Image) {
        self.image = new_image;
    }
}

fn ImageLayer(image: @layers::Image) -> ImageLayer {
    ImageLayer {
        common : CommonLayer(),
        image : image,
    }
}

struct TiledImageLayer {
    mut common: CommonLayer,
    tiles: DVec<@layers::Image>,
    mut tiles_across: uint,
}

fn TiledImageLayer(in_tiles: &[@layers::Image], tiles_across: uint) -> TiledImageLayer {
    let tiles = DVec();
    for in_tiles.each |tile| {
        tiles.push(tile);
    }

    TiledImageLayer {
        common: CommonLayer(),
        tiles: tiles,
        tiles_across: tiles_across
    }
}

<|endoftext|>


<commit_before>use vangers::level::{
    Level, LevelData, TerrainBits, DELTA_MASK, DELTA_SHIFT0, DELTA_SHIFT1, DOUBLE_LEVEL,
};

const DELTA_MAX: u8 = (0x3 << DELTA_SHIFT0) + (0x3 << DELTA_SHIFT1);

fn avg(a: u8, b: u8) -> u8 {
    (a >> 1) + (b >> 1) + (a & b & 1)
}

pub fn extract_palette(level: &Level) -> Vec<u8> {
    level
        .terrains
        .iter()
        .flat_map(|terr| {
            let slice = match terr.colors.end.checked_sub(1) {
                Some(index) => &level.palette[index as usize][..3],
                None => &[0xFF; 3],
            };
            slice.iter().cloned()
        })
        .collect()
}

pub struct LevelLayers {
    pub size: (u32, u32),
    pub num_terrains: u8,
    pub het0: Vec<u8>,
    pub het1: Vec<u8>,
    pub delta: Vec<u8>,
    pub mat0: Vec<u8>,
    pub mat1: Vec<u8>,
}

impl LevelLayers {
    pub fn new(size: (u32, u32), num_terrains: u8) -> Self {
        let total = (size.0 * size.1) as usize;
        LevelLayers {
            size,
            num_terrains,
            het0: Vec::with_capacity(total),
            het1: Vec::with_capacity(total),
            delta: Vec::with_capacity(total),
            mat0: Vec::with_capacity(total / 2),
            mat1: Vec::with_capacity(total / 2),
        }
    }

    pub fn from_level_data(data: &LevelData, num_terrains: u8) -> Self {
        let mut ll = LevelLayers::new((data.size.0 as u32, data.size.1 as u32), num_terrains);
        ll.import(data);
        ll
    }

    fn import(&mut self, data: &LevelData) {
        let terrain_bits = TerrainBits::new(self.num_terrains);
        let row_size = data.size.0 as usize;
        for (hrow, mrow) in data.height.chunks(row_size).zip(data.meta.chunks(row_size)) {
            for ((&h0, &h1), (&m0, &m1)) in hrow
                .iter()
                .zip(hrow[1..].iter())
                .zip(mrow.iter().zip(mrow[1..].iter()))
                .step_by(2)
            {
                let t0 = terrain_bits.read(m0);
                let t1 = terrain_bits.read(m1);
                if m0 & DOUBLE_LEVEL != 0 {
                    let d =
                        ((m0 & DELTA_MASK) << DELTA_SHIFT0) + ((m1 & DELTA_MASK) << DELTA_SHIFT1);
                    //assert!(h0 + d <= h1); //TODO: figure out why this isn't true
                    self.het0.push(h0);
                    self.het0.push(h0);
                    self.het1.push(h1);
                    self.het1.push(h1);
                    self.delta.push(d);
                    self.delta.push(d);
                    self.mat0.push(t0 | (t0 << 4));
                    self.mat1.push(t1 | (t1 << 4));
                } else {
                    self.het0.push(h0);
                    self.het0.push(h1);
                    self.het1.push(h0);
                    self.het1.push(h1);
                    self.delta.push(0);
                    self.delta.push(0);
                    self.mat0.push(t0 | (t1 << 4));
                    self.mat1.push(t0 | (t1 << 4));
                }
            }
        }
    }

    pub fn export(self) -> LevelData {
        let terrain_bits = TerrainBits::new(self.num_terrains);
        let total = self.size.0 as usize * self.size.1 as usize;
        let mut height = Vec::with_capacity(total);
        let mut meta = Vec::with_capacity(total);

        for (((&h0a, &h0b), (&h1a, &h1b)), ((&da, &db), (&mat0, &mat1))) in self
            .het0
            .iter()
            .zip(&self.het0[1..])
            .zip(self.het1.iter().zip(&self.het1[1..]))
            .step_by(2)
            .zip(
                self.delta
                    .iter()
                    .zip(&self.delta[1..])
                    .step_by(2)
                    .zip(self.mat0.iter().zip(&self.mat1)),
            )
        {
            //assert!(h0a + da <= h1a && h0b + db <= h1b);
            let (m0a, m0b) = (mat0 & 0xF, mat0 >> 4);
            let (m1a, m1b) = (mat1 & 0xF, mat1 >> 4);
            let delta = avg(da, db).min(DELTA_MAX);
            if delta != 0 {
                let _ = (m0b, m1a); // assuming the same as mat0a and mat1b respectively
                let rda = (delta >> DELTA_SHIFT0) & DELTA_MASK;
                let rdb = (delta >> DELTA_SHIFT1) & DELTA_MASK;
                meta.push(DOUBLE_LEVEL | terrain_bits.write(m0a) | rda);
                meta.push(DOUBLE_LEVEL | terrain_bits.write(m1b) | rdb);
                height.push(avg(h0a, h0b));
                height.push(avg(h1a, h1b));
            } else {
                let _ = (m1a, m1b, delta); // assuming mat0 == mat1, delta == 0
                meta.push(terrain_bits.write(m0a));
                meta.push(terrain_bits.write(m0b));
                height.push(avg(h0a, h1a));
                height.push(avg(h0b, h1b));
            }
        }

        LevelData {
            size: (self.size.0 as i32, self.size.1 as i32),
            meta,
            height,
        }
    }
}
<commit_msg>Add level conversion roundtrip test<commit_after>use vangers::level::{
    Level, LevelData, TerrainBits, DELTA_MASK, DELTA_SHIFT0, DELTA_SHIFT1, DOUBLE_LEVEL,
};

const DELTA_MAX: u8 = (0x3 << DELTA_SHIFT0) + (0x3 << DELTA_SHIFT1);

fn avg(a: u8, b: u8) -> u8 {
    (a >> 1) + (b >> 1) + (a & b & 1)
}

pub fn extract_palette(level: &Level) -> Vec<u8> {
    level
        .terrains
        .iter()
        .flat_map(|terr| {
            let slice = match terr.colors.end.checked_sub(1) {
                Some(index) => &level.palette[index as usize][..3],
                None => &[0xFF; 3],
            };
            slice.iter().cloned()
        })
        .collect()
}

#[cfg_attr(test, derive(Clone, PartialEq))]
pub struct LevelLayers {
    pub size: (u32, u32),
    pub num_terrains: u8,
    pub het0: Vec<u8>,
    pub het1: Vec<u8>,
    pub delta: Vec<u8>,
    pub mat0: Vec<u8>,
    pub mat1: Vec<u8>,
}

impl LevelLayers {
    pub fn new(size: (u32, u32), num_terrains: u8) -> Self {
        let total = (size.0 * size.1) as usize;
        LevelLayers {
            size,
            num_terrains,
            het0: Vec::with_capacity(total),
            het1: Vec::with_capacity(total),
            delta: Vec::with_capacity(total),
            mat0: Vec::with_capacity(total / 2),
            mat1: Vec::with_capacity(total / 2),
        }
    }

    pub fn from_level_data(data: &LevelData, num_terrains: u8) -> Self {
        let mut ll = LevelLayers::new((data.size.0 as u32, data.size.1 as u32), num_terrains);
        ll.import(data);
        ll
    }

    fn import(&mut self, data: &LevelData) {
        let terrain_bits = TerrainBits::new(self.num_terrains);
        let row_size = data.size.0 as usize;
        for (hrow, mrow) in data.height.chunks(row_size).zip(data.meta.chunks(row_size)) {
            for ((&h0, &h1), (&m0, &m1)) in hrow
                .iter()
                .zip(hrow[1..].iter())
                .zip(mrow.iter().zip(mrow[1..].iter()))
                .step_by(2)
            {
                let t0 = terrain_bits.read(m0);
                let t1 = terrain_bits.read(m1);
                if m0 & DOUBLE_LEVEL != 0 {
                    let d =
                        ((m0 & DELTA_MASK) << DELTA_SHIFT0) + ((m1 & DELTA_MASK) << DELTA_SHIFT1);
                    //assert!(h0 + d <= h1); //TODO: figure out why this isn't true
                    self.het0.push(h0);
                    self.het0.push(h0);
                    self.het1.push(h1);
                    self.het1.push(h1);
                    self.delta.push(d);
                    self.delta.push(d);
                    self.mat0.push(t0 | (t0 << 4));
                    self.mat1.push(t1 | (t1 << 4));
                } else {
                    self.het0.push(h0);
                    self.het0.push(h1);
                    self.het1.push(h0);
                    self.het1.push(h1);
                    self.delta.push(0);
                    self.delta.push(0);
                    self.mat0.push(t0 | (t1 << 4));
                    self.mat1.push(t0 | (t1 << 4));
                }
            }
        }
    }

    pub fn export(self) -> LevelData {
        let terrain_bits = TerrainBits::new(self.num_terrains);
        let total = self.size.0 as usize * self.size.1 as usize;
        let mut height = Vec::with_capacity(total);
        let mut meta = Vec::with_capacity(total);

        for (((&h0a, &h0b), (&h1a, &h1b)), ((&da, &db), (&mat0, &mat1))) in self
            .het0
            .iter()
            .zip(&self.het0[1..])
            .zip(self.het1.iter().zip(&self.het1[1..]))
            .step_by(2)
            .zip(
                self.delta
                    .iter()
                    .zip(&self.delta[1..])
                    .step_by(2)
                    .zip(self.mat0.iter().zip(&self.mat1)),
            )
        {
            //assert!(h0a + da <= h1a && h0b + db <= h1b);
            let (m0a, m0b) = (mat0 & 0xF, mat0 >> 4);
            let (m1a, m1b) = (mat1 & 0xF, mat1 >> 4);
            let delta = avg(da, db).min(DELTA_MAX);
            if delta != 0 {
                let _ = (m0b, m1a); // assuming the same as mat0a and mat1b respectively
                let rda = (delta >> DELTA_SHIFT0) & DELTA_MASK;
                let rdb = (delta >> DELTA_SHIFT1) & DELTA_MASK;
                meta.push(DOUBLE_LEVEL | terrain_bits.write(m0a) | rda);
                meta.push(DOUBLE_LEVEL | terrain_bits.write(m1b) | rdb);
                height.push(avg(h0a, h0b));
                height.push(avg(h1a, h1b));
            } else {
                let _ = (m1a, m1b, delta); // assuming mat0 == mat1, delta == 0
                meta.push(terrain_bits.write(m0a));
                meta.push(terrain_bits.write(m0b));
                height.push(avg(h0a, h1a));
                height.push(avg(h0b, h1b));
            }
        }

        LevelData {
            size: (self.size.0 as i32, self.size.1 as i32),
            meta,
            height,
        }
    }
}

#[test]
fn test_roundtrip() {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    let size = 64;
    let num_terrains = 8;
    let mut layers = LevelLayers::new((size, size), num_terrains);

    for _ in 0..size {
        for _ in 0..size / 2 {
            let delta = rng.gen_range(0, 16) << DELTA_SHIFT1;
            let h0 = rng.gen();
            let h1 = rng.gen();
            let t0 = rng.gen_range(0, num_terrains);
            let t1 = rng.gen_range(0, num_terrains);
            layers.delta.push(delta);
            layers.delta.push(delta);
            if delta != 0 {
                layers.het0.push(h0);
                layers.het0.push(h0);
                layers.het1.push(h1);
                layers.het1.push(h1);
                layers.mat0.push(t0 | (t0 << 4));
                layers.mat1.push(t1 | (t1 << 4));
            } else {
                layers.het0.push(h0);
                layers.het0.push(h1);
                layers.het1.push(h0);
                layers.het1.push(h1);
                layers.mat0.push(t0 | (t1 << 4));
                layers.mat1.push(t0 | (t1 << 4));
            }
        }
    }

    let ldata = layers.clone().export();
    let mut layers2 = LevelLayers::new((size, size), num_terrains);
    layers2.import(&ldata);

    assert!(layers == layers2);
}
<|endoftext|>