<commit_before>//! Kata OS security coordinator fake support

extern crate alloc;
use alloc::string::{String, ToString};
use hashbrown::HashMap;
use kata_memory_interface::ObjDescBundle;
use kata_memory_interface::kata_object_free_in_cnode;
use kata_security_interface::*;
use kata_storage_interface::KeyValueData;

struct BundleData {
    pkg_contents: ObjDescBundle,
    pkg_size: usize,
    manifest: String,
    keys: HashMap<String, KeyValueData>,
}
impl BundleData {
    fn new(pkg_contents: &ObjDescBundle) -> Self {
        let size = pkg_contents.objs.len() * 4096; // XXX
        BundleData {
            pkg_contents: pkg_contents.clone(),
            pkg_size: size,
            manifest: String::from(
                "# Comments like this
                        [Manifest]
                        BundleId=com.google.cerebra.hw.HelloWorld

                        [Binaries]
                        App=HelloWorldBin
                        Model=NeuralNetworkName

                        [Storage]
                        Required=1
                      ",
            ),
            keys: HashMap::with_capacity(2),
        }
    }
}
impl Drop for BundleData {
    fn drop(&mut self) {
        let _ = kata_object_free_in_cnode(&self.pkg_contents);
    }
}

pub struct FakeSecurityCoordinator {
    bundles: HashMap<String, BundleData>,
}
impl Default for FakeSecurityCoordinator {
    fn default() -> Self {
        Self::new()
    }
}
impl FakeSecurityCoordinator {
    pub fn new() -> Self {
        FakeSecurityCoordinator {
            bundles: HashMap::with_capacity(2),
        }
    }

    fn get_bundle(&self, bundle_id: &str) -> Result<&BundleData, SecurityRequestError> {
        self.bundles
            .get(bundle_id)
            .map_or_else(|| Err(SecurityRequestError::SreBundleNotFound), Ok)
    }
    fn get_bundle_mut(&mut self, bundle_id: &str) -> Result<&mut BundleData, SecurityRequestError> {
        self.bundles
            .get_mut(bundle_id)
            .map_or_else(|| Err(SecurityRequestError::SreBundleNotFound), Ok)
    }
    fn remove_bundle(&mut self, bundle_id: &str) -> Result<(), SecurityRequestError> {
        self.bundles
            .remove(bundle_id)
            .map_or_else(|| Err(SecurityRequestError::SreBundleNotFound), |_| Ok(()))
    }
}
pub type KataSecurityCoordinatorInterface = FakeSecurityCoordinator;

impl SecurityCoordinatorInterface for FakeSecurityCoordinator {
    fn install(&mut self, pkg_contents: &ObjDescBundle) -> Result<String, SecurityRequestError> {
        let bundle_id = "fubar".to_string(); // XXX
        if self.bundles.contains_key(&bundle_id) {
            return Err(SecurityRequestError::SreDeleteFirst);
        }
        assert!(self.bundles.insert(bundle_id.clone(), BundleData::new(pkg_contents)).is_none());
        Ok(bundle_id)
    }
    fn uninstall(&mut self, bundle_id: &str) -> Result<(), SecurityRequestError> {
        self.remove_bundle(bundle_id)
    }
    fn size_buffer(&self, bundle_id: &str) -> Result<usize, SecurityRequestError> {
        let bundle = self.get_bundle(bundle_id)?;
        Ok(bundle.pkg_size) // TODO(sleffler): do better
    }
    fn get_manifest(&self, bundle_id: &str) -> Result<String, SecurityRequestError> {
        let bundle = self.get_bundle(bundle_id)?;
        // return &?
        Ok(bundle.manifest.clone())
    }
    fn load_application(&self, bundle_id: &str) -> Result<ObjDescBundle, SecurityRequestError> {
        let bundle_data = self.get_bundle(bundle_id)?;
        // XXX just return the package for now
        Ok(bundle_data.pkg_contents.clone())
    }
    fn load_model(&self, bundle_id: &str, _model_id: &str) -> Result<ObjDescBundle, SecurityRequestError> {
        let bundle_data = self.get_bundle(bundle_id)?;
        // TODO(sleffler): check model id
        // XXX just return the package for now
        Ok(bundle_data.pkg_contents.clone())
    }
    fn read_key(&self, bundle_id: &str, key: &str) -> Result<&KeyValueData, SecurityRequestError> {
        let bundle = self.get_bundle(bundle_id)?;
        bundle.keys.get(key).ok_or(SecurityRequestError::SreKeyNotFound)
    }
    fn write_key(&mut self, bundle_id: &str, key: &str, value: &KeyValueData) -> Result<(), SecurityRequestError> {
        let bundle = self.get_bundle_mut(bundle_id)?;
        let _ = bundle.keys.insert(key.to_string(), *value);
        Ok(())
    }
    fn delete_key(&mut self, bundle_id: &str, key: &str) -> Result<(), SecurityRequestError> {
        let bundle = self.get_bundle_mut(bundle_id)?;
        // TODO(sleffler): error if no entry?
        let _ = bundle.keys.remove(key);
        Ok(())
    }
}
<commit_msg>kata-security-coordinator::fakeimpl: construct unique bundle id's<commit_after>//! Kata OS security coordinator fake support

extern crate alloc;
use alloc::fmt;
use alloc::string::{String, ToString};
use hashbrown::HashMap;
use kata_memory_interface::ObjDescBundle;
use kata_memory_interface::kata_object_free_in_cnode;
use kata_security_interface::*;
use kata_storage_interface::KeyValueData;

struct BundleData {
    pkg_contents: ObjDescBundle,
    pkg_size: usize,
    manifest: String,
    keys: HashMap<String, KeyValueData>,
}
impl BundleData {
    fn new(pkg_contents: &ObjDescBundle) -> Self {
        let size = pkg_contents.objs.len() * 4096; // XXX
        BundleData {
            pkg_contents: pkg_contents.clone(),
            pkg_size: size,
            manifest: String::from(
                "# Comments like this
                [Manifest]
                BundleId=com.google.cerebra.hw.HelloWorld

                [Binaries]
                App=HelloWorldBin
                Model=NeuralNetworkName

                [Storage]
                Required=1
              ",
            ),
            keys: HashMap::with_capacity(2),
        }
    }
}
impl Drop for BundleData {
    fn drop(&mut self) {
        let _ = kata_object_free_in_cnode(&self.pkg_contents);
    }
}

pub struct FakeSecurityCoordinator {
    bundles: HashMap<String, BundleData>,
}
impl Default for FakeSecurityCoordinator {
    fn default() -> Self {
        Self::new()
    }
}
impl FakeSecurityCoordinator {
    pub fn new() -> Self {
        FakeSecurityCoordinator {
            bundles: HashMap::with_capacity(2),
        }
    }

    fn get_bundle(&self, bundle_id: &str) -> Result<&BundleData, SecurityRequestError> {
        self.bundles
            .get(bundle_id)
            .map_or_else(|| Err(SecurityRequestError::SreBundleNotFound), Ok)
    }
    fn get_bundle_mut(&mut self, bundle_id: &str) -> Result<&mut BundleData, SecurityRequestError> {
        self.bundles
            .get_mut(bundle_id)
            .map_or_else(|| Err(SecurityRequestError::SreBundleNotFound), Ok)
    }
    fn remove_bundle(&mut self, bundle_id: &str) -> Result<(), SecurityRequestError> {
        self.bundles
            .remove(bundle_id)
            .map_or_else(|| Err(SecurityRequestError::SreBundleNotFound), |_| Ok(()))
    }
}
pub type KataSecurityCoordinatorInterface = FakeSecurityCoordinator;

impl SecurityCoordinatorInterface for FakeSecurityCoordinator {
    fn install(&mut self, pkg_contents: &ObjDescBundle) -> Result<String, SecurityRequestError> {
        // TODO(sleffler): get bundle_id from the manifest; for now use the
        //    cnode's CPtr since it is unique wrt all installed packages
        let bundle_id = fmt::format(format_args!("fake.{}", pkg_contents.cnode));
        if self.bundles.contains_key(&bundle_id) {
            return Err(SecurityRequestError::SreDeleteFirst);
        }
        assert!(self.bundles.insert(bundle_id.clone(), BundleData::new(pkg_contents)).is_none());
        Ok(bundle_id)
    }
    fn uninstall(&mut self, bundle_id: &str) -> Result<(), SecurityRequestError> {
        self.remove_bundle(bundle_id)
    }
    fn size_buffer(&self, bundle_id: &str) -> Result<usize, SecurityRequestError> {
        let bundle = self.get_bundle(bundle_id)?;
        Ok(bundle.pkg_size) // TODO(sleffler): do better
    }
    fn get_manifest(&self, bundle_id: &str) -> Result<String, SecurityRequestError> {
        let bundle = self.get_bundle(bundle_id)?;
        // return &?
        Ok(bundle.manifest.clone())
    }
    fn load_application(&self, bundle_id: &str) -> Result<ObjDescBundle, SecurityRequestError> {
        let bundle_data = self.get_bundle(bundle_id)?;
        // XXX just return the package for now
        Ok(bundle_data.pkg_contents.clone())
    }
    fn load_model(&self, bundle_id: &str, _model_id: &str) -> Result<ObjDescBundle, SecurityRequestError> {
        let bundle_data = self.get_bundle(bundle_id)?;
        // TODO(sleffler): check model id
        // XXX just return the package for now
        Ok(bundle_data.pkg_contents.clone())
    }
    fn read_key(&self, bundle_id: &str, key: &str) -> Result<&KeyValueData, SecurityRequestError> {
        let bundle = self.get_bundle(bundle_id)?;
        bundle.keys.get(key).ok_or(SecurityRequestError::SreKeyNotFound)
    }
    fn write_key(&mut self, bundle_id: &str, key: &str, value: &KeyValueData) -> Result<(), SecurityRequestError> {
        let bundle = self.get_bundle_mut(bundle_id)?;
        let _ = bundle.keys.insert(key.to_string(), *value);
        Ok(())
    }
    fn delete_key(&mut self, bundle_id: &str, key: &str) -> Result<(), SecurityRequestError> {
        let bundle = self.get_bundle_mut(bundle_id)?;
        // TODO(sleffler): error if no entry?
        let _ = bundle.keys.remove(key);
        Ok(())
    }
}
<|endoftext|>


<commit_before>// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// 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. This file may not be copied, modified, or distributed
// except according to those terms.

#[allow(missing_doc)];

use digest::DigestUtil;
use json;
use sha1::Sha1;
use serialize::{Encoder, Encodable, Decoder, Decodable};
use sort;

use std::cell::Cell;
use std::cmp;
use std::comm::{PortOne, oneshot, send_one, recv_one};
use std::either::{Either, Left, Right};
use std::hashmap::HashMap;
use std::io;
use std::result;
use std::run;
use std::task;
use std::to_bytes;

/**
*
* This is a loose clone of the [fbuild build system](https://github.com/felix-lang/fbuild),
* made a touch more generic (not wired to special cases on files) and much
* less metaprogram-y due to rust's comparative weakness there, relative to
* python.
*
* It's based around _imperative builds_ that happen to have some function
* calls cached. That is, it's _just_ a mechanism for describing cached
* functions. This makes it much simpler and smaller than a "build system"
* that produces an IR and evaluates it. The evaluation order is normal
* function calls. Some of them just return really quickly.
*
* A cached function consumes and produces a set of _works_. A work has a
* name, a kind (that determines how the value is to be checked for
* freshness) and a value. Works must also be (de)serializable. Some
* examples of works:
*
*    kind   name    value
*   ------------------------
*    cfg    os      linux
*    file   foo.c   <sha1>
*    url    foo.com <etag>
*
* Works are conceptually single units, but we store them most of the time
* in maps of the form (type,name) => value. These are WorkMaps.
*
* A cached function divides the works it's interested in into inputs and
* outputs, and subdivides those into declared (input) works and
* discovered (input and output) works.
*
* A _declared_ input or is one that is given to the workcache before
* any work actually happens, in the "prep" phase. Even when a function's
* work-doing part (the "exec" phase) never gets called, it has declared
* inputs, which can be checked for freshness (and potentially
* used to determine that the function can be skipped).
*
* The workcache checks _all_ works for freshness, but uses the set of
* discovered outputs from the _previous_ exec (which it will re-discover
* and re-record each time the exec phase runs).
*
* Therefore the discovered works cached in the db might be a
* mis-approximation of the current discoverable works, but this is ok for
* the following reason: we assume that if an artifact A changed from
* depending on B,C,D to depending on B,C,D,E, then A itself changed (as
* part of the change-in-dependencies), so we will be ok.
*
* Each function has a single discriminated output work called its _result_.
* This is only different from other works in that it is returned, by value,
* from a call to the cacheable function; the other output works are used in
* passing to invalidate dependencies elsewhere in the cache, but do not
* otherwise escape from a function invocation. Most functions only have one
* output work anyways.
*
* A database (the central store of a workcache) stores a mappings:
*
* (fn_name,{declared_input}) => ({discovered_input},
*                                {discovered_output},result)
*
* (Note: fbuild, which workcache is based on, has the concept of a declared
* output as separate from a discovered output. This distinction exists only
* as an artifact of how fbuild works: via annotations on function types
* and metaprogramming, with explicit dependency declaration as a fallback.
* Workcache is more explicit about dependencies, and as such treats all
* outputs the same, as discovered-during-the-last-run.)
*
*/

#[deriving(Clone, Eq, Encodable, Decodable)]
struct WorkKey {
    kind: ~str,
    name: ~str
}

impl to_bytes::IterBytes for WorkKey {
    #[inline]
    fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool {
        self.kind.iter_bytes(lsb0, |b| f(b)) && self.name.iter_bytes(lsb0, |b| f(b))
    }
}

impl cmp::Ord for WorkKey {
    fn lt(&self, other: &WorkKey) -> bool {
        self.kind < other.kind ||
            (self.kind == other.kind &&
             self.name < other.name)
    }
    fn le(&self, other: &WorkKey) -> bool {
        self.lt(other) || self.eq(other)
    }
    fn ge(&self, other: &WorkKey) -> bool {
        self.gt(other) || self.eq(other)
    }
    fn gt(&self, other: &WorkKey) -> bool {
        ! self.le(other)
    }
}

impl WorkKey {
    pub fn new(kind: &str, name: &str) -> WorkKey {
        WorkKey {
            kind: kind.to_owned(),
            name: name.to_owned(),
        }
    }
}

struct WorkMap(HashMap<WorkKey, ~str>);

impl Clone for WorkMap {
    fn clone(&self) -> WorkMap {
        WorkMap((**self).clone())
    }
}

impl WorkMap {
    fn new() -> WorkMap { WorkMap(HashMap::new()) }
}

impl<S:Encoder> Encodable<S> for WorkMap {
    fn encode(&self, s: &mut S) {
        let mut d = ~[];
        for self.iter().advance |(k, v)| {
            d.push(((*k).clone(), (*v).clone()))
        }
        sort::tim_sort(d);
        d.encode(s)
    }
}

impl<D:Decoder> Decodable<D> for WorkMap {
    fn decode(d: &mut D) -> WorkMap {
        let v : ~[(WorkKey,~str)] = Decodable::decode(d);
        let mut w = WorkMap::new();
        for v.iter().advance |pair| {
            w.insert(pair.first(), pair.second());
        }
        w
    }
}

struct Database {
    db_filename: Path,
    db_cache: HashMap<~str, ~str>,
    db_dirty: bool
}

impl Database {
    pub fn prepare(&mut self,
                   fn_name: &str,
                   declared_inputs: &WorkMap)
                   -> Option<(WorkMap, WorkMap, ~str)> {
        let k = json_encode(&(fn_name, declared_inputs));
        match self.db_cache.find(&k) {
            None => None,
            Some(v) => Some(json_decode(*v))
        }
    }

    pub fn cache(&mut self,
                 fn_name: &str,
                 declared_inputs: &WorkMap,
                 discovered_inputs: &WorkMap,
                 discovered_outputs: &WorkMap,
                 result: &str) {
        let k = json_encode(&(fn_name, declared_inputs));
        let v = json_encode(&(discovered_inputs,
                              discovered_outputs,
                              result));
        self.db_cache.insert(k,v);
        self.db_dirty = true
    }
}

struct Logger {
    // FIXME #4432: Fill in
    a: ()
}

impl Logger {
    pub fn info(&self, i: &str) {
        io::println(~"workcache: " + i);
    }
}

struct Context {
    db: @mut Database,
    logger: @mut Logger,
    cfg: @json::Object,
    freshness: HashMap<~str,@fn(&str,&str)->bool>
}

#[deriving(Clone)]
struct Prep {
    ctxt: @Context,
    fn_name: ~str,
    declared_inputs: WorkMap,
}

struct Exec {
    discovered_inputs: WorkMap,
    discovered_outputs: WorkMap
}

struct Work<T> {
    prep: @mut Prep,
    res: Option<Either<T,PortOne<(Exec,T)>>>
}

fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
    do io::with_str_writer |wr| {
        let mut encoder = json::Encoder(wr);
        t.encode(&mut encoder);
    }
}

// FIXME(#5121)
fn json_decode<T:Decodable<json::Decoder>>(s: &str) -> T {
    do io::with_str_reader(s) |rdr| {
        let j = result::unwrap(json::from_reader(rdr));
        let mut decoder = json::Decoder(j);
        Decodable::decode(&mut decoder)
    }
}

fn digest<T:Encodable<json::Encoder>>(t: &T) -> ~str {
    let mut sha = ~Sha1::new();
    (*sha).input_str(json_encode(t));
    (*sha).result_str()
}

fn digest_file(path: &Path) -> ~str {
    let mut sha = ~Sha1::new();
    let s = io::read_whole_file_str(path);
    (*sha).input_str(*s.get_ref());
    (*sha).result_str()
}

impl Context {
    pub fn new(db: @mut Database, lg: @mut Logger, cfg: @json::Object)
               -> Context {
        Context {
            db: db,
            logger: lg,
            cfg: cfg,
            freshness: HashMap::new()
        }
    }

    pub fn prep<T:Send +
                  Encodable<json::Encoder> +
                  Decodable<json::Decoder>>(@self, // FIXME(#5121)
                                            fn_name:&str,
                                            blk: &fn(@mut Prep)->Work<T>)
                                            -> Work<T> {
        let p = @mut Prep {
            ctxt: self,
            fn_name: fn_name.to_owned(),
            declared_inputs: WorkMap::new()
        };
        blk(p)
    }
}

trait TPrep {
    fn declare_input(&mut self, kind:&str, name:&str, val:&str);
    fn is_fresh(&self, cat:&str, kind:&str, name:&str, val:&str) -> bool;
    fn all_fresh(&self, cat:&str, map:&WorkMap) -> bool;
    fn exec<T:Send +
              Encodable<json::Encoder> +
              Decodable<json::Decoder>>( // FIXME(#5121)
        &self, blk: ~fn(&Exec) -> T) -> Work<T>;
}

impl TPrep for Prep {
    fn declare_input(&mut self, kind:&str, name:&str, val:&str) {
        self.declared_inputs.insert(WorkKey::new(kind, name),
                                 val.to_owned());
    }

    fn is_fresh(&self, cat: &str, kind: &str,
                name: &str, val: &str) -> bool {
        let k = kind.to_owned();
        let f = (*self.ctxt.freshness.get(&k))(name, val);
        let lg = self.ctxt.logger;
            if f {
                lg.info(fmt!("%s %s:%s is fresh",
                             cat, kind, name));
            } else {
                lg.info(fmt!("%s %s:%s is not fresh",
                             cat, kind, name))
            }
        f
    }

    fn all_fresh(&self, cat: &str, map: &WorkMap) -> bool {
        for map.iter().advance |(k, v)| {
            if ! self.is_fresh(cat, k.kind, k.name, *v) {
                return false;
            }
        }
        return true;
    }

    fn exec<T:Send +
              Encodable<json::Encoder> +
              Decodable<json::Decoder>>( // FIXME(#5121)
            &self, blk: ~fn(&Exec) -> T) -> Work<T> {
        let mut bo = Some(blk);

        let cached = self.ctxt.db.prepare(self.fn_name, &self.declared_inputs);

        match cached {
            Some((ref disc_in, ref disc_out, ref res))
            if self.all_fresh("declared input",
                              &self.declared_inputs) &&
            self.all_fresh("discovered input", disc_in) &&
            self.all_fresh("discovered output", disc_out) => {
                Work::new(@mut (*self).clone(), Left(json_decode(*res)))
            }

            _ => {
                let (port, chan) = oneshot();
                let blk = bo.take_unwrap();
                let chan = Cell::new(chan);

                do task::spawn {
                    let exe = Exec {
                        discovered_inputs: WorkMap::new(),
                        discovered_outputs: WorkMap::new(),
                    };
                    let chan = chan.take();
                    let v = blk(&exe);
                    send_one(chan, (exe, v));
                }
                Work::new(@mut (*self).clone(), Right(port))
            }
        }
    }
}

impl<T:Send +
       Encodable<json::Encoder> +
       Decodable<json::Decoder>> Work<T> { // FIXME(#5121)
    pub fn new(p: @mut Prep, e: Either<T,PortOne<(Exec,T)>>) -> Work<T> {
        Work { prep: p, res: Some(e) }
    }
}

// FIXME (#3724): movable self. This should be in impl Work.
fn unwrap<T:Send +
            Encodable<json::Encoder> +
            Decodable<json::Decoder>>( // FIXME(#5121)
        w: Work<T>) -> T {
    let mut ww = w;
    let s = ww.res.take();

    match s {
        None => fail!(),
        Some(Left(v)) => v,
        Some(Right(port)) => {
            let (exe, v) = recv_one(port);

            let s = json_encode(&v);

            let p = &*ww.prep;
            let db = p.ctxt.db;
            db.cache(p.fn_name,
                 &p.declared_inputs,
                 &exe.discovered_inputs,
                 &exe.discovered_outputs,
                 s);
            v
        }
    }
}

//#[test]
fn test() {
    use std::io::WriterUtil;

    let db = @mut Database { db_filename: Path("db.json"),
                             db_cache: HashMap::new(),
                             db_dirty: false };
    let lg = @mut Logger { a: () };
    let cfg = @HashMap::new();
    let cx = @Context::new(db, lg, cfg);
    let w:Work<~str> = do cx.prep("test1") |prep| {
        let pth = Path("foo.c");
        {
            let file = io::file_writer(&pth, [io::Create]).unwrap();
            file.write_str("int main() { return 0; }");
        }

        prep.declare_input("file", pth.to_str(), digest_file(&pth));
        do prep.exec |_exe| {
            let out = Path("foo.o");
            run::process_status("gcc", [~"foo.c", ~"-o", out.to_str()]);
            out.to_str()
        }
    };
    let s = unwrap(w);
    io::println(s);
}
<commit_msg>extra: use deriving more in workcache, switch to treemaps.<commit_after>// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// 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. This file may not be copied, modified, or distributed
// except according to those terms.

#[allow(missing_doc)];

use digest::DigestUtil;
use json;
use sha1::Sha1;
use serialize::{Encoder, Encodable, Decoder, Decodable};
use treemap::TreeMap;

use std::cell::Cell;
use std::comm::{PortOne, oneshot, send_one, recv_one};
use std::either::{Either, Left, Right};
use std::hashmap::HashMap;
use std::io;
use std::result;
use std::run;
use std::task;

/**
*
* This is a loose clone of the [fbuild build system](https://github.com/felix-lang/fbuild),
* made a touch more generic (not wired to special cases on files) and much
* less metaprogram-y due to rust's comparative weakness there, relative to
* python.
*
* It's based around _imperative builds_ that happen to have some function
* calls cached. That is, it's _just_ a mechanism for describing cached
* functions. This makes it much simpler and smaller than a "build system"
* that produces an IR and evaluates it. The evaluation order is normal
* function calls. Some of them just return really quickly.
*
* A cached function consumes and produces a set of _works_. A work has a
* name, a kind (that determines how the value is to be checked for
* freshness) and a value. Works must also be (de)serializable. Some
* examples of works:
*
*    kind   name    value
*   ------------------------
*    cfg    os      linux
*    file   foo.c   <sha1>
*    url    foo.com <etag>
*
* Works are conceptually single units, but we store them most of the time
* in maps of the form (type,name) => value. These are WorkMaps.
*
* A cached function divides the works it's interested in into inputs and
* outputs, and subdivides those into declared (input) works and
* discovered (input and output) works.
*
* A _declared_ input or is one that is given to the workcache before
* any work actually happens, in the "prep" phase. Even when a function's
* work-doing part (the "exec" phase) never gets called, it has declared
* inputs, which can be checked for freshness (and potentially
* used to determine that the function can be skipped).
*
* The workcache checks _all_ works for freshness, but uses the set of
* discovered outputs from the _previous_ exec (which it will re-discover
* and re-record each time the exec phase runs).
*
* Therefore the discovered works cached in the db might be a
* mis-approximation of the current discoverable works, but this is ok for
* the following reason: we assume that if an artifact A changed from
* depending on B,C,D to depending on B,C,D,E, then A itself changed (as
* part of the change-in-dependencies), so we will be ok.
*
* Each function has a single discriminated output work called its _result_.
* This is only different from other works in that it is returned, by value,
* from a call to the cacheable function; the other output works are used in
* passing to invalidate dependencies elsewhere in the cache, but do not
* otherwise escape from a function invocation. Most functions only have one
* output work anyways.
*
* A database (the central store of a workcache) stores a mappings:
*
* (fn_name,{declared_input}) => ({discovered_input},
*                                {discovered_output},result)
*
* (Note: fbuild, which workcache is based on, has the concept of a declared
* output as separate from a discovered output. This distinction exists only
* as an artifact of how fbuild works: via annotations on function types
* and metaprogramming, with explicit dependency declaration as a fallback.
* Workcache is more explicit about dependencies, and as such treats all
* outputs the same, as discovered-during-the-last-run.)
*
*/

#[deriving(Clone, Eq, Encodable, Decodable, TotalOrd, TotalEq)]
struct WorkKey {
    kind: ~str,
    name: ~str
}

impl WorkKey {
    pub fn new(kind: &str, name: &str) -> WorkKey {
        WorkKey {
            kind: kind.to_owned(),
            name: name.to_owned(),
        }
    }
}

#[deriving(Clone, Eq, Encodable, Decodable)]
struct WorkMap(TreeMap<WorkKey, ~str>);

impl WorkMap {
    fn new() -> WorkMap { WorkMap(TreeMap::new()) }
}

struct Database {
    db_filename: Path,
    db_cache: TreeMap<~str, ~str>,
    db_dirty: bool
}

impl Database {
    pub fn prepare(&mut self,
                   fn_name: &str,
                   declared_inputs: &WorkMap)
                   -> Option<(WorkMap, WorkMap, ~str)> {
        let k = json_encode(&(fn_name, declared_inputs));
        match self.db_cache.find(&k) {
            None => None,
            Some(v) => Some(json_decode(*v))
        }
    }

    pub fn cache(&mut self,
                 fn_name: &str,
                 declared_inputs: &WorkMap,
                 discovered_inputs: &WorkMap,
                 discovered_outputs: &WorkMap,
                 result: &str) {
        let k = json_encode(&(fn_name, declared_inputs));
        let v = json_encode(&(discovered_inputs,
                              discovered_outputs,
                              result));
        self.db_cache.insert(k,v);
        self.db_dirty = true
    }
}

struct Logger {
    // FIXME #4432: Fill in
    a: ()
}

impl Logger {
    pub fn info(&self, i: &str) {
        io::println(~"workcache: " + i);
    }
}

struct Context {
    db: @mut Database,
    logger: @mut Logger,
    cfg: @json::Object,
    freshness: TreeMap<~str,@fn(&str,&str)->bool>
}

#[deriving(Clone)]
struct Prep {
    ctxt: @Context,
    fn_name: ~str,
    declared_inputs: WorkMap,
}

struct Exec {
    discovered_inputs: WorkMap,
    discovered_outputs: WorkMap
}

struct Work<T> {
    prep: @mut Prep,
    res: Option<Either<T,PortOne<(Exec,T)>>>
}

fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
    do io::with_str_writer |wr| {
        let mut encoder = json::Encoder(wr);
        t.encode(&mut encoder);
    }
}

// FIXME(#5121)
fn json_decode<T:Decodable<json::Decoder>>(s: &str) -> T {
    do io::with_str_reader(s) |rdr| {
        let j = result::unwrap(json::from_reader(rdr));
        let mut decoder = json::Decoder(j);
        Decodable::decode(&mut decoder)
    }
}

fn digest<T:Encodable<json::Encoder>>(t: &T) -> ~str {
    let mut sha = ~Sha1::new();
    (*sha).input_str(json_encode(t));
    (*sha).result_str()
}

fn digest_file(path: &Path) -> ~str {
    let mut sha = ~Sha1::new();
    let s = io::read_whole_file_str(path);
    (*sha).input_str(*s.get_ref());
    (*sha).result_str()
}

impl Context {
    pub fn new(db: @mut Database, lg: @mut Logger, cfg: @json::Object)
               -> Context {
        Context {
            db: db,
            logger: lg,
            cfg: cfg,
            freshness: TreeMap::new()
        }
    }

    pub fn prep<T:Send +
                  Encodable<json::Encoder> +
                  Decodable<json::Decoder>>(@self, // FIXME(#5121)
                                            fn_name:&str,
                                            blk: &fn(@mut Prep)->Work<T>)
                                            -> Work<T> {
        let p = @mut Prep {
            ctxt: self,
            fn_name: fn_name.to_owned(),
            declared_inputs: WorkMap::new()
        };
        blk(p)
    }
}

trait TPrep {
    fn declare_input(&mut self, kind:&str, name:&str, val:&str);
    fn is_fresh(&self, cat:&str, kind:&str, name:&str, val:&str) -> bool;
    fn all_fresh(&self, cat:&str, map:&WorkMap) -> bool;
    fn exec<T:Send +
              Encodable<json::Encoder> +
              Decodable<json::Decoder>>( // FIXME(#5121)
        &self, blk: ~fn(&Exec) -> T) -> Work<T>;
}

impl TPrep for Prep {
    fn declare_input(&mut self, kind:&str, name:&str, val:&str) {
        self.declared_inputs.insert(WorkKey::new(kind, name),
                                 val.to_owned());
    }

    fn is_fresh(&self, cat: &str, kind: &str,
                name: &str, val: &str) -> bool {
        let k = kind.to_owned();
        let f = self.ctxt.freshness.find(&k);
        let fresh = match f {
            None => fail!("missing freshness-function for '%s'", kind),
            Some(f) => (*f)(name, val)
        };
        let lg = self.ctxt.logger;
        if fresh {
            lg.info(fmt!("%s %s:%s is fresh",
                         cat, kind, name));
        } else {
            lg.info(fmt!("%s %s:%s is not fresh",
                         cat, kind, name))
        }
        fresh
    }

    fn all_fresh(&self, cat: &str, map: &WorkMap) -> bool {
        for map.iter().advance |(k, v)| {
            if ! self.is_fresh(cat, k.kind, k.name, *v) {
                return false;
            }
        }
        return true;
    }

    fn exec<T:Send +
              Encodable<json::Encoder> +
              Decodable<json::Decoder>>( // FIXME(#5121)
            &self, blk: ~fn(&Exec) -> T) -> Work<T> {
        let mut bo = Some(blk);

        let cached = self.ctxt.db.prepare(self.fn_name, &self.declared_inputs);

        match cached {
            Some((ref disc_in, ref disc_out, ref res))
            if self.all_fresh("declared input",
                              &self.declared_inputs) &&
            self.all_fresh("discovered input", disc_in) &&
            self.all_fresh("discovered output", disc_out) => {
                Work::new(@mut (*self).clone(), Left(json_decode(*res)))
            }

            _ => {
                let (port, chan) = oneshot();
                let blk = bo.take_unwrap();
                let chan = Cell::new(chan);

                do task::spawn {
                    let exe = Exec {
                        discovered_inputs: WorkMap::new(),
                        discovered_outputs: WorkMap::new(),
                    };
                    let chan = chan.take();
                    let v = blk(&exe);
                    send_one(chan, (exe, v));
                }
                Work::new(@mut (*self).clone(), Right(port))
            }
        }
    }
}

impl<T:Send +
       Encodable<json::Encoder> +
       Decodable<json::Decoder>> Work<T> { // FIXME(#5121)
    pub fn new(p: @mut Prep, e: Either<T,PortOne<(Exec,T)>>) -> Work<T> {
        Work { prep: p, res: Some(e) }
    }
}

// FIXME (#3724): movable self. This should be in impl Work.
fn unwrap<T:Send +
            Encodable<json::Encoder> +
            Decodable<json::Decoder>>( // FIXME(#5121)
        w: Work<T>) -> T {
    let mut ww = w;
    let s = ww.res.take();

    match s {
        None => fail!(),
        Some(Left(v)) => v,
        Some(Right(port)) => {
            let (exe, v) = recv_one(port);

            let s = json_encode(&v);

            let p = &*ww.prep;
            let db = p.ctxt.db;
            db.cache(p.fn_name,
                 &p.declared_inputs,
                 &exe.discovered_inputs,
                 &exe.discovered_outputs,
                 s);
            v
        }
    }
}

//#[test]
fn test() {
    use std::io::WriterUtil;

    let db = @mut Database { db_filename: Path("db.json"),
                             db_cache: TreeMap::new(),
                             db_dirty: false };
    let lg = @mut Logger { a: () };
    let cfg = @HashMap::new();
    let cx = @Context::new(db, lg, cfg);
    let w:Work<~str> = do cx.prep("test1") |prep| {
        let pth = Path("foo.c");
        {
            let file = io::file_writer(&pth, [io::Create]).unwrap();
            file.write_str("int main() { return 0; }");
        }

        prep.declare_input("file", pth.to_str(), digest_file(&pth));
        do prep.exec |_exe| {
            let out = Path("foo.o");
            run::process_status("gcc", [~"foo.c", ~"-o", out.to_str()]);
            out.to_str()
        }
    };
    let s = unwrap(w);
    io::println(s);
}
<|endoftext|>


<commit_before>//! Integer operations and traits.

#![warn(bad_style, missing_docs,
        unused, unused_extern_crates, unused_import_braces,
        unused_qualifications, unused_results, unused_typecasts)]

extern crate num;

use num::{One, Zero};

/// Extension methods for num::Integer trait.
pub trait Integer: num::Integer + Clone + FromPrimitive + ToPrimitive {

    /// Divide two numbers, return the result, rounded up.
    ///
    /// # Arguments
    ///
    /// * x - an integer
    /// * y - an integer distinct from 0u
    ///
    /// # Return value
    ///
    /// The smallest integer `q` such that `x/y <= q`.
    ///
    fn div_ceil(&self, other: &Self) -> Self {
        let div = self.clone() / other.clone();
        if self.is_multiple_of(other) {
            div
        } else {
            div + One::one()
        }
    }

    /// Divide two numbers, return the result, rounded to the closest integer.
    ///
    /// # Arguments
    ///
    /// * x - an integer
    /// * y - an integer distinct from 0u
    ///
    /// # Return value
    ///
    /// The integer `q` closest to `x/y`.
    ///
    fn div_round(&self, other: &Self) -> Self {
        let (div, rem) = self.div_rem(other);
        if rem.clone() + rem.clone() < other.clone() {
            div
        } else {
            div + One::one()
        }
    }

    /// Creates an iterator to enumerate each digit from the lower of the number.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    ///
    /// let mut it = 12345i.into_digits(10);
    /// assert_eq!(Some(5), it.next());
    /// assert_eq!(Some(4), it.next());
    /// assert_eq!(Some(3), it.next());
    /// assert_eq!(Some(2), it.next());
    /// assert_eq!(Some(1), it.next());
    /// assert_eq!(None,    it.next());
    /// ```
    #[inline]
    fn into_digits(self, radix: Self) -> Digits<Self> {
        Digits::new(self, radix)
    }

    /// Creates a histogram of the number.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    /// assert!([0, 1, 1, 1, 0, 0, 0, 0, 0, 0] == 123u.into_digit_histogram());
    /// assert!([0, 3, 0, 0, 0, 0, 0, 0, 0, 0] == 111u.into_digit_histogram());
    /// assert!([0, 0, 0, 0, 0, 0, 0, 0, 0, 0] == 0u.into_digit_histogram());
    /// ```
    #[inline]
    fn into_digit_histogram(self) -> [uint, .. 10] {
        let mut hist = [0, .. 10];
        let ten = FromPrimitive::from_uint(10).unwrap();
        for d in self.into_digits(ten) {
            hist[d.to_uint().unwrap()] += 1;
        }
        hist
    }

    /// Creates an integer from an iterator to enumerate each digit from the lower.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    ///
    /// assert_eq!(321, Integer::from_digits(vec![1, 2, 3].into_iter(), 10i));
    /// assert_eq!(0x321, Integer::from_digits(vec![1, 2, 3].into_iter(), 16i));
    /// assert_eq!(0, Integer::from_digits(vec![].into_iter(), 10i));
    /// ```
    #[inline]
    fn from_digits<T: Iterator<Self>>(mut digits: T, radix: Self) -> Self {
        let mut result: Self = Zero::zero();
        let mut order: Self = One::one();
        for d in digits {
            result = result + order.clone() * d;
            order = order * radix.clone();
        }
        result
    }

    /// Creates a palindromic number from `self`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    ///
    /// assert_eq!(12321, 123i.into_palindromic(10, false));
    /// assert_eq!(123321, 123i.into_palindromic(10, true));
    /// ```
    #[inline]
    fn into_palindromic(self, radix: Self, duplicate_middle: bool) -> Self {
        let digits = self.into_digits(radix.clone());
        let mut rv = digits.clone().rev();
        if !duplicate_middle { let _ = rv.next_back(); }
        rv.chain(digits).fold(Zero::zero(), |sum: Self, i| sum * radix.clone() + i)
    }

    /// Returns `true` if the number is palindromic.
    ///
    /// # Example
    ///
    /// ```
    /// use integer::Integer;
    ///
    /// assert_eq!(true, 12321i.is_palindromic(10));
    /// assert_eq!(false, 12345i.is_palindromic(10));
    /// ```
    fn is_palindromic(self, radix: Self) -> bool {
        let mut digits = self.into_digits(radix);
        loop {
            let next = digits.next();
            let next_back = digits.next_back();
            if next.is_none() || next_back.is_none() {
                return true;
            }
            if next != next_back {
                return false;
            }
        }
    }

    /// Takes the square root of the number.
    #[inline]
    fn sqrt(&self) -> Self {
        let one: Self = One::one();
        let two: Self = one.clone() + one.clone();
        let mut min: Self = Zero::zero();
        let mut max: Self = self.clone();

        while min < max {
            let mid = (min.clone() + max.clone() + one.clone()) / two.clone();
            let mid2 = mid.clone() * mid.clone();
            match mid2.partial_cmp(self).unwrap() {
                Equal => return mid,
                Greater => { max = mid - one.clone() }
                Less    => { min = mid }
            }
        }

        return min
    }

    /// Gets the factorial of the number.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    ///
    /// assert_eq!(1u, 0u.factorial());
    /// assert_eq!(1u, 1u.factorial());
    /// assert_eq!(2u, 2u.factorial());
    /// assert_eq!(6u, 3u.factorial());
    /// assert_eq!(24u, 4u.factorial());
    /// ```
    fn factorial(&self) -> Self {
        assert!(*self >= Zero::zero());

        let mut p: Self = One::one();
        let mut i: Self = One::one();
        while i <= *self {
            p = p * i.clone();
            i = i + One::one();
        }
        p
    }

    /// Takes the modular exponentation of the number.
    fn mod_pow(&self, exp: &Self, modulo: &Self) -> Self {
        let zero = Zero::zero();
        let one: Self  = One::one();
        let two: Self  = one.clone() + one.clone();
        if *self == zero { return zero }

        let mut result = one.clone();
        let mut base   = self.clone();
        let mut exp    = exp.clone();
        let     modulo = modulo.clone();

        while exp > zero {
            if exp.is_odd() {
                result = (result * base.clone()) % modulo.clone();
            }
            exp = exp / two.clone();
            base = (base.clone() * base) % modulo.clone();
        }
        result
    }
}

impl Integer for num::BigUint {}
impl Integer for num::BigInt {}
impl Integer for i8 {}
impl Integer for i16 {}
impl Integer for i32 {}
impl Integer for i64 {}
impl Integer for int {}
impl Integer for u8 {}
impl Integer for u16 {}
impl Integer for u32 {}
impl Integer for u64 {}
impl Integer for uint {}

/// An iterator that enumerates each digit of a number.
#[deriving(Clone)]
pub struct Digits<T> { num: T, radix: T, order: T }

impl<T: num::Integer + Clone> Digits<T> {
    fn new(num: T, radix: T) -> Digits<T> {
        let mut order: T;
        if num.is_zero() {
            order = Zero::zero();
        } else {
            order = One::one();
            let mut prod = order.clone() * radix.clone();
            while prod <= num {
                order = prod;
                prod = order.clone() * radix.clone();
            }
        }
        Digits { num: num, radix: radix, order: order }
    }
}

impl<T: num::Integer + Clone> Iterator<T> for Digits<T> {
    #[inline]
    fn next(&mut self) -> Option<T> {
        if self.order.is_zero() { return None; }
        let (d, r) = self.num.div_rem(&self.radix);
        self.num   = d;
        self.order = self.order.clone() / self.radix.clone();
        Some(r)
    }
}

impl<T: num::Integer + Clone> DoubleEndedIterator<T> for Digits<T> {
    #[inline]
    fn next_back(&mut self) -> Option<T> {
        if self.order.is_zero() { return None; }
        let (d, r) = self.num.div_rem(&self.order);
        self.num   = r;
        self.order = self.order.clone() / self.radix.clone();
        Some(d)
    }
}

#[cfg(test)]
mod tests {
    use super::Integer;
    use num;
    use num::Integer as NumInteger;

    #[test]
    fn div() {
        assert_eq!(0u, 0u.div_floor(&3));
        assert_eq!(0u, 0u.div_round(&3));
        assert_eq!(0u, 0u.div_ceil(&3));

        assert_eq!(0u, 1u.div_floor(&3));
        assert_eq!(0u, 1u.div_round(&3));
        assert_eq!(1u, 1u.div_ceil(&3));

        assert_eq!(0u, 2u.div_floor(&3));
        assert_eq!(1u, 2u.div_round(&3));
        assert_eq!(1u, 2u.div_ceil(&3));

        assert_eq!(1u, 3u.div_floor(&3));
        assert_eq!(1u, 3u.div_round(&3));
        assert_eq!(1u, 3u.div_ceil(&3));

        assert_eq!(1u, 4u.div_floor(&3));
        assert_eq!(1u, 4u.div_round(&3));
        assert_eq!(2u, 4u.div_ceil(&3));

        assert_eq!(1u, 5u.div_floor(&3));
        assert_eq!(2u, 5u.div_round(&3));
        assert_eq!(2u, 5u.div_ceil(&3));

        assert_eq!(3u, 5u.div_round(&2));
    }

    #[test]
    fn digits() {
        fn check(n: uint, v: &[uint], radix: uint) {
            assert_eq!(v, n.into_digits(radix).collect::<Vec<_>>()[]);
            let mut rev = n.into_digits(radix).rev().collect::<Vec<_>>();
            rev.reverse();
            assert_eq!(v, rev[])
        }

        check(0, &[], 10);
        check(1, &[1], 10);
        check(3, &[3], 10);
        check(12345, &[5, 4, 3, 2, 1], 10);
        check(0x12345, &[5, 4, 3, 2, 1], 16);
        check(0xabcde, &[0xe, 0xd, 0xc, 0xb, 0xa], 16);
        check(0b11100, &[0, 0, 1, 1, 1], 2);
    }

    #[test]
    fn to_palindromic() {
        assert_eq!(101, 10i.into_palindromic(10, false));
        assert_eq!(1001, 10i.into_palindromic(10, true));

        assert_eq!(99999, 999i.into_palindromic(10, false));
        assert_eq!(999999, 999i.into_palindromic(10, true));

        assert_eq!(99099, 990i.into_palindromic(10, false));
        assert_eq!(990099, 990i.into_palindromic(10, true));

        assert_eq!(1100011, 1100i.into_palindromic(10, false));
        assert_eq!(11000011, 1100i.into_palindromic(10, true));

        assert_eq!(0xabcba, 0xabci.into_palindromic(16, false));
        assert_eq!(0xabccba, 0xabci.into_palindromic(16, true));
    }

    #[test]
    fn is_palindromic() {
        assert!(0i.is_palindromic(10));
        assert!(1i.is_palindromic(10));
        assert!(9i.is_palindromic(10));
        assert!(11i.is_palindromic(10));
        assert!(121i.is_palindromic(10));
        assert!(!123i.is_palindromic(10));
        assert!(1221i.is_palindromic(10));
        assert!(12321i.is_palindromic(10));
    }

    #[test]
    fn mod_pow() {
        for b in range(1u, 10) {
            for e in range(0u, 5) {
                for r in range(10u, 100) {
                    assert_eq!(num::pow(b, e) % r, b.mod_pow(&e, &r));
                }
            }
        }
    }
}
<commit_msg>integer: Remove `uint` or `int` usage as general integer<commit_after>//! Integer operations and traits.

#![warn(bad_style, missing_docs,
        unused, unused_extern_crates, unused_import_braces,
        unused_qualifications, unused_results, unused_typecasts)]

extern crate num;

use num::{One, Zero};

/// Extension methods for num::Integer trait.
pub trait Integer: num::Integer + Clone + FromPrimitive + ToPrimitive {

    /// Divide two numbers, return the result, rounded up.
    ///
    /// # Arguments
    ///
    /// * x - an integer
    /// * y - an integer distinct from 0u
    ///
    /// # Return value
    ///
    /// The smallest integer `q` such that `x/y <= q`.
    ///
    fn div_ceil(&self, other: &Self) -> Self {
        let div = self.clone() / other.clone();
        if self.is_multiple_of(other) {
            div
        } else {
            div + One::one()
        }
    }

    /// Divide two numbers, return the result, rounded to the closest integer.
    ///
    /// # Arguments
    ///
    /// * x - an integer
    /// * y - an integer distinct from 0u
    ///
    /// # Return value
    ///
    /// The integer `q` closest to `x/y`.
    ///
    fn div_round(&self, other: &Self) -> Self {
        let (div, rem) = self.div_rem(other);
        if rem.clone() + rem.clone() < other.clone() {
            div
        } else {
            div + One::one()
        }
    }

    /// Creates an iterator to enumerate each digit from the lower of the number.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    ///
    /// let mut it = 12345i.into_digits(10);
    /// assert_eq!(Some(5), it.next());
    /// assert_eq!(Some(4), it.next());
    /// assert_eq!(Some(3), it.next());
    /// assert_eq!(Some(2), it.next());
    /// assert_eq!(Some(1), it.next());
    /// assert_eq!(None,    it.next());
    /// ```
    #[inline]
    fn into_digits(self, radix: Self) -> Digits<Self> {
        Digits::new(self, radix)
    }

    /// Creates a histogram of the number.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    /// assert!([0, 1, 1, 1, 0, 0, 0, 0, 0, 0] == 123u.into_digit_histogram());
    /// assert!([0, 3, 0, 0, 0, 0, 0, 0, 0, 0] == 111u.into_digit_histogram());
    /// assert!([0, 0, 0, 0, 0, 0, 0, 0, 0, 0] == 0u.into_digit_histogram());
    /// ```
    #[inline]
    fn into_digit_histogram(self) -> [u32, .. 10] {
        let mut hist = [0, .. 10];
        let ten = FromPrimitive::from_u32(10).unwrap();
        for d in self.into_digits(ten) {
            hist[d.to_uint().unwrap()] += 1;
        }
        hist
    }

    /// Creates an integer from an iterator to enumerate each digit from the lower.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    ///
    /// assert_eq!(321, Integer::from_digits(vec![1, 2, 3].into_iter(), 10i));
    /// assert_eq!(0x321, Integer::from_digits(vec![1, 2, 3].into_iter(), 16i));
    /// assert_eq!(0, Integer::from_digits(vec![].into_iter(), 10i));
    /// ```
    #[inline]
    fn from_digits<T: Iterator<Self>>(mut digits: T, radix: Self) -> Self {
        let mut result: Self = Zero::zero();
        let mut order: Self = One::one();
        for d in digits {
            result = result + order.clone() * d;
            order = order * radix.clone();
        }
        result
    }

    /// Creates a palindromic number from `self`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    ///
    /// assert_eq!(12321, 123i.into_palindromic(10, false));
    /// assert_eq!(123321, 123i.into_palindromic(10, true));
    /// ```
    #[inline]
    fn into_palindromic(self, radix: Self, duplicate_middle: bool) -> Self {
        let digits = self.into_digits(radix.clone());
        let mut rv = digits.clone().rev();
        if !duplicate_middle { let _ = rv.next_back(); }
        rv.chain(digits).fold(Zero::zero(), |sum: Self, i| sum * radix.clone() + i)
    }

    /// Returns `true` if the number is palindromic.
    ///
    /// # Example
    ///
    /// ```
    /// use integer::Integer;
    ///
    /// assert_eq!(true, 12321i.is_palindromic(10));
    /// assert_eq!(false, 12345i.is_palindromic(10));
    /// ```
    fn is_palindromic(self, radix: Self) -> bool {
        let mut digits = self.into_digits(radix);
        loop {
            let next = digits.next();
            let next_back = digits.next_back();
            if next.is_none() || next_back.is_none() {
                return true;
            }
            if next != next_back {
                return false;
            }
        }
    }

    /// Takes the square root of the number.
    #[inline]
    fn sqrt(&self) -> Self {
        let one: Self = One::one();
        let two: Self = one.clone() + one.clone();
        let mut min: Self = Zero::zero();
        let mut max: Self = self.clone();

        while min < max {
            let mid = (min.clone() + max.clone() + one.clone()) / two.clone();
            let mid2 = mid.clone() * mid.clone();
            match mid2.partial_cmp(self).unwrap() {
                Equal => return mid,
                Greater => { max = mid - one.clone() }
                Less    => { min = mid }
            }
        }

        return min
    }

    /// Gets the factorial of the number.
    ///
    /// # Example
    ///
    /// ```rust
    /// use integer::Integer;
    ///
    /// assert_eq!(1u, 0u.factorial());
    /// assert_eq!(1u, 1u.factorial());
    /// assert_eq!(2u, 2u.factorial());
    /// assert_eq!(6u, 3u.factorial());
    /// assert_eq!(24u, 4u.factorial());
    /// ```
    fn factorial(&self) -> Self {
        assert!(*self >= Zero::zero());

        let mut p: Self = One::one();
        let mut i: Self = One::one();
        while i <= *self {
            p = p * i.clone();
            i = i + One::one();
        }
        p
    }

    /// Takes the modular exponentation of the number.
    fn mod_pow(&self, exp: &Self, modulo: &Self) -> Self {
        let zero = Zero::zero();
        let one: Self  = One::one();
        let two: Self  = one.clone() + one.clone();
        if *self == zero { return zero }

        let mut result = one.clone();
        let mut base   = self.clone();
        let mut exp    = exp.clone();
        let     modulo = modulo.clone();

        while exp > zero {
            if exp.is_odd() {
                result = (result * base.clone()) % modulo.clone();
            }
            exp = exp / two.clone();
            base = (base.clone() * base) % modulo.clone();
        }
        result
    }
}

impl Integer for num::BigUint {}
impl Integer for num::BigInt {}
impl Integer for i8 {}
impl Integer for i16 {}
impl Integer for i32 {}
impl Integer for i64 {}
impl Integer for int {}
impl Integer for u8 {}
impl Integer for u16 {}
impl Integer for u32 {}
impl Integer for u64 {}
impl Integer for uint {}

/// An iterator that enumerates each digit of a number.
#[deriving(Clone)]
pub struct Digits<T> { num: T, radix: T, order: T }

impl<T: num::Integer + Clone> Digits<T> {
    fn new(num: T, radix: T) -> Digits<T> {
        let mut order: T;
        if num.is_zero() {
            order = Zero::zero();
        } else {
            order = One::one();
            let mut prod = order.clone() * radix.clone();
            while prod <= num {
                order = prod;
                prod = order.clone() * radix.clone();
            }
        }
        Digits { num: num, radix: radix, order: order }
    }
}

impl<T: num::Integer + Clone> Iterator<T> for Digits<T> {
    #[inline]
    fn next(&mut self) -> Option<T> {
        if self.order.is_zero() { return None; }
        let (d, r) = self.num.div_rem(&self.radix);
        self.num   = d;
        self.order = self.order.clone() / self.radix.clone();
        Some(r)
    }
}

impl<T: num::Integer + Clone> DoubleEndedIterator<T> for Digits<T> {
    #[inline]
    fn next_back(&mut self) -> Option<T> {
        if self.order.is_zero() { return None; }
        let (d, r) = self.num.div_rem(&self.order);
        self.num   = r;
        self.order = self.order.clone() / self.radix.clone();
        Some(d)
    }
}

#[cfg(test)]
mod tests {
    use super::Integer;
    use num;
    use num::Integer as NumInteger;

    #[test]
    fn div() {
        assert_eq!(0u, 0u.div_floor(&3));
        assert_eq!(0u, 0u.div_round(&3));
        assert_eq!(0u, 0u.div_ceil(&3));

        assert_eq!(0u, 1u.div_floor(&3));
        assert_eq!(0u, 1u.div_round(&3));
        assert_eq!(1u, 1u.div_ceil(&3));

        assert_eq!(0u, 2u.div_floor(&3));
        assert_eq!(1u, 2u.div_round(&3));
        assert_eq!(1u, 2u.div_ceil(&3));

        assert_eq!(1u, 3u.div_floor(&3));
        assert_eq!(1u, 3u.div_round(&3));
        assert_eq!(1u, 3u.div_ceil(&3));

        assert_eq!(1u, 4u.div_floor(&3));
        assert_eq!(1u, 4u.div_round(&3));
        assert_eq!(2u, 4u.div_ceil(&3));

        assert_eq!(1u, 5u.div_floor(&3));
        assert_eq!(2u, 5u.div_round(&3));
        assert_eq!(2u, 5u.div_ceil(&3));

        assert_eq!(3u, 5u.div_round(&2));
    }

    #[test]
    fn digits() {
        fn check(n: u32, v: &[u32], radix: u32) {
            assert_eq!(v, n.into_digits(radix).collect::<Vec<_>>()[]);
            let mut rev = n.into_digits(radix).rev().collect::<Vec<_>>();
            rev.reverse();
            assert_eq!(v, rev[])
        }

        check(0, &[], 10);
        check(1, &[1], 10);
        check(3, &[3], 10);
        check(12345, &[5, 4, 3, 2, 1], 10);
        check(0x12345, &[5, 4, 3, 2, 1], 16);
        check(0xabcde, &[0xe, 0xd, 0xc, 0xb, 0xa], 16);
        check(0b11100, &[0, 0, 1, 1, 1], 2);
    }

    #[test]
    fn to_palindromic() {
        assert_eq!(101, 10i.into_palindromic(10, false));
        assert_eq!(1001, 10i.into_palindromic(10, true));

        assert_eq!(99999, 999i.into_palindromic(10, false));
        assert_eq!(999999, 999i.into_palindromic(10, true));

        assert_eq!(99099, 990i.into_palindromic(10, false));
        assert_eq!(990099, 990i.into_palindromic(10, true));

        assert_eq!(1100011, 1100i.into_palindromic(10, false));
        assert_eq!(11000011, 1100i.into_palindromic(10, true));

        assert_eq!(0xabcba, 0xabci.into_palindromic(16, false));
        assert_eq!(0xabccba, 0xabci.into_palindromic(16, true));
    }

    #[test]
    fn is_palindromic() {
        assert!(0i.is_palindromic(10));
        assert!(1i.is_palindromic(10));
        assert!(9i.is_palindromic(10));
        assert!(11i.is_palindromic(10));
        assert!(121i.is_palindromic(10));
        assert!(!123i.is_palindromic(10));
        assert!(1221i.is_palindromic(10));
        assert!(12321i.is_palindromic(10));
    }

    #[test]
    fn mod_pow() {
        for b in range(1u, 10) {
            for e in range(0u, 5) {
                for r in range(10u, 100) {
                    assert_eq!(num::pow(b, e) % r, b.mod_pow(&e, &r));
                }
            }
        }
    }
}
<|endoftext|>


<commit_before>
use {
    AddRoundBorder,
    Borrowed,
    Color,
    Field,
    Line,
    Matrix2d,
    RoundBorderLineColorContext,
    Value,
    View,
};
use vecmath::{
    identity,
};
use internal::{
    CanTransform,
    HasTransform,
};

/// A line context.
pub struct LineColorContext<'a> {
    /// Base/original transform.
    pub base: Field<'a, Matrix2d>,
    /// Current transform.
    pub transform: Field<'a, Matrix2d>,
    /// Current line.
    pub line: Field<'a, Line>,
    /// Current color.
    pub color: Field<'a, Color>,
}

impl<'a> HasTransform<'a, Matrix2d> for LineColorContext<'a> {
    #[inline(always)]
    fn get_transform(&'a self) -> &'a Matrix2d {
        self.transform.get()
    }
}

impl<'a> CanTransform<'a, LineColorContext<'a>, Matrix2d> for LineColorContext<'a> {
    #[inline(always)]
    fn transform(&'a self, value: Matrix2d) -> LineColorContext<'a> {
        LineColorContext {
            base: Borrowed(self.base.get()),
            transform: Value(value),
            line: Borrowed(self.line.get()),
            color: Borrowed(self.color.get()),
        }
    }
}

impl<'a> View<'a> for LineColorContext<'a> {
    #[inline(always)]
    fn view(&'a self) -> LineColorContext<'a> {
        LineColorContext {
            base: Borrowed(self.base.get()),
            transform: Borrowed(self.base.get()),
            line: Borrowed(self.line.get()),
            color: Borrowed(self.color.get()),
        }
    }

    #[inline(always)]
    fn reset(&'a self) -> LineColorContext<'a> {
        LineColorContext {
            base: Borrowed(self.base.get()),
            transform: Value(identity()),
            line: Borrowed(self.line.get()),
            color: Borrowed(self.color.get()),
        }
    }

    #[inline(always)]
    fn store_view(&'a self) -> LineColorContext<'a> {
        LineColorContext {
            base: Borrowed(self.transform.get()),
            transform: Borrowed(self.transform.get()),
            line: Borrowed(self.line.get()),
            color: Borrowed(self.color.get()),
        }
    }
}

impl<'a> AddRoundBorder<'a, RoundBorderLineColorContext<'a>> for LineColorContext<'a> {
    #[inline(always)]
    fn round_border_radius(&'a self, radius: f64) -> RoundBorderLineColorContext<'a> {
        RoundBorderLineColorContext {
            base: Borrowed(self.base.get()),
            transform: Borrowed(self.transform.get()),
            line: Borrowed(self.line.get()),
            round_border_radius: Value(radius),
            color: Borrowed(self.color.get()),
        }
    }
}

<commit_msg>Implemented `Clear` for `LineColorContext`<commit_after>
use {
    AddRoundBorder,
    Borrowed,
    Clear,
    Color,
    Field,
    Line,
    Matrix2d,
    RoundBorderLineColorContext,
    Value,
    View,
};
use vecmath::{
    identity,
};
use internal::{
    CanTransform,
    HasTransform,
};

/// A line context.
pub struct LineColorContext<'a> {
    /// Base/original transform.
    pub base: Field<'a, Matrix2d>,
    /// Current transform.
    pub transform: Field<'a, Matrix2d>,
    /// Current line.
    pub line: Field<'a, Line>,
    /// Current color.
    pub color: Field<'a, Color>,
}

impl<'a> HasTransform<'a, Matrix2d> for LineColorContext<'a> {
    #[inline(always)]
    fn get_transform(&'a self) -> &'a Matrix2d {
        self.transform.get()
    }
}

impl<'a> CanTransform<'a, LineColorContext<'a>, Matrix2d> for LineColorContext<'a> {
    #[inline(always)]
    fn transform(&'a self, value: Matrix2d) -> LineColorContext<'a> {
        LineColorContext {
            base: Borrowed(self.base.get()),
            transform: Value(value),
            line: Borrowed(self.line.get()),
            color: Borrowed(self.color.get()),
        }
    }
}

impl<'a> View<'a> for LineColorContext<'a> {
    #[inline(always)]
    fn view(&'a self) -> LineColorContext<'a> {
        LineColorContext {
            base: Borrowed(self.base.get()),
            transform: Borrowed(self.base.get()),
            line: Borrowed(self.line.get()),
            color: Borrowed(self.color.get()),
        }
    }

    #[inline(always)]
    fn reset(&'a self) -> LineColorContext<'a> {
        LineColorContext {
            base: Borrowed(self.base.get()),
            transform: Value(identity()),
            line: Borrowed(self.line.get()),
            color: Borrowed(self.color.get()),
        }
    }

    #[inline(always)]
    fn store_view(&'a self) -> LineColorContext<'a> {
        LineColorContext {
            base: Borrowed(self.transform.get()),
            transform: Borrowed(self.transform.get()),
            line: Borrowed(self.line.get()),
            color: Borrowed(self.color.get()),
        }
    }
}

impl<'a> AddRoundBorder<'a, RoundBorderLineColorContext<'a>> for LineColorContext<'a> {
    #[inline(always)]
    fn round_border_radius(&'a self, radius: f64) -> RoundBorderLineColorContext<'a> {
        RoundBorderLineColorContext {
            base: Borrowed(self.base.get()),
            transform: Borrowed(self.transform.get()),
            line: Borrowed(self.line.get()),
            round_border_radius: Value(radius),
            color: Borrowed(self.color.get()),
        }
    }
}

impl<'a> Clear for LineColorContext<'a> {
    fn clear<B: BackEnd>(&self, back_end: &mut B) {
        if back_end.supports_clear_rgba() {
            let color = self.color.get();
            back_end.clear_rgba(color[0], color[1], color[2], color[3]);
        }
    }
}

<|endoftext|>


<commit_before>#![cfg(test)]

extern crate env_logger;

use super::*;
use super::super::grammar;
use self::value::{Obj, Value};

const SIMPLE_INIT: &'static str = r#"when (message init) { }"#;
const TWO_MSGS: &'static str = r#"when (message foo, message bar) { }"#;
const SIMPLE_EQUAL: &'static str = r#"when (foo == bar) { unset foo; }"#;
const SIMPLE_NOT_EQUAL: &'static str = r#"when (foo != bar) { set foo bar; }"#;
const SIMPLE_IS_SET: &'static str = r#"when (is_set foo) { unset foo; }"#;

fn setup() {
    let _ = env_logger::init();
}

fn parse_one_match(s: &str) -> ast::Match {
    let mut g = grammar::glop(s).unwrap();
    assert_eq!(g.matches.len(), 1);
    g.matches.pop().unwrap()
}

#[test]
fn unmatched_init_empty_state() {
    setup();
    let m_ast = parse_one_match(SIMPLE_INIT);
    let mut st = State::new(MemStorage::new());
    let m_exc = Match::new_from_ast(&m_ast);
    match st.eval(m_exc).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => (),
    }
}

#[test]
fn matched_init_message() {
    setup();
    let m_ast = parse_one_match(SIMPLE_INIT);
    let mut st = State::new(MemStorage::new());
    st.mut_storage().push_msg("init", Obj::new()).unwrap();
    let m_exc = Match::new_from_ast(&m_ast);
    let txn = match st.eval(m_exc.clone()).unwrap() {
        Some(mut txn) => {
            assert_eq!(txn.seq, 0);
            txn.with_context(|ctx| {
                assert!(ctx.msgs.contains_key("init"));
                assert_eq!(ctx.msgs.len(), 1);
            });
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());

    match st.eval(m_exc.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }
}

#[test]
fn matched_only_init_message() {
    setup();
    let m_ast = parse_one_match(SIMPLE_INIT);
    let mut st = State::new(MemStorage::new());
    st.mut_storage().push_msg("init", Obj::new()).unwrap();
    st.mut_storage()
        .push_msg("blah",
                  [("foo".to_string(), Value::Str("bar".to_string()))]
                      .iter()
                      .cloned()
                      .collect())
        .unwrap();
    let m_exc = Match::new_from_ast(&m_ast);
    let txn = match st.eval(m_exc.clone()).unwrap() {
        Some(mut txn) => {
            assert_eq!(txn.seq, 0);
            txn.with_context(|ref mut ctx| {
                assert!(ctx.msgs.contains_key("init"));
                assert_eq!(ctx.msgs.len(), 1);
            });
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());

    match st.eval(m_exc.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }
}

#[test]
fn matched_two_messages() {
    setup();
    let m_ast = parse_one_match(TWO_MSGS);
    let mut st = State::new(MemStorage::new());
    st.mut_storage().push_msg("foo", Obj::new()).unwrap();
    st.mut_storage().push_msg("bar", Obj::new()).unwrap();
    st.mut_storage().push_msg("foo", Obj::new()).unwrap();
    st.mut_storage().push_msg("bar", Obj::new()).unwrap();
    let m_exc = Match::new_from_ast(&m_ast);

    for i in 0..2 {
        let txn = match st.eval(m_exc.clone()).unwrap() {
            Some(mut txn) => {
                assert_eq!(txn.seq, i);
                txn.with_context(|ref mut ctx| {
                    assert!(ctx.msgs.contains_key("foo"));
                    assert!(ctx.msgs.contains_key("bar"));
                    assert_eq!(ctx.msgs.len(), 2);
                });
                txn
            }
            None => panic!("expected match"),
        };
        assert!(st.commit(txn).is_ok());
    }
    match st.eval(m_exc.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }
}

#[test]
fn match_equal() {
    setup();
    let m_ast = parse_one_match(SIMPLE_EQUAL);
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("bar"));
        let m_exc = Match::new_from_ast(&m_ast);
        let txn = match st.eval(m_exc.clone()).unwrap() {
            Some(txn) => {
                assert_eq!(txn.seq, 0);
                txn
            }
            None => panic!("expected match"),
        };
        assert!(st.commit(txn).is_ok());
        // foo is now unset
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("blah"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
}

#[test]
fn match_not_equal() {
    setup();
    let m_ast = parse_one_match(SIMPLE_NOT_EQUAL);
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("blah"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(txn) => {
                assert_eq!(txn.seq, 0);
            }
            None => panic!("expected match"),
        }
    }
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("bar"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
}

#[test]
fn simple_commit_progression() {
    setup();
    let m_exc_ne = Match::new_from_ast(&parse_one_match(SIMPLE_NOT_EQUAL));
    let m_exc_eq = Match::new_from_ast(&parse_one_match(SIMPLE_EQUAL));
    let mut st = State::new(MemStorage::new());
    st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("blah"));
    // foo starts out != bar so we expect a match and apply
    let txn = match st.eval(m_exc_ne.clone()).unwrap() {
        Some(txn) => {
            assert_eq!(txn.seq, 0);
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());
    // above match sets foo == bar so m_exc_ne no longer matches
    match st.eval(m_exc_ne.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }

    // now let's match on foo == bar, should match committed state now
    let txn = match st.eval(m_exc_eq.clone()).unwrap() {
        Some(txn) => {
            assert_eq!(txn.seq, 1);
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());
}

#[test]
fn match_is_set() {
    setup();
    let m_ast = parse_one_match(SIMPLE_IS_SET);
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("bar"));
        let m_exc = Match::new_from_ast(&m_ast);
        let txn = match st.eval(m_exc.clone()).unwrap() {
            Some(txn) => {
                assert_eq!(txn.seq, 0);
                txn
            }
            None => panic!("expected match"),
        };
        assert!(st.commit(txn).is_ok());

        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("bar".to_string(), Value::from_str("foo"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
}
<commit_msg>Add rollback tests.<commit_after>#![cfg(test)]

extern crate env_logger;

use super::*;
use super::super::grammar;
use self::value::{Obj, Value};

const SIMPLE_INIT: &'static str = r#"when (message init) { }"#;
const TWO_MSGS: &'static str = r#"when (message foo, message bar) { }"#;
const SIMPLE_EQUAL: &'static str = r#"when (foo == bar) { unset foo; }"#;
const SIMPLE_NOT_EQUAL: &'static str = r#"when (foo != bar) { set foo bar; }"#;
const SIMPLE_IS_SET: &'static str = r#"when (is_set foo) { unset foo; }"#;

fn setup() {
    let _ = env_logger::init();
}

fn parse_one_match(s: &str) -> ast::Match {
    let mut g = grammar::glop(s).unwrap();
    assert_eq!(g.matches.len(), 1);
    g.matches.pop().unwrap()
}

#[test]
fn unmatched_init_empty_state() {
    setup();
    let m_ast = parse_one_match(SIMPLE_INIT);
    let mut st = State::new(MemStorage::new());
    let m_exc = Match::new_from_ast(&m_ast);
    match st.eval(m_exc).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => (),
    }
}

#[test]
fn matched_init_message() {
    setup();
    let m_ast = parse_one_match(SIMPLE_INIT);
    let mut st = State::new(MemStorage::new());
    st.mut_storage().push_msg("init", Obj::new()).unwrap();
    let m_exc = Match::new_from_ast(&m_ast);
    let txn = match st.eval(m_exc.clone()).unwrap() {
        Some(mut txn) => {
            assert_eq!(txn.seq, 0);
            txn.with_context(|ctx| {
                assert!(ctx.msgs.contains_key("init"));
                assert_eq!(ctx.msgs.len(), 1);
            });
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());

    match st.eval(m_exc.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }
}

#[test]
fn rollback_msg() {
    setup();
    let m_ast = parse_one_match(SIMPLE_INIT);
    let mut st = State::new(MemStorage::new());
    st.mut_storage().push_msg("init", Obj::new()).unwrap();
    let m_exc = Match::new_from_ast(&m_ast);
    let txn = match st.eval(m_exc.clone()).unwrap() {
        Some(mut txn) => {
            assert_eq!(txn.seq, 0);
            txn.with_context(|ctx| {
                assert!(ctx.msgs.contains_key("init"));
                assert_eq!(ctx.msgs.len(), 1);
            });
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.rollback(txn).is_ok());

    // init message should have been nak'ed on rollback
    let txn = match st.eval(m_exc.clone()).unwrap() {
        Some(mut txn) => {
            assert_eq!(txn.seq, 0);
            txn.with_context(|ctx| {
                assert!(ctx.msgs.contains_key("init"));
                assert_eq!(ctx.msgs.len(), 1);
            });
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());

    // Now init message has been consumed.
    match st.eval(m_exc.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }
}

#[test]
fn matched_only_init_message() {
    setup();
    let m_ast = parse_one_match(SIMPLE_INIT);
    let mut st = State::new(MemStorage::new());
    st.mut_storage().push_msg("init", Obj::new()).unwrap();
    st.mut_storage()
        .push_msg("blah",
                  [("foo".to_string(), Value::Str("bar".to_string()))]
                      .iter()
                      .cloned()
                      .collect())
        .unwrap();
    let m_exc = Match::new_from_ast(&m_ast);
    let txn = match st.eval(m_exc.clone()).unwrap() {
        Some(mut txn) => {
            assert_eq!(txn.seq, 0);
            txn.with_context(|ref mut ctx| {
                assert!(ctx.msgs.contains_key("init"));
                assert_eq!(ctx.msgs.len(), 1);
            });
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());

    match st.eval(m_exc.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }
}

#[test]
fn matched_two_messages() {
    setup();
    let m_ast = parse_one_match(TWO_MSGS);
    let mut st = State::new(MemStorage::new());
    st.mut_storage().push_msg("foo", Obj::new()).unwrap();
    st.mut_storage().push_msg("bar", Obj::new()).unwrap();
    st.mut_storage().push_msg("foo", Obj::new()).unwrap();
    st.mut_storage().push_msg("bar", Obj::new()).unwrap();
    let m_exc = Match::new_from_ast(&m_ast);

    for i in 0..2 {
        let txn = match st.eval(m_exc.clone()).unwrap() {
            Some(mut txn) => {
                assert_eq!(txn.seq, i);
                txn.with_context(|ref mut ctx| {
                    assert!(ctx.msgs.contains_key("foo"));
                    assert!(ctx.msgs.contains_key("bar"));
                    assert_eq!(ctx.msgs.len(), 2);
                });
                txn
            }
            None => panic!("expected match"),
        };
        assert!(st.commit(txn).is_ok());
    }
    match st.eval(m_exc.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }
}

#[test]
fn match_equal() {
    setup();
    let m_ast = parse_one_match(SIMPLE_EQUAL);
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("bar"));
        let m_exc = Match::new_from_ast(&m_ast);
        let txn = match st.eval(m_exc.clone()).unwrap() {
            Some(txn) => {
                assert_eq!(txn.seq, 0);
                txn
            }
            None => panic!("expected match"),
        };
        assert!(st.commit(txn).is_ok());
        // foo is now unset
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("blah"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
}

#[test]
fn rollback_vars() {
    setup();
    let m_ast = parse_one_match(SIMPLE_EQUAL);
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("bar"));
        let m_exc = Match::new_from_ast(&m_ast);
        let txn = match st.eval(m_exc.clone()).unwrap() {
            Some(txn) => {
                assert_eq!(txn.seq, 0);
                txn
            }
            None => panic!("expected match"),
        };
        assert!(st.rollback(txn).is_ok());
        // foo should be still set
        let txn = match st.eval(m_exc.clone()).unwrap() {
            Some(txn) => {
                assert_eq!(txn.seq, 0);
                txn
            }
            None => panic!("expected match"),
        };
        assert!(st.commit(txn).is_ok());
        // foo is now unset
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("blah"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
}

#[test]
fn match_not_equal() {
    setup();
    let m_ast = parse_one_match(SIMPLE_NOT_EQUAL);
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("blah"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(txn) => {
                assert_eq!(txn.seq, 0);
            }
            None => panic!("expected match"),
        }
    }
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("bar"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
}

#[test]
fn simple_commit_progression() {
    setup();
    let m_exc_ne = Match::new_from_ast(&parse_one_match(SIMPLE_NOT_EQUAL));
    let m_exc_eq = Match::new_from_ast(&parse_one_match(SIMPLE_EQUAL));
    let mut st = State::new(MemStorage::new());
    st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("blah"));
    // foo starts out != bar so we expect a match and apply
    let txn = match st.eval(m_exc_ne.clone()).unwrap() {
        Some(txn) => {
            assert_eq!(txn.seq, 0);
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());
    // above match sets foo == bar so m_exc_ne no longer matches
    match st.eval(m_exc_ne.clone()).unwrap() {
        Some(_) => panic!("unexpected match"),
        None => {}
    }

    // now let's match on foo == bar, should match committed state now
    let txn = match st.eval(m_exc_eq.clone()).unwrap() {
        Some(txn) => {
            assert_eq!(txn.seq, 1);
            txn
        }
        None => panic!("expected match"),
    };
    assert!(st.commit(txn).is_ok());
}

#[test]
fn match_is_set() {
    setup();
    let m_ast = parse_one_match(SIMPLE_IS_SET);
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("foo".to_string(), Value::from_str("bar"));
        let m_exc = Match::new_from_ast(&m_ast);
        let txn = match st.eval(m_exc.clone()).unwrap() {
            Some(txn) => {
                assert_eq!(txn.seq, 0);
                txn
            }
            None => panic!("expected match"),
        };
        assert!(st.commit(txn).is_ok());

        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
    {
        let mut st = State::new(MemStorage::new());
        st.mut_storage().mut_vars().insert("bar".to_string(), Value::from_str("foo"));
        let m_exc = Match::new_from_ast(&m_ast);
        match st.eval(m_exc.clone()).unwrap() {
            Some(_) => panic!("unexpected match"),
            None => {}
        }
    }
}
<|endoftext|>


<commit_before>use super::super::std;

use std::fmt;
use super::super::ring::{digest, hmac};
use super::iron::prelude::*;
use super::iron::{status, BeforeMiddleware};
use super::super::rustc_serialize::hex::FromHex;
use super::bodyparser;

#[derive(Debug)]
struct StringError(String);

impl fmt::Display for StringError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl std::error::Error for StringError {
    fn description(&self) -> &str { &*self.0 }
}

pub struct GithubWebhookVerifier {
    pub secret: String,
}

impl GithubWebhookVerifier {
    fn is_valid(&self, body: &[u8], signature: String) -> IronResult<()> {
        // assume it starts with 'sha1='
        if signature.len() < 6 {
            return Err(IronError::new(StringError("Invalid signature value".to_string()), status::BadRequest));
        }
        let sig_prefix = &signature[0..5];
        if sig_prefix != "sha1=" {
            return Err(IronError::new(StringError(format!("Invalid signature value. Expected 'sha1='; found: '{}'", sig_prefix)), status::BadRequest));
        }

        let sig_bytes: Vec<u8> = match signature[5..].from_hex() {
            Ok(s) => s,
            Err(e) => return Err(IronError::new(StringError(format!("Invalid hex value: {}", e)), status::BadRequest)),
        };

        let key = hmac::VerificationKey::new(&digest::SHA1, self.secret.as_bytes());
        match hmac::verify(&key, body, &sig_bytes) {
            Ok(_) => Ok(()),
            Err(_) => Err(IronError::new(StringError("Invalid signature!".to_string()), status::BadRequest))
        }
    }
}

impl BeforeMiddleware for GithubWebhookVerifier {
    fn before(&self, req: &mut Request) -> IronResult<()> {
        let sig_header: String = match req.headers.get_raw("x-hub-signature") {
            Some(h) => {
                if h.len() == 0 || h.len() > 1 {
                    return Err(IronError::new(StringError("Expected to find exactly one signature header".to_string()), status::BadRequest))
                } else {
                    String::from_utf8_lossy(&h[0]).into_owned()
                }
            },
            None => return Err(IronError::new(StringError("Expected to find exactly one signature header".to_string()), status::BadRequest)),
        };

        let body = match req.get::<bodyparser::Raw>() {
            Ok(Some(body)) => body,
            Ok(None) => String::new(),
            Err(e) => return Err(IronError::new(StringError(format!("Error reading body: {}", e)), status::InternalServerError)),
        };

        self.is_valid(body.as_bytes(), sig_header)
    }
}

<commit_msg>Add tests for github_verify<commit_after>use super::super::std;

use std::fmt;
use super::super::ring::{digest, hmac};
use super::iron::prelude::*;
use super::iron::{status, BeforeMiddleware};
use super::super::rustc_serialize::hex::FromHex;
use super::bodyparser;

#[derive(Debug)]
struct StringError(String);

impl fmt::Display for StringError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl std::error::Error for StringError {
    fn description(&self) -> &str { &*self.0 }
}

pub struct GithubWebhookVerifier {
    pub secret: String,
}

impl GithubWebhookVerifier {
    fn is_valid(&self, data: &[u8], signature: &String) -> IronResult<()> {
        // assume it starts with 'sha1='
        if signature.len() < 6 {
            return Err(IronError::new(StringError("Invalid signature value".to_string()), status::BadRequest));
        }
        let sig_prefix = &signature[0..5];
        if sig_prefix != "sha1=" {
            return Err(IronError::new(StringError(format!("Invalid signature value. Expected 'sha1='; found: '{}'", sig_prefix)), status::BadRequest));
        }

        let sig_bytes: Vec<u8> = match signature[5..].from_hex() {
            Ok(s) => s,
            Err(e) => return Err(IronError::new(StringError(format!("Invalid hex value: {}", e)), status::BadRequest)),
        };

        let key = hmac::VerificationKey::new(&digest::SHA1, self.secret.as_bytes());
        match hmac::verify(&key, data, &sig_bytes) {
            Ok(_) => Ok(()),
            Err(_) => Err(IronError::new(StringError("Invalid signature!".to_string()), status::BadRequest))
        }
    }
}

impl BeforeMiddleware for GithubWebhookVerifier {
    fn before(&self, req: &mut Request) -> IronResult<()> {
        let sig_header: String = match req.headers.get_raw("x-hub-signature") {
            Some(h) => {
                if h.len() == 0 || h.len() > 1 {
                    return Err(IronError::new(StringError("Expected to find exactly one signature header".to_string()), status::BadRequest))
                } else {
                    String::from_utf8_lossy(&h[0]).into_owned()
                }
            },
            None => return Err(IronError::new(StringError("Expected to find exactly one signature header".to_string()), status::BadRequest)),
        };

        let body = match req.get::<bodyparser::Raw>() {
            Ok(Some(body)) => body,
            Ok(None) => String::new(),
            Err(e) => return Err(IronError::new(StringError(format!("Error reading body: {}", e)), status::InternalServerError)),
        };

        self.is_valid(body.as_bytes(), &sig_header)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::super::super::ring::{digest, hmac};
    use super::super::super::rustc_serialize::hex::ToHex;

    #[test]
    fn verify_sig_valid() {
        let key_value = String::from("this is my secret key!");
        let key = hmac::SigningKey::new(&digest::SHA1, key_value.as_bytes());

        let msg = "a message from the githubs.";
        let signature = hmac::sign(&key, msg.as_bytes());
        let signature_hex = "sha1=".to_string() + signature.as_ref().to_hex().as_str();

        let verifier = GithubWebhookVerifier { secret: key_value.clone() };

        assert!(verifier.is_valid(msg.as_bytes(), &signature_hex).is_ok());
    }

    #[test]
    fn verify_sig_wrong_digest() {
        let key_value = String::from("this is my secret key!");
        let key = hmac::SigningKey::new(&digest::SHA1, key_value.as_bytes());

        let msg = "a message from the githubs.";
        let signature = hmac::sign(&key, msg.as_bytes());
        let signature_hex = "sha9=".to_string() + signature.as_ref().to_hex().as_str();

        let verifier = GithubWebhookVerifier { secret: key_value.clone() };

        assert!(verifier.is_valid(msg.as_bytes(), &signature_hex).is_err());
    }

    #[test]
    fn verify_sig_missing_header() {
        let key_value = String::from("this is my secret key!");
        let key = hmac::SigningKey::new(&digest::SHA1, key_value.as_bytes());

        let msg = "a message from the githubs.";
        let signature = hmac::sign(&key, msg.as_bytes());
        let signature_hex = signature.as_ref().to_hex();

        let verifier = GithubWebhookVerifier { secret: key_value.clone() };

        assert!(verifier.is_valid(msg.as_bytes(), &signature_hex).is_err());
    }
}
<|endoftext|>