<commit_before>#![feature(core_intrinsics, patchpoint_call_intrinsic, stackmap_call_intrinsic)]
#![feature(question_mark)]
#![feature(type_ascription)]
#![feature(rustc_attrs)]
#![allow(unused_features)]

use std::intrinsics;

#[allow(dead_code)]
mod byteorder;
#[allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod elf;

mod llvm_stackmaps;

fn subcall(data: *mut u8) {
    println!("Enter `subcall`");
    let mut i = 0;
    let mut saw_one = false;
    print!("[");
    let mut datum = unsafe { *data.offset(i) as char };
    while datum != '\0' {
        if saw_one {
            print!(", ");
        }
        print!("{}", datum);
        saw_one = true;
        i += 1;
        datum = unsafe { *data.offset(i) as char };
    }
    println!("]");
    println!("Finis `subcall`");
}

fn main() {
    demo().unwrap();
}

#[derive(Debug)]
enum DemoError {
    ParseError(elf::ParseError),
    MissingSection(String),
}

impl std::convert::From<elf::ParseError> for DemoError {
    fn from(x: elf::ParseError) -> Self { DemoError::ParseError(x) }
}

#[rustc_mir(borrowck_graphviz_postflow="/tmp/foo.dot")]
fn demo() -> Result<(), DemoError> {
    use std::path::Path;
    use self::llvm_stackmaps::StackMap;
    use self::byteorder::LittleEndian;

    // FIXME: this is not a reliable way to extract the executable file path.
    // (look into alternatives)
    let binary = std::env::args().next().unwrap();

    println!("Hello World from {}", binary);

    let path = Path::new(&binary);
    let file = elf::File::open_path(&path)?;

    let stackmap_section = {
        let name = ".llvm_stackmaps".to_string();
        file.get_section(name.clone())
            .ok_or_else(|| DemoError::MissingSection(name))?
    };

    println!("stackmap_section: {:?}", stackmap_section);

    let stack_map = StackMap::read_from::<LittleEndian>(&mut &stackmap_section.data[..]);
    println!("stack_map: {:?}", stack_map);

    let mut data = vec![b'h', b'e', b'l', b'l', b'o', 0];
    println!("path addr: {:?}", &path as *const _);
    println!("file addr: {:?}", &file as *const _);
    println!("data addr: {:?}", &data as *const _);
    unsafe {
        intrinsics::stackmap_call(0, 13, subcall, data.as_mut_ptr());
    }

    println!("Goodbye World");

    Ok(())
}
<commit_msg>hook in backtrace hack module.<commit_after>#![feature(core_intrinsics, patchpoint_call_intrinsic, stackmap_call_intrinsic)]
#![feature(question_mark)]
#![feature(type_ascription)]
#![feature(rustc_attrs)]
#![feature(libc)]
#![feature(const_fn)]
#![allow(unused_features)]
extern crate libc;

use std::intrinsics;

#[allow(dead_code)]
mod byteorder;
#[allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod elf;

mod backtrace_hack;
mod llvm_stackmaps;

fn subcall(data: *mut u8) {
    println!("Enter `subcall`");
    let mut i = 0;
    let mut saw_one = false;
    print!("[");
    let mut datum = unsafe { *data.offset(i) as char };
    while datum != '\0' {
        if saw_one {
            print!(", ");
        }
        print!("{}", datum);
        saw_one = true;
        i += 1;
        datum = unsafe { *data.offset(i) as char };
    }
    println!("]");
    println!("Finis `subcall`");
}

static mut MAIN_LOCAL: usize = 0;
pub fn main_height() -> usize { unsafe { MAIN_LOCAL } }

fn main() {
    let local: u32 = 0;
    unsafe { MAIN_LOCAL = &local as *const _ as usize; }
    demo().unwrap();
}

#[derive(Debug)]
enum DemoError {
    ParseError(elf::ParseError),
    MissingSection(String),
}

impl std::convert::From<elf::ParseError> for DemoError {
    fn from(x: elf::ParseError) -> Self { DemoError::ParseError(x) }
}

#[rustc_mir(borrowck_graphviz_postflow="/tmp/foo.dot")]
fn demo() -> Result<(), DemoError> {
    use std::path::Path;
    use libc::{c_char, c_int, c_void};
    use self::llvm_stackmaps::StackMap;
    use self::byteorder::LittleEndian;

    // FIXME: this is not a reliable way to extract the executable file path.
    // (look into alternatives)
    let binary = std::env::args().next().unwrap();

    println!("Hello World from {}", binary);

    let path = Path::new(&binary);
    let file = elf::File::open_path(&path)?;

    let stackmap_section = {
        let name = ".llvm_stackmaps".to_string();
        file.get_section(name.clone())
            .ok_or_else(|| DemoError::MissingSection(name))?
    };

    println!("stackmap_section: {:?}", stackmap_section);

    let stack_map = StackMap::read_from::<LittleEndian>(&mut &stackmap_section.data[..]);
    println!("stack_map: {:?}", stack_map);

    let mut data = vec![b'h', b'e', b'l', b'l', b'o', 0];
    println!("path addr: {:?}", &path as *const _);
    println!("file addr: {:?}", &file as *const _);
    println!("data addr: {:?}", &data as *const _);

    println!("conservative stack_height: {}", backtrace_hack::stack_height());
    let addresses = backtrace_hack::backtrace_return_addresses();
    println!("backtrace:      {:?}", addresses);
    println!("backtrace map*: {:?}", {
        let derefed: Vec<_> = addresses.iter()
            .map(|p| unsafe { *(*p as *const *mut c_void) })
            .collect();
        derefed
    });
    unsafe {
        intrinsics::stackmap_call(0, 13, subcall, data.as_mut_ptr());
    }

    println!("Goodbye World");

    Ok(())
}
<|endoftext|>


<commit_before>use num_bigint::{BigInt, BigUint};
use num_traits::cast::ToPrimitive;
use num_traits::identities::Zero;
use num_traits::sign::Signed;
use smallvec::{Array, SmallVec};
use std::fmt;
use std::str::from_utf8_unchecked;

/// Prints BigInt in Base62
pub struct Bi62<'a> (pub &'a BigInt);
impl<'a> fmt::Display for Bi62<'a> {
  fn fmt (&self, ft: &mut fmt::Formatter) -> fmt::Result {
    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    if let Err (_) = base62enc (self.0, &mut buf) {return Err (fmt::Error)}
    ft.write_str (unsafe {from_utf8_unchecked (&buf)})}}

/// Prints u64 in Base62
pub struct U62 (pub u64);
impl fmt::Display for U62 {
  fn fmt (&self, ft: &mut fmt::Formatter) -> fmt::Result {
    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    if let Err (_) = base62uenc (self.0, &mut buf) {return Err (fmt::Error)}
    ft.write_str (unsafe {from_utf8_unchecked (&buf)})}}

pub const BASE62ALPH: &[u8; 62] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

pub fn base62uenc<A: Array<Item=u8>> (mut ui: u64, sv: &mut SmallVec<A>) -> Result<(), String> {
  let start = sv.len();

  loop {
    sv.push (BASE62ALPH[(ui % 62) as usize]);
    ui /= 62; if ui == 0 {break}}

  let end = sv.len();
  &sv[start..end].reverse();
  Ok(())}

pub fn base62enc<A: Array<Item=u8>> (bi: &BigInt, sv: &mut SmallVec<A>) -> Result<(), String> {
  // cf. https://github.com/kryptco/base62.rs

  if let Some (u64v) = bi.to_u64() {return base62uenc (u64v, sv)}

  let mut bu = if bi.is_negative() {
    sv.push (b'-');
    bi.abs().to_biguint()
  } else {
    bi.to_biguint()
  }.unwrap();

  let start = sv.len();

  let base: BigUint = 62u32.into();
  loop {
    sv.push (BASE62ALPH[(&bu % &base).to_usize().unwrap()]);
    bu /= &base; if bu.is_zero() {break}}

  let end = sv.len();
  &sv[start..end].reverse();
  Ok(())}

/// Decode a base62 number
pub fn base62udec (bv: &[u8]) -> Result<u64, String> {
  let mut uv = 0;
  for &ch in bv.iter() {
    let v = if ch >= b'0' && ch <= b'9' {
      ch - b'0'
    } else if ch >= b'A' && ch <= b'Z' {
      ch - b'A' + 10
    } else if ch >= b'a' && ch <= b'z' {
      ch - b'a' + 36
    } else {
      return ERR! ("!base62: {}", ch)};
    uv = uv * 62 + v as u64}
  Ok (uv)}

#[cfg(all(test, feature = "nightly"))] mod test {
  extern crate test;

  use num_bigint::BigInt;
  use smallvec::SmallVec;
  use super::*;

  #[test] fn base62encᵗ() {
    // cf. https://www.dcode.fr/base-n-convert

    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    base62enc (&0.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"0");

    buf.clear();
    base62enc (&1.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"1");

    buf.clear();
    base62enc (&10.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"A");

    buf.clear();
    base62enc (&61.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"z");

    buf.clear();
    base62enc (&62.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"10");

    buf.clear();
    base62enc (&63.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"11");

    buf.clear();
    base62enc (&123.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"1z");

    buf.clear();
    base62enc (&124.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"20");

    buf.clear();
    base62enc (&999999999.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"15ftgF");

    assert! (format! ("{}", Bi62 (&999999999.into())) == "15ftgF");

    assert! (format! ("{}", Bi62 (&(-1).into())) == "-1");
    assert! (format! ("{}", Bi62 (&(-123).into())) == "-1z");
    assert! (format! ("{}", Bi62 (&(-124).into())) == "-20");
    assert! (format! ("{}", Bi62 (&(-999999999).into())) == "-15ftgF")}

  // RUSTFLAGS=-Ctarget-cpu=native cargo bench

  #[bench] fn base62uencᵇ (bm: &mut test::Bencher) {
    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    bm.iter (|| {
      buf.clear();
      base62uenc (999999999, &mut buf) .unwrap()});
    assert! (&buf[..] == b"15ftgF");
    assert_eq! (base62udec (b"15ftgF"), Ok (999999999))}

  #[bench] fn base62encᵇ (bm: &mut test::Bencher) {
    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    let bi: BigInt = 999999999.into();
    bm.iter (|| {
      buf.clear();
      base62enc (&bi, &mut buf) .unwrap()});
    assert! (&buf[..] == b"15ftgF")}

  #[bench] fn base36encᵇ (bm: &mut test::Bencher) {
    let bi: BigInt = 999999999.into();
    let mut buf = String::new();
    bm.iter (|| {
      buf = bi.to_str_radix (36);
      test::black_box (&buf);});
    assert! (buf == "gjdgxr")}
}
<commit_msg>Note divergence from JS<commit_after>use num_bigint::{BigInt, BigUint};
use num_traits::cast::ToPrimitive;
use num_traits::identities::Zero;
use num_traits::sign::Signed;
use smallvec::{Array, SmallVec};
use std::fmt;
use std::str::from_utf8_unchecked;

/// Prints BigInt in Base62
pub struct Bi62<'a> (pub &'a BigInt);
impl<'a> fmt::Display for Bi62<'a> {
  fn fmt (&self, ft: &mut fmt::Formatter) -> fmt::Result {
    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    if let Err (_) = base62enc (self.0, &mut buf) {return Err (fmt::Error)}
    ft.write_str (unsafe {from_utf8_unchecked (&buf)})}}

/// Prints u64 in Base62
pub struct U62 (pub u64);
impl fmt::Display for U62 {
  fn fmt (&self, ft: &mut fmt::Formatter) -> fmt::Result {
    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    if let Err (_) = base62uenc (self.0, &mut buf) {return Err (fmt::Error)}
    ft.write_str (unsafe {from_utf8_unchecked (&buf)})}}

/// NB: Incompatible (uses a different character order) with the JavaScript base62,
/// https://github.com/base62/base62.js/blob/72a89c57c7c4da4552150467967b091da9f41f7f/lib/ascii.js#L3
pub const BASE62ALPH: &[u8; 62] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

pub fn base62uenc<A: Array<Item=u8>> (mut ui: u64, sv: &mut SmallVec<A>) -> Result<(), String> {
  let start = sv.len();

  loop {
    sv.push (BASE62ALPH[(ui % 62) as usize]);
    ui /= 62; if ui == 0 {break}}

  let end = sv.len();
  &sv[start..end].reverse();
  Ok(())}

pub fn base62enc<A: Array<Item=u8>> (bi: &BigInt, sv: &mut SmallVec<A>) -> Result<(), String> {
  // cf. https://github.com/kryptco/base62.rs

  if let Some (u64v) = bi.to_u64() {return base62uenc (u64v, sv)}

  let mut bu = if bi.is_negative() {
    sv.push (b'-');
    bi.abs().to_biguint()
  } else {
    bi.to_biguint()
  }.unwrap();

  let start = sv.len();

  let base: BigUint = 62u32.into();
  loop {
    sv.push (BASE62ALPH[(&bu % &base).to_usize().unwrap()]);
    bu /= &base; if bu.is_zero() {break}}

  let end = sv.len();
  &sv[start..end].reverse();
  Ok(())}

/// Decode a base62 number
pub fn base62udec (bv: &[u8]) -> Result<u64, String> {
  let mut uv = 0;
  for &ch in bv.iter() {
    let v = if ch >= b'0' && ch <= b'9' {
      ch - b'0'
    } else if ch >= b'A' && ch <= b'Z' {
      ch - b'A' + 10
    } else if ch >= b'a' && ch <= b'z' {
      ch - b'a' + 36
    } else {
      return ERR! ("!base62: {}", ch)};
    uv = uv * 62 + v as u64}
  Ok (uv)}

#[cfg(all(test, feature = "nightly"))] mod test {
  extern crate test;

  use num_bigint::BigInt;
  use smallvec::SmallVec;
  use super::*;

  #[test] fn base62encᵗ() {
    // cf. https://www.dcode.fr/base-n-convert

    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    base62enc (&0.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"0");

    buf.clear();
    base62enc (&1.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"1");

    buf.clear();
    base62enc (&10.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"A");

    buf.clear();
    base62enc (&61.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"z");

    buf.clear();
    base62enc (&62.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"10");

    buf.clear();
    base62enc (&63.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"11");

    buf.clear();
    base62enc (&123.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"1z");

    buf.clear();
    base62enc (&124.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"20");

    buf.clear();
    base62enc (&999999999.into(), &mut buf) .unwrap();
    assert! (&buf[..] == b"15ftgF");

    assert! (format! ("{}", Bi62 (&999999999.into())) == "15ftgF");

    assert! (format! ("{}", Bi62 (&(-1).into())) == "-1");
    assert! (format! ("{}", Bi62 (&(-123).into())) == "-1z");
    assert! (format! ("{}", Bi62 (&(-124).into())) == "-20");
    assert! (format! ("{}", Bi62 (&(-999999999).into())) == "-15ftgF")}

  // RUSTFLAGS=-Ctarget-cpu=native cargo bench

  #[bench] fn base62uencᵇ (bm: &mut test::Bencher) {
    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    bm.iter (|| {
      buf.clear();
      base62uenc (999999999, &mut buf) .unwrap()});
    assert! (&buf[..] == b"15ftgF");
    assert_eq! (base62udec (b"15ftgF"), Ok (999999999))}

  #[bench] fn base62encᵇ (bm: &mut test::Bencher) {
    let mut buf: SmallVec<[u8; 32]> = SmallVec::new();
    let bi: BigInt = 999999999.into();
    bm.iter (|| {
      buf.clear();
      base62enc (&bi, &mut buf) .unwrap()});
    assert! (&buf[..] == b"15ftgF")}

  #[bench] fn base36encᵇ (bm: &mut test::Bencher) {
    let bi: BigInt = 999999999.into();
    let mut buf = String::new();
    bm.iter (|| {
      buf = bi.to_str_radix (36);
      test::black_box (&buf);});
    assert! (buf == "gjdgxr")}
}
<|endoftext|>


<commit_before>// test main
extern crate twobit;
use std::os;
use twobit::TwoBit;

fn print_sequence(seq: &str) {
   let mut i = 0;

   for base in seq.chars() {
     if i != 0 && (i % 50) == 0 {
       println!("");
     }
     print!("{}", base);
     i = i + 1;
   }
   println!("");
}

fn main() {
  let args = os::args();

	match args.as_slice() {
		[ _, ref filename, ref chrom, ref start, ref end ] => {

			let start = from_str::<u32>(start.as_slice()).expect("Invalid start coordinate");
			let end = from_str::<u32>(end.as_slice()).expect("Invalid end coordinate");

			let tb = TwoBit::new(filename.as_slice());

			match tb {
				Ok(tbv) => {
					// get chromosome size
 					match tbv.sequence_len(chrom.as_slice()) {
   					Some(n) => println!("{}: size = {}", chrom, n),
   					None => {
     					println!("unknown sequence: {}", chrom);
     					return;
   					}
 					};

					let seq = tbv.sequence(chrom.as_slice(), start, end);
					match seq {
    	    	Some(seqstr) => {
      	    	println!(">{}:{}-{}", chrom, start, end + 1);
     					//println!("{}", seqstr);
     					print_sequence(seqstr.as_slice());
   					},
   					None => println!("nothing")
 					};
				},
				Err(std::io::IoError{ kind: _, desc: x, detail: _}) => println!("{}: {}", x, filename)
			}
		},
		[ ref prog, ..] => println!("Usage: {} <2bit filename> <name> <start> <end>", prog),
		_ => fail!()
	}
}
<commit_msg>Indentation fixes<commit_after>// test main
extern crate twobit;
use std::os;
use twobit::TwoBit;

fn print_sequence(seq: &str) {
   let mut i = 0;

   for base in seq.chars() {
     if i != 0 && (i % 50) == 0 {
       println!("");
     }
     print!("{}", base);
     i = i + 1;
   }
   println!("");
}

fn main() {
  let args = os::args();

	match args.as_slice() {
		[ _, ref filename, ref chrom, ref start, ref end ] => {

			let start = from_str::<u32>(start.as_slice()).expect("Invalid start coordinate");
			let end = from_str::<u32>(end.as_slice()).expect("Invalid end coordinate");

			let tb = TwoBit::new(filename.as_slice());

			match tb {
				Ok(tbv) => {
					// get chromosome size
 					match tbv.sequence_len(chrom.as_slice()) {
   						Some(n) => println!("{}: size = {}", chrom, n),
   						None => {
     						println!("unknown sequence: {}", chrom);
     						return;
   						}
 					};

					let seq = tbv.sequence(chrom.as_slice(), start, end);
					match seq {
    	    			Some(seqstr) => {
      	    			println!(">{}:{}-{}", chrom, start, end + 1);
     					//println!("{}", seqstr);
     					print_sequence(seqstr.as_slice());
   					},
   					None => println!("nothing")
 					};
				},
				Err(std::io::IoError{ kind: _, desc: x, detail: _}) => println!("{}: {}", x, filename)
			}
		},
		[ ref prog, ..] => println!("Usage: {} <2bit filename> <name> <start> <end>", prog),
		_ => fail!()
	}
}
<|endoftext|>


<commit_before>use std::rc::{Rc, Weak};
use std::cell::RefCell;
use std::borrow::Borrow;
use dovahkiin::types::custom_types::id::Id;
use smallvec::SmallVec;

type EntryKey = SmallVec<[u8; 16]>;

pub struct LSMTree {
    num_levels: u8,
    levels: Vec<Rc<BTree>>
}

trait BTree {
    fn root(&self) -> &Node;
    fn root_mut(&self) -> &mut Node;
    fn get_height(&self) -> u32;
    fn set_height(&mut self) -> u32;
    fn get_num_nodes(&self) -> u32;
    fn set_num_nodes(&self) -> u32;
    fn get(&self, key: &EntryKey) -> Option<&Id> {
        self.search(self.root(), key, self.get_height())
    }
    fn search<'a>(&self, node: &'a Node, key: &EntryKey, ht: u32) -> Option<&'a Id> {
        let children = &node.entries();
        match node.entries().binary_search_by(|e| e.key.cmp(key)) {
            Ok(pos) => return Some(&node.entries()[pos].val),
            Err(pos) => {
                if ht == 0 {
                    return None
                }
                else {
                    match &node.delimiters()[pos] {
                        Delimiter::External(id) => unimplemented!(), // TODO: read page from remote
                        Delimiter::Internal(node) => return self.search(node.borrow(), key, ht - 1)
                    }
                }
            }
        }
    }
}

struct Entry {
    key: EntryKey,
    val: Id
}

enum Delimiter {
    External(Id),
    Internal(Box<Node>)
}

trait Array<T> {
    #[inline]
    fn index_of(&self, index: usize) -> &T;
    #[inline]
    fn size(&self) -> usize;
}

trait Entries : Array<Entry> {}

trait Delimiters : Array<Delimiter> {}

trait Node {
    fn id(&self) -> &Id;
    fn entries(&self) -> &[Entry];
    fn delimiters(&self) -> &[Delimiter];
    fn entries_mut(&mut self) -> &mut [Entry];
    fn delimiters_mut(&mut self) -> &mut [Delimiter];
}

macro_rules! impl_nodes {
    ($(($level: ident, $entry_size: expr, $delimiter_size: expr)),+) => {
        $(
            mod $level {
                use super::*;

                type LEntries = [Entry; $entry_size];

                impl Array<Entry> for LEntries {
                    #[inline]
                    fn size(&self) -> usize { $entry_size }
                    #[inline]
                    fn index_of(&self, index: usize) -> &Entry { &self[index] }
                }

                type LDelimiter = [Delimiter; $delimiter_size];

                impl Array<Delimiter> for LDelimiter {
                    #[inline]
                    fn size(&self) -> usize { $delimiter_size }
                    #[inline]
                    fn index_of(&self, index: usize) -> &Delimiter { &self[index] }
                }

                impl Delimiters for LDelimiter {}

                struct LNode {
                    id: Id,
                    entries: LEntries,
                    delimeters: LDelimiter,
                }

                impl Node for LNode {
                    #[inline]
                    fn id(&self) -> &Id {
                        &self.id
                    }
                    #[inline]
                    fn entries(&self) -> &[Entry] {
                        &self.entries
                    }
                    #[inline]
                    fn delimiters(&self) -> &[Delimiter] {
                        &self.delimeters
                    }
                    #[inline]
                    fn entries_mut(&mut self) -> &mut [Entry] {
                        &mut self.entries
                    }
                    #[inline]
                    fn delimiters_mut(&mut self) -> &mut [Delimiter] {
                        &mut self.delimeters
                    }
                }
            }
        )+
    };
}

impl_nodes!((level_0, 2, 3), (level_1, 20, 21), (level_2, 200, 201), (level_3, 2000, 2001), (level_4, 20000, 20001));

//impl BTree {
//    pub fn new(max_entries: u64) -> BTree {
//        BTree {
//            node_size: 0,
//            height: 0,
//            max_entries,
//            root: Node::new(node_size)
//        }
//    }
//}
//
//impl Node {
//    pub fn new(node_size: u16) -> Rc<Node> {
//        Rc::new(Node {
//            id: Id::unit_id(),
//            children: [u8; 16]
//        })
//    }
//}<commit_msg>cope with failing search<commit_after>use std::rc::{Rc, Weak};
use std::cell::RefCell;
use std::borrow::Borrow;
use dovahkiin::types::custom_types::id::Id;
use smallvec::SmallVec;

type EntryKey = SmallVec<[u8; 16]>;

pub struct LSMTree {
    num_levels: u8,
    levels: Vec<Rc<BTree>>
}

trait BTree {
    fn root(&self) -> &Node;
    fn root_mut(&self) -> &mut Node;
    fn get_height(&self) -> u32;
    fn set_height(&mut self) -> u32;
    fn get_num_nodes(&self) -> u32;
    fn set_num_nodes(&self) -> u32;
    fn get(&self, key: &EntryKey) -> Option<&Id> {
        self.search(self.root(), key, self.get_height())
    }
    fn search<'a>(&self, node: &'a Node, key: &EntryKey, ht: u32) -> Option<&'a Id> {
        let children = &node.entries();
        match node.entries().binary_search_by(|e| e.key.cmp(key)) {
            Ok(pos) => return Some(&node.entries()[pos].val),
            Err(pos) => {
                if ht == 0 {
                    return None
                }
                else {
                    match &node.delimiters().get(pos) {
                        Some(Delimiter::External(id)) => unimplemented!(), // TODO: read page from remote
                        Some(Delimiter::Internal(node)) => return self.search(node.borrow(), key, ht - 1),
                        None => return None
                    }
                }
            }
        }
    }
}

struct Entry {
    key: EntryKey,
    val: Id
}

enum Delimiter {
    External(Id),
    Internal(Box<Node>)
}

trait Array<T> {
    #[inline]
    fn index_of(&self, index: usize) -> &T;
    #[inline]
    fn size(&self) -> usize;
}

trait Entries : Array<Entry> {}

trait Delimiters : Array<Delimiter> {}

trait Node {
    fn id(&self) -> &Id;
    fn entries(&self) -> &[Entry];
    fn delimiters(&self) -> &[Delimiter];
    fn entries_mut(&mut self) -> &mut [Entry];
    fn delimiters_mut(&mut self) -> &mut [Delimiter];
}

macro_rules! impl_nodes {
    ($(($level: ident, $entry_size: expr, $delimiter_size: expr)),+) => {
        $(
            mod $level {
                use super::*;

                type LEntries = [Entry; $entry_size];

                impl Array<Entry> for LEntries {
                    #[inline]
                    fn size(&self) -> usize { $entry_size }
                    #[inline]
                    fn index_of(&self, index: usize) -> &Entry { &self[index] }
                }

                type LDelimiter = [Delimiter; $delimiter_size];

                impl Array<Delimiter> for LDelimiter {
                    #[inline]
                    fn size(&self) -> usize { $delimiter_size }
                    #[inline]
                    fn index_of(&self, index: usize) -> &Delimiter { &self[index] }
                }

                impl Delimiters for LDelimiter {}

                struct LNode {
                    id: Id,
                    entries: LEntries,
                    delimeters: LDelimiter,
                }

                impl Node for LNode {
                    #[inline]
                    fn id(&self) -> &Id {
                        &self.id
                    }
                    #[inline]
                    fn entries(&self) -> &[Entry] {
                        &self.entries
                    }
                    #[inline]
                    fn delimiters(&self) -> &[Delimiter] {
                        &self.delimeters
                    }
                    #[inline]
                    fn entries_mut(&mut self) -> &mut [Entry] {
                        &mut self.entries
                    }
                    #[inline]
                    fn delimiters_mut(&mut self) -> &mut [Delimiter] {
                        &mut self.delimeters
                    }
                }
            }
        )+
    };
}

impl_nodes!((level_0, 2, 3), (level_1, 20, 21), (level_2, 200, 201), (level_3, 2000, 2001), (level_4, 20000, 20001));

//impl BTree {
//    pub fn new(max_entries: u64) -> BTree {
//        BTree {
//            node_size: 0,
//            height: 0,
//            max_entries,
//            root: Node::new(node_size)
//        }
//    }
//}
//
//impl Node {
//    pub fn new(node_size: u16) -> Rc<Node> {
//        Rc::new(Node {
//            id: Id::unit_id(),
//            children: [u8; 16]
//        })
//    }
//}<|endoftext|>


<commit_before>use azure_sdk_core::errors::{check_status_extract_body, AzureError};
use azure_sdk_storage_core::client::Client;
use azure_sdk_storage_core::{
    get_default_json_mime, get_json_mime_fullmetadata, get_json_mime_nometadata, ServiceType,
};
use hyper::{
    client::ResponseFuture,
    header::{self, HeaderValue},
};
use hyper::{Method, StatusCode};
use log;
use serde_json;

const TABLE_TABLES: &str = "TABLES";

/// Requetsed meta data detail
pub enum MetadataDetail {
    Default,
    None,
    Full,
}

#[derive(Clone)]
pub struct TableClient {
    client: Client,
}

impl TableClient {
    pub fn new(account: &str, key: &str) -> Result<Self, AzureError> {
        Ok(TableClient {
            client: Client::new(account, key)?,
        })
    }

    pub async fn list_tables(&self) -> Result<Vec<String>, AzureError> {
        let future_response = self.request_with_default_header(
            TABLE_TABLES,
            &Method::GET,
            None,
            MetadataDetail::None,
            |req| req,
        )?;
        let body = check_status_extract_body(future_response, StatusCode::OK).await?;
        let entities = serde_json::from_str::<TableDataCollection>(&body)?;
        // todo: shall we use the continuation or query result always fits into a single page
        let e: Vec<String> = entities.value.into_iter().map(|x| x.table_name).collect();
        Ok(e)
    }

    // Create table if not exists.
    pub async fn create_table<T: Into<String>>(&self, table_name: T) -> Result<(), AzureError> {
        let body = &serde_json::to_string(&TableData {
            table_name: table_name.into(),
        })
        .unwrap();
        log::debug!("body == {}", body);
        let future_response = self.request_with_default_header(
            TABLE_TABLES,
            &Method::POST,
            Some(body),
            MetadataDetail::None,
            |req| req,
        )?;

        check_status_extract_body(future_response, StatusCode::CREATED).await?;
        Ok(())
    }

    pub fn get_uri_prefix(&self) -> String {
        self.client.get_uri_prefix(ServiceType::Table)
    }

    pub(crate) fn request_with_default_header<F>(
        &self,
        segment: &str,
        method: &Method,
        request_str: Option<&str>,
        metadata: MetadataDetail,
        request_extra: F,
    ) -> Result<ResponseFuture, AzureError>
    where
        F: FnOnce(::http::request::Builder) -> ::http::request::Builder,
    {
        self.request(segment, method, request_str, |mut request| {
            request = match metadata {
                MetadataDetail::Full => request.header(
                    header::ACCEPT,
                    HeaderValue::from_static(get_json_mime_fullmetadata()),
                ),
                MetadataDetail::None => request.header(
                    header::ACCEPT,
                    HeaderValue::from_static(get_json_mime_nometadata()),
                ),
                MetadataDetail::Default => request.header(
                    header::ACCEPT,
                    HeaderValue::from_static(get_default_json_mime()),
                ),
            };
            if request_str.is_some() {
                request = request.header(
                    header::CONTENT_TYPE,
                    HeaderValue::from_static(get_default_json_mime()),
                );
            }

            request_extra(request)
        })
    }

    pub(crate) fn request<F>(
        &self,
        segment: &str,
        method: &Method,
        request_str: Option<&str>,
        request_extra: F,
    ) -> Result<ResponseFuture, AzureError>
    where
        F: FnOnce(::http::request::Builder) -> ::http::request::Builder,
    {
        log::trace!("{:?} {}", method, segment);
        if let Some(body) = request_str {
            log::trace!("Request: {}", body);
        }

        let request_vec: Option<&[u8]> = match request_str {
            Some(s) => Some(s.as_bytes()),
            None => None,
        };

        self.client
            .perform_table_request(segment, method, request_extra, request_vec)
    }
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct TableData {
    table_name: String,
}

#[derive(Serialize, Deserialize)]
struct TableDataCollection {
    value: Vec<TableData>,
}

#[inline]
pub(crate) fn get_batch_mime() -> &'static str {
    "multipart/mixed; boundary=batch_a1e9d677-b28b-435e-a89e-87e6a768a431"
}

pub(crate) fn entity_path(table_name: &str, partition_key: &str, row_key: &str) -> String {
    table_name.to_owned() + "(PartitionKey='" + partition_key + "',RowKey='" + row_key + "')"
}
<commit_msg>Add azure_sas associated function to TableClient<commit_after>use azure_sdk_core::errors::{check_status_extract_body, AzureError};
use azure_sdk_storage_core::client::Client;
use azure_sdk_storage_core::{
    get_default_json_mime, get_json_mime_fullmetadata, get_json_mime_nometadata, ServiceType,
};
use hyper::{
    client::ResponseFuture,
    header::{self, HeaderValue},
};
use hyper::{Method, StatusCode};
use log;
use serde_json;

const TABLE_TABLES: &str = "TABLES";

/// Requetsed meta data detail
pub enum MetadataDetail {
    Default,
    None,
    Full,
}

#[derive(Clone)]
pub struct TableClient {
    client: Client,
}

impl TableClient {
    /// Create a new `TableClient` using a key.
    pub fn new(account: &str, key: &str) -> Result<Self, AzureError> {
        Ok(TableClient {
            client: Client::new(account, key)?,
        })
    }

    /// Create a new `TableClient` using a SAS token.
    pub fn azure_sas(account: &str, sas_token: &str) -> Result<Self, AzureError> {
        Ok(TableClient {
            client: Client::azure_sas(account, sas_token)?,
        })
    }

    pub async fn list_tables(&self) -> Result<Vec<String>, AzureError> {
        let future_response = self.request_with_default_header(
            TABLE_TABLES,
            &Method::GET,
            None,
            MetadataDetail::None,
            |req| req,
        )?;
        let body = check_status_extract_body(future_response, StatusCode::OK).await?;
        let entities = serde_json::from_str::<TableDataCollection>(&body)?;
        // todo: shall we use the continuation or query result always fits into a single page
        let e: Vec<String> = entities.value.into_iter().map(|x| x.table_name).collect();
        Ok(e)
    }

    // Create table if not exists.
    pub async fn create_table<T: Into<String>>(&self, table_name: T) -> Result<(), AzureError> {
        let body = &serde_json::to_string(&TableData {
            table_name: table_name.into(),
        })
        .unwrap();
        log::debug!("body == {}", body);
        let future_response = self.request_with_default_header(
            TABLE_TABLES,
            &Method::POST,
            Some(body),
            MetadataDetail::None,
            |req| req,
        )?;

        check_status_extract_body(future_response, StatusCode::CREATED).await?;
        Ok(())
    }

    pub fn get_uri_prefix(&self) -> String {
        self.client.get_uri_prefix(ServiceType::Table)
    }

    pub(crate) fn request_with_default_header<F>(
        &self,
        segment: &str,
        method: &Method,
        request_str: Option<&str>,
        metadata: MetadataDetail,
        request_extra: F,
    ) -> Result<ResponseFuture, AzureError>
    where
        F: FnOnce(::http::request::Builder) -> ::http::request::Builder,
    {
        self.request(segment, method, request_str, |mut request| {
            request = match metadata {
                MetadataDetail::Full => request.header(
                    header::ACCEPT,
                    HeaderValue::from_static(get_json_mime_fullmetadata()),
                ),
                MetadataDetail::None => request.header(
                    header::ACCEPT,
                    HeaderValue::from_static(get_json_mime_nometadata()),
                ),
                MetadataDetail::Default => request.header(
                    header::ACCEPT,
                    HeaderValue::from_static(get_default_json_mime()),
                ),
            };
            if request_str.is_some() {
                request = request.header(
                    header::CONTENT_TYPE,
                    HeaderValue::from_static(get_default_json_mime()),
                );
            }

            request_extra(request)
        })
    }

    pub(crate) fn request<F>(
        &self,
        segment: &str,
        method: &Method,
        request_str: Option<&str>,
        request_extra: F,
    ) -> Result<ResponseFuture, AzureError>
    where
        F: FnOnce(::http::request::Builder) -> ::http::request::Builder,
    {
        log::trace!("{:?} {}", method, segment);
        if let Some(body) = request_str {
            log::trace!("Request: {}", body);
        }

        let request_vec: Option<&[u8]> = match request_str {
            Some(s) => Some(s.as_bytes()),
            None => None,
        };

        self.client
            .perform_table_request(segment, method, request_extra, request_vec)
    }
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct TableData {
    table_name: String,
}

#[derive(Serialize, Deserialize)]
struct TableDataCollection {
    value: Vec<TableData>,
}

#[inline]
pub(crate) fn get_batch_mime() -> &'static str {
    "multipart/mixed; boundary=batch_a1e9d677-b28b-435e-a89e-87e6a768a431"
}

pub(crate) fn entity_path(table_name: &str, partition_key: &str, row_key: &str) -> String {
    table_name.to_owned() + "(PartitionKey='" + partition_key + "',RowKey='" + row_key + "')"
}
<|endoftext|>


<commit_before>use codespan::{ByteIndex, Files, LineIndex, Location, Span};
use std::io;
use termcolor::{Color, ColorSpec, WriteColor};

use crate::emitter::Config;
use crate::{Diagnostic, Label, Severity};

use super::{Gutter, Locus, NewLine, Note};

enum MarkStyle {
    Primary(Severity),
    Secondary,
}

/// A marked section of source code.
///
/// ```text
///   ┌── test:2:9 ───
///   │
/// 2 │ (+ test "")
///   │         ^^ expected `Int` but found `String`
///   │
///   = expected type `Int`
///        found type `String`
/// ```
pub struct SourceSnippet<'a> {
    files: &'a Files,
    label: &'a Label,
    mark_style: MarkStyle,
    notes: &'a [String],
}

impl<'a> SourceSnippet<'a> {
    pub fn new_primary(files: &'a Files, diagnostic: &'a Diagnostic) -> SourceSnippet<'a> {
        SourceSnippet {
            files,
            label: &diagnostic.primary_label,
            mark_style: MarkStyle::Primary(diagnostic.severity),
            notes: &diagnostic.notes,
        }
    }

    pub fn new_secondary(files: &'a Files, label: &'a Label) -> SourceSnippet<'a> {
        SourceSnippet {
            files,
            label,
            mark_style: MarkStyle::Secondary,
            notes: &[],
        }
    }

    fn span(&self) -> Span {
        self.label.span
    }

    fn start(&self) -> ByteIndex {
        self.span().start()
    }

    fn end(&self) -> ByteIndex {
        self.span().end()
    }

    fn file_name(&self) -> &'a str {
        self.files.name(self.label.file_id)
    }

    fn location(&self, byte_index: ByteIndex) -> Result<Location, impl std::error::Error> {
        self.files.location(self.label.file_id, byte_index)
    }

    fn source_slice(&self, span: Span, tab: &'a str) -> Result<String, impl std::error::Error> {
        // NOTE: Not sure if we can do this more efficiently? Perhaps a custom
        // writer might be better?
        self.files
            .source_slice(self.label.file_id, span)
            .map(|s| s.replace('\t', tab))
    }

    fn line_span(&self, line_index: LineIndex) -> Result<Span, impl std::error::Error> {
        self.files.line_span(self.label.file_id, line_index)
    }

    fn label_color(&self, config: &Config) -> Color {
        match self.mark_style {
            MarkStyle::Primary(severity) => config.severity_color(severity),
            MarkStyle::Secondary => config.secondary_color,
        }
    }

    fn underline_char(&self, config: &Config) -> char {
        match self.mark_style {
            MarkStyle::Primary(_) => config.primary_underline_char,
            MarkStyle::Secondary => config.secondary_underline_char,
        }
    }

    pub fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
        let label_spec = ColorSpec::new()
            .set_fg(Some(self.label_color(config)))
            .clone();

        let start = self.location(self.start()).expect("location_start");
        let end = self.location(self.end()).expect("location_end");
        let start_line_span = self.line_span(start.line).expect("start_line_span");
        let end_line_span = self.line_span(end.line).expect("end_line_span");

        // Use the length of the last line number as the gutter padding
        let gutter_padding = format!("{}", end.line.number()).len();
        // Cache the tabs we'll be using to pad the source strings.
        let tab = config.tab_padding();

        // Top left border and locus.
        //
        // ```text
        // ┌── test:2:9 ───
        // ```

        Gutter::new(None, gutter_padding).emit(writer, config)?;
        BorderTopLeft::new().emit(writer, config)?;
        BorderTop::new(2).emit(writer, config)?;
        write!(writer, " ")?;

        Locus::new(self.file_name(), start).emit(writer, config)?;

        write!(writer, " ")?;
        BorderTop::new(3).emit(writer, config)?;
        NewLine::new().emit(writer, config)?;

        // SourceSnippet code snippet
        //
        // ```text
        //   │
        // 2 │ (+ test "")
        //   │         ^^ expected `Int` but found `String`
        //   │
        // ```

        // Write initial border
        Gutter::new(None, gutter_padding).emit(writer, config)?;
        BorderLeft::new().emit(writer, config)?;
        NewLine::new().emit(writer, config)?;

        // Write line number and border
        Gutter::new(start.line.number(), gutter_padding).emit(writer, config)?;
        BorderLeft::new().emit(writer, config)?;

        let line_trimmer = |ch: char| ch == '\r' || ch == '\n';

        // Write source prefix before marked section
        let prefix_span = start_line_span.with_end(self.start());
        let source_prefix = self.source_slice(prefix_span, &tab).expect("source_prefix");
        write!(writer, "{}", source_prefix)?;

        // Write marked section
        let mark_len = if start.line == end.line {
            // Single line

            // Write marked source section
            let marked_source = self.source_slice(self.span(), &tab).expect("marked_source");
            writer.set_color(&label_spec)?;
            write!(writer, "{}", marked_source)?;
            writer.reset()?;
            config.width(&marked_source)
        } else {
            // Multiple lines

            // Write marked source section
            let marked_span = start_line_span.with_start(self.start());
            let marked_source = self
                .source_slice(marked_span, &tab)
                .expect("marked_source_1");
            writer.set_color(&label_spec)?;
            write!(writer, "{}", marked_source)?;

            for line_index in ((start.line.to_usize() + 1)..end.line.to_usize())
                .map(|i| LineIndex::from(i as u32))
            {
                // Write line number and border
                Gutter::new(line_index.number(), gutter_padding).emit(writer, config)?;
                BorderLeft::new().emit(writer, config)?;

                // Write marked source section
                let marked_span = self.line_span(line_index).expect("marked_span");
                let marked_source = self
                    .source_slice(marked_span, &tab)
                    .expect("marked_source_2");
                writer.set_color(&label_spec)?;
                write!(writer, "{}", marked_source.trim_end_matches(line_trimmer))?;
                NewLine::new().emit(writer, config)?;
            }

            // Write line number and border
            Gutter::new(end.line.number(), gutter_padding).emit(writer, config)?;
            BorderLeft::new().emit(writer, config)?;

            // Write marked source section
            let marked_span = end_line_span.with_end(self.end());
            let marked_source = self
                .source_slice(marked_span, &tab)
                .expect("marked_source_3");
            writer.set_color(&label_spec)?;
            write!(writer, "{}", marked_source)?;
            writer.reset()?;
            config.width(&marked_source)
        };

        // Write source suffix after marked section
        let suffix_span = end_line_span.with_start(self.end());
        let source_suffix = self.source_slice(suffix_span, &tab).expect("source_suffix");
        write!(writer, "{}", source_suffix.trim_end_matches(line_trimmer))?;
        NewLine::new().emit(writer, config)?;

        // Write underline border
        Gutter::new(None, gutter_padding).emit(writer, config)?;
        BorderLeft::new().emit(writer, config)?;

        // Write underline and label
        writer.set_color(&label_spec)?;
        write!(
            writer,
            "{space: >width$}",
            space = "",
            width = config.width(&source_prefix),
        )?;
        // We use `std::cmp::max` here to ensure that we print at least one
        // underline character - even when we have a zero-length span.
        for _ in 0..std::cmp::max(mark_len, 1) {
            write!(writer, "{}", self.underline_char(config))?;
        }
        if !self.label.message.is_empty() {
            write!(writer, " {}", self.label.message)?;
        }
        NewLine::new().emit(writer, config)?;
        writer.reset()?;

        // Write final border
        Gutter::new(None, gutter_padding).emit(writer, config)?;
        BorderLeft::new().emit(writer, config)?;
        NewLine::new().emit(writer, config)?;

        // Additional notes
        //
        // ```text
        // = expected type `Int`
        //      found type `String`
        // ```

        for note in self.notes {
            Note::new(gutter_padding, note).emit(writer, config)?;
        }

        Ok(())
    }
}

/// The top-left corner of a source line.
struct BorderTopLeft {}

impl BorderTopLeft {
    fn new() -> BorderTopLeft {
        BorderTopLeft {}
    }

    fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
        let border_spec = ColorSpec::new().set_fg(Some(config.border_color)).clone();

        writer.set_color(&border_spec)?;
        write!(writer, "{top_left}", top_left = config.border_top_left_char)?;
        writer.reset()?;

        Ok(())
    }
}

/// The top border of a source line.
struct BorderTop {
    width: usize,
}

impl BorderTop {
    fn new(width: usize) -> BorderTop {
        BorderTop { width }
    }

    fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
        let border_spec = ColorSpec::new().set_fg(Some(config.border_color)).clone();

        writer.set_color(&border_spec)?;
        for _ in 0..self.width {
            write!(writer, "{top}", top = config.border_top_char)?
        }
        writer.reset()?;

        Ok(())
    }
}

/// The left-hand border of a source line.
struct BorderLeft {}

impl BorderLeft {
    fn new() -> BorderLeft {
        BorderLeft {}
    }

    fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
        let border_spec = ColorSpec::new().set_fg(Some(config.border_color)).clone();

        writer.set_color(&border_spec)?;
        write!(writer, "{left} ", left = config.border_left_char)?;
        writer.reset()?;

        Ok(())
    }
}
<commit_msg>Use Ord::max<commit_after>use codespan::{ByteIndex, Files, LineIndex, Location, Span};
use std::io;
use termcolor::{Color, ColorSpec, WriteColor};

use crate::emitter::Config;
use crate::{Diagnostic, Label, Severity};

use super::{Gutter, Locus, NewLine, Note};

enum MarkStyle {
    Primary(Severity),
    Secondary,
}

/// A marked section of source code.
///
/// ```text
///   ┌── test:2:9 ───
///   │
/// 2 │ (+ test "")
///   │         ^^ expected `Int` but found `String`
///   │
///   = expected type `Int`
///        found type `String`
/// ```
pub struct SourceSnippet<'a> {
    files: &'a Files,
    label: &'a Label,
    mark_style: MarkStyle,
    notes: &'a [String],
}

impl<'a> SourceSnippet<'a> {
    pub fn new_primary(files: &'a Files, diagnostic: &'a Diagnostic) -> SourceSnippet<'a> {
        SourceSnippet {
            files,
            label: &diagnostic.primary_label,
            mark_style: MarkStyle::Primary(diagnostic.severity),
            notes: &diagnostic.notes,
        }
    }

    pub fn new_secondary(files: &'a Files, label: &'a Label) -> SourceSnippet<'a> {
        SourceSnippet {
            files,
            label,
            mark_style: MarkStyle::Secondary,
            notes: &[],
        }
    }

    fn span(&self) -> Span {
        self.label.span
    }

    fn start(&self) -> ByteIndex {
        self.span().start()
    }

    fn end(&self) -> ByteIndex {
        self.span().end()
    }

    fn file_name(&self) -> &'a str {
        self.files.name(self.label.file_id)
    }

    fn location(&self, byte_index: ByteIndex) -> Result<Location, impl std::error::Error> {
        self.files.location(self.label.file_id, byte_index)
    }

    fn source_slice(&self, span: Span, tab: &'a str) -> Result<String, impl std::error::Error> {
        // NOTE: Not sure if we can do this more efficiently? Perhaps a custom
        // writer might be better?
        self.files
            .source_slice(self.label.file_id, span)
            .map(|s| s.replace('\t', tab))
    }

    fn line_span(&self, line_index: LineIndex) -> Result<Span, impl std::error::Error> {
        self.files.line_span(self.label.file_id, line_index)
    }

    fn label_color(&self, config: &Config) -> Color {
        match self.mark_style {
            MarkStyle::Primary(severity) => config.severity_color(severity),
            MarkStyle::Secondary => config.secondary_color,
        }
    }

    fn underline_char(&self, config: &Config) -> char {
        match self.mark_style {
            MarkStyle::Primary(_) => config.primary_underline_char,
            MarkStyle::Secondary => config.secondary_underline_char,
        }
    }

    pub fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
        let label_spec = ColorSpec::new()
            .set_fg(Some(self.label_color(config)))
            .clone();

        let start = self.location(self.start()).expect("location_start");
        let end = self.location(self.end()).expect("location_end");
        let start_line_span = self.line_span(start.line).expect("start_line_span");
        let end_line_span = self.line_span(end.line).expect("end_line_span");

        // Use the length of the last line number as the gutter padding
        let gutter_padding = format!("{}", end.line.number()).len();
        // Cache the tabs we'll be using to pad the source strings.
        let tab = config.tab_padding();

        // Top left border and locus.
        //
        // ```text
        // ┌── test:2:9 ───
        // ```

        Gutter::new(None, gutter_padding).emit(writer, config)?;
        BorderTopLeft::new().emit(writer, config)?;
        BorderTop::new(2).emit(writer, config)?;
        write!(writer, " ")?;

        Locus::new(self.file_name(), start).emit(writer, config)?;

        write!(writer, " ")?;
        BorderTop::new(3).emit(writer, config)?;
        NewLine::new().emit(writer, config)?;

        // SourceSnippet code snippet
        //
        // ```text
        //   │
        // 2 │ (+ test "")
        //   │         ^^ expected `Int` but found `String`
        //   │
        // ```

        // Write initial border
        Gutter::new(None, gutter_padding).emit(writer, config)?;
        BorderLeft::new().emit(writer, config)?;
        NewLine::new().emit(writer, config)?;

        // Write line number and border
        Gutter::new(start.line.number(), gutter_padding).emit(writer, config)?;
        BorderLeft::new().emit(writer, config)?;

        let line_trimmer = |ch: char| ch == '\r' || ch == '\n';

        // Write source prefix before marked section
        let prefix_span = start_line_span.with_end(self.start());
        let source_prefix = self.source_slice(prefix_span, &tab).expect("source_prefix");
        write!(writer, "{}", source_prefix)?;

        // Write marked section
        let mark_len = if start.line == end.line {
            // Single line

            // Write marked source section
            let marked_source = self.source_slice(self.span(), &tab).expect("marked_source");
            writer.set_color(&label_spec)?;
            write!(writer, "{}", marked_source)?;
            writer.reset()?;
            config.width(&marked_source)
        } else {
            // Multiple lines

            // Write marked source section
            let marked_span = start_line_span.with_start(self.start());
            let marked_source = self
                .source_slice(marked_span, &tab)
                .expect("marked_source_1");
            writer.set_color(&label_spec)?;
            write!(writer, "{}", marked_source)?;

            for line_index in ((start.line.to_usize() + 1)..end.line.to_usize())
                .map(|i| LineIndex::from(i as u32))
            {
                // Write line number and border
                Gutter::new(line_index.number(), gutter_padding).emit(writer, config)?;
                BorderLeft::new().emit(writer, config)?;

                // Write marked source section
                let marked_span = self.line_span(line_index).expect("marked_span");
                let marked_source = self
                    .source_slice(marked_span, &tab)
                    .expect("marked_source_2");
                writer.set_color(&label_spec)?;
                write!(writer, "{}", marked_source.trim_end_matches(line_trimmer))?;
                NewLine::new().emit(writer, config)?;
            }

            // Write line number and border
            Gutter::new(end.line.number(), gutter_padding).emit(writer, config)?;
            BorderLeft::new().emit(writer, config)?;

            // Write marked source section
            let marked_span = end_line_span.with_end(self.end());
            let marked_source = self
                .source_slice(marked_span, &tab)
                .expect("marked_source_3");
            writer.set_color(&label_spec)?;
            write!(writer, "{}", marked_source)?;
            writer.reset()?;
            config.width(&marked_source)
        };

        // Write source suffix after marked section
        let suffix_span = end_line_span.with_start(self.end());
        let source_suffix = self.source_slice(suffix_span, &tab).expect("source_suffix");
        write!(writer, "{}", source_suffix.trim_end_matches(line_trimmer))?;
        NewLine::new().emit(writer, config)?;

        // Write underline border
        Gutter::new(None, gutter_padding).emit(writer, config)?;
        BorderLeft::new().emit(writer, config)?;

        // Write underline and label
        writer.set_color(&label_spec)?;
        write!(
            writer,
            "{space: >width$}",
            space = "",
            width = config.width(&source_prefix),
        )?;
        // We use `usize::max` here to ensure that we print at least one
        // underline character - even when we have a zero-length span.
        for _ in 0..usize::max(mark_len, 1) {
            write!(writer, "{}", self.underline_char(config))?;
        }
        if !self.label.message.is_empty() {
            write!(writer, " {}", self.label.message)?;
        }
        NewLine::new().emit(writer, config)?;
        writer.reset()?;

        // Write final border
        Gutter::new(None, gutter_padding).emit(writer, config)?;
        BorderLeft::new().emit(writer, config)?;
        NewLine::new().emit(writer, config)?;

        // Additional notes
        //
        // ```text
        // = expected type `Int`
        //      found type `String`
        // ```

        for note in self.notes {
            Note::new(gutter_padding, note).emit(writer, config)?;
        }

        Ok(())
    }
}

/// The top-left corner of a source line.
struct BorderTopLeft {}

impl BorderTopLeft {
    fn new() -> BorderTopLeft {
        BorderTopLeft {}
    }

    fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
        let border_spec = ColorSpec::new().set_fg(Some(config.border_color)).clone();

        writer.set_color(&border_spec)?;
        write!(writer, "{top_left}", top_left = config.border_top_left_char)?;
        writer.reset()?;

        Ok(())
    }
}

/// The top border of a source line.
struct BorderTop {
    width: usize,
}

impl BorderTop {
    fn new(width: usize) -> BorderTop {
        BorderTop { width }
    }

    fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
        let border_spec = ColorSpec::new().set_fg(Some(config.border_color)).clone();

        writer.set_color(&border_spec)?;
        for _ in 0..self.width {
            write!(writer, "{top}", top = config.border_top_char)?
        }
        writer.reset()?;

        Ok(())
    }
}

/// The left-hand border of a source line.
struct BorderLeft {}

impl BorderLeft {
    fn new() -> BorderLeft {
        BorderLeft {}
    }

    fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
        let border_spec = ColorSpec::new().set_fg(Some(config.border_color)).clone();

        writer.set_color(&border_spec)?;
        write!(writer, "{left} ", left = config.border_left_char)?;
        writer.reset()?;

        Ok(())
    }
}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2014, David Renshaw (dwrenshaw@gmail.com)
 *
 * See the LICENSE file in the capnproto-rust root directory.
 */

use any_pointer;
use common::{MessageSize};
use traits::{FromPointerReader, FromPointerBuilder};
use message::{MallocMessageBuilder};
use std;
use std::vec::Vec;

pub struct ResultFuture<Results, Pipeline> {
    pub answer_port : std::sync::mpsc::Receiver<Box<ResponseHook+Send>>,
    pub answer_result : Result<Box<ResponseHook+Send>, ()>,
    pub pipeline : Pipeline,
}

pub trait ResponseHook:Send {
    fn get<'a>(&'a mut self) -> any_pointer::Reader<'a>;
}

pub trait RequestHook {
    fn message<'a>(&'a mut self) -> &'a mut MallocMessageBuilder;
    fn send<'a>(self : Box<Self>) -> ResultFuture<any_pointer::Reader<'a>, any_pointer::Pipeline>;
}

pub struct Request<Params, Results, Pipeline> {
    pub hook : Box<RequestHook+Send>
}

impl <Params, Results, Pipeline > Request <Params, Results, Pipeline> {
    pub fn new(hook : Box<RequestHook+Send>) -> Request <Params, Results, Pipeline> {
        Request { hook : hook }
    }
}
impl <Params, Results, Pipeline : FromTypelessPipeline> Request <Params, Results, Pipeline> {
    pub fn send(self) -> ResultFuture<Results, Pipeline> {
        let ResultFuture {answer_port, answer_result, pipeline} = self.hook.send();
        ResultFuture { answer_port : answer_port, answer_result : answer_result,
                        pipeline : FromTypelessPipeline::new(pipeline) }
    }
}

pub trait FromClientHook {
    fn new(Box<ClientHook+Send>) -> Self;
}

pub trait ClientHook : Send {
    fn copy(&self) -> Box<ClientHook+Send>;
    fn new_call(&self,
                interface_id : u64,
                method_id : u16,
                size_hint : Option<MessageSize>)
                -> Request<any_pointer::Builder, any_pointer::Reader, any_pointer::Pipeline>;
    fn call(&self, interface_id : u64, method_id : u16, context : Box<CallContextHook+Send>);

    // HACK
    fn get_descriptor(&self) -> Box<std::any::Any + 'static>;
}

pub trait ServerHook {
    fn new_client(unused : Option<Self>, server : Box<Server+Send>) -> Client;
}

pub trait FromServer<T, U> {
    fn new(hook : Option<T>, server : Box<U>) -> Self;
}

pub struct Client {
    pub hook : Box<ClientHook+Send>
}

impl Client {
    pub fn new(hook : Box<ClientHook+Send>) -> Client {
        Client { hook : hook }
    }

    pub fn new_call<Params, Results, Pipeline>(&self,
                                               interface_id : u64,
                                               method_id : u16,
                                               size_hint : Option<MessageSize>)
                                               -> Request<Params, Results, Pipeline> {
        let typeless = self.hook.new_call(interface_id, method_id, size_hint);
        Request { hook : typeless.hook }
    }
}

pub struct CallContext<Params, Results> {
    pub hook : Box<CallContextHook+Send>,
}

impl <Params, Results> CallContext<Params, Results> {
    pub fn fail(self) {self.hook.fail();}
    pub fn done(self) {self.hook.done();}
}

impl <'a, Params : FromPointerReader<'a>, Results : FromPointerBuilder<'a>>
CallContext<Params, Results> {
    // XXX this 'b lifetime should be 'a.
    pub fn get<'b>(&'b mut self) -> (Params, Results) {
        let tmp : &'a mut Box<CallContextHook+Send> = unsafe { ::std::mem::transmute(& mut self.hook)};
        let (any_params, any_results) = tmp.get();
        (any_params.get_as(), any_results.get_as())
    }
}

pub trait CallContextHook {
    fn get<'a>(&'a mut self) -> (any_pointer::Reader<'a>, any_pointer::Builder<'a>);
    fn fail(self : Box<Self>);
    fn done(self : Box<Self>);
}

pub trait Server {
    fn dispatch_call(&mut self, interface_id : u64, method_id : u16,
                     context : CallContext<any_pointer::Reader, any_pointer::Builder>);

}

// Where should this live?
pub fn internal_get_typed_context<Params, Results>(
    typeless : CallContext<any_pointer::Reader, any_pointer::Builder>)
    -> CallContext<Params, Results> {
    CallContext { hook : typeless.hook }
}

pub trait PipelineHook {
    fn copy(&self) -> Box<PipelineHook+Send>;
    fn get_pipelined_cap(&self, ops : Vec<PipelineOp>) -> Box<ClientHook+Send>;
}

#[derive(Clone, Copy)]
pub enum PipelineOp {
    Noop,
    GetPointerField(u16),
}

pub trait FromTypelessPipeline {
    fn new (typeless : any_pointer::Pipeline) -> Self;
}
<commit_msg>tweaking FromServer so we can drop old_orphan_check<commit_after>/*
 * Copyright (c) 2014, David Renshaw (dwrenshaw@gmail.com)
 *
 * See the LICENSE file in the capnproto-rust root directory.
 */

use any_pointer;
use common::{MessageSize};
use traits::{FromPointerReader, FromPointerBuilder};
use message::{MallocMessageBuilder};
use std;
use std::vec::Vec;

pub struct ResultFuture<Results, Pipeline> {
    pub answer_port : std::sync::mpsc::Receiver<Box<ResponseHook+Send>>,
    pub answer_result : Result<Box<ResponseHook+Send>, ()>,
    pub pipeline : Pipeline,
}

pub trait ResponseHook:Send {
    fn get<'a>(&'a mut self) -> any_pointer::Reader<'a>;
}

pub trait RequestHook {
    fn message<'a>(&'a mut self) -> &'a mut MallocMessageBuilder;
    fn send<'a>(self : Box<Self>) -> ResultFuture<any_pointer::Reader<'a>, any_pointer::Pipeline>;
}

pub struct Request<Params, Results, Pipeline> {
    pub hook : Box<RequestHook+Send>
}

impl <Params, Results, Pipeline > Request <Params, Results, Pipeline> {
    pub fn new(hook : Box<RequestHook+Send>) -> Request <Params, Results, Pipeline> {
        Request { hook : hook }
    }
}
impl <Params, Results, Pipeline : FromTypelessPipeline> Request <Params, Results, Pipeline> {
    pub fn send(self) -> ResultFuture<Results, Pipeline> {
        let ResultFuture {answer_port, answer_result, pipeline} = self.hook.send();
        ResultFuture { answer_port : answer_port, answer_result : answer_result,
                        pipeline : FromTypelessPipeline::new(pipeline) }
    }
}

pub trait FromClientHook {
    fn new(Box<ClientHook+Send>) -> Self;
}

pub trait ClientHook : Send {
    fn copy(&self) -> Box<ClientHook+Send>;
    fn new_call(&self,
                interface_id : u64,
                method_id : u16,
                size_hint : Option<MessageSize>)
                -> Request<any_pointer::Builder, any_pointer::Reader, any_pointer::Pipeline>;
    fn call(&self, interface_id : u64, method_id : u16, context : Box<CallContextHook+Send>);

    // HACK
    fn get_descriptor(&self) -> Box<std::any::Any + 'static>;
}

pub trait ServerHook {
    fn new_client(unused : Option<Self>, server : Box<Server+Send>) -> Client;
}

pub trait FromServer<T, U> {
    fn from_server(self, hook : Option<T>) -> U;
}

pub struct Client {
    pub hook : Box<ClientHook+Send>
}

impl Client {
    pub fn new(hook : Box<ClientHook+Send>) -> Client {
        Client { hook : hook }
    }

    pub fn new_call<Params, Results, Pipeline>(&self,
                                               interface_id : u64,
                                               method_id : u16,
                                               size_hint : Option<MessageSize>)
                                               -> Request<Params, Results, Pipeline> {
        let typeless = self.hook.new_call(interface_id, method_id, size_hint);
        Request { hook : typeless.hook }
    }
}

pub struct CallContext<Params, Results> {
    pub hook : Box<CallContextHook+Send>,
}

impl <Params, Results> CallContext<Params, Results> {
    pub fn fail(self) {self.hook.fail();}
    pub fn done(self) {self.hook.done();}
}

impl <'a, Params : FromPointerReader<'a>, Results : FromPointerBuilder<'a>>
CallContext<Params, Results> {
    // XXX this 'b lifetime should be 'a.
    pub fn get<'b>(&'b mut self) -> (Params, Results) {
        let tmp : &'a mut Box<CallContextHook+Send> = unsafe { ::std::mem::transmute(& mut self.hook)};
        let (any_params, any_results) = tmp.get();
        (any_params.get_as(), any_results.get_as())
    }
}

pub trait CallContextHook {
    fn get<'a>(&'a mut self) -> (any_pointer::Reader<'a>, any_pointer::Builder<'a>);
    fn fail(self : Box<Self>);
    fn done(self : Box<Self>);
}

pub trait Server {
    fn dispatch_call(&mut self, interface_id : u64, method_id : u16,
                     context : CallContext<any_pointer::Reader, any_pointer::Builder>);

}

// Where should this live?
pub fn internal_get_typed_context<Params, Results>(
    typeless : CallContext<any_pointer::Reader, any_pointer::Builder>)
    -> CallContext<Params, Results> {
    CallContext { hook : typeless.hook }
}

pub trait PipelineHook {
    fn copy(&self) -> Box<PipelineHook+Send>;
    fn get_pipelined_cap(&self, ops : Vec<PipelineOp>) -> Box<ClientHook+Send>;
}

#[derive(Clone, Copy)]
pub enum PipelineOp {
    Noop,
    GetPointerField(u16),
}

pub trait FromTypelessPipeline {
    fn new (typeless : any_pointer::Pipeline) -> Self;
}
<|endoftext|>


<commit_before>// =================================================================
//
//                           * WARNING *
//
//                    This file is generated!
//
//  Changes made to this file will be overwritten. If changes are
//  required to the generated code, the service_crategen project
//  must be updated to generate the changes.
//
// =================================================================

use std::error::Error;
use std::fmt;
use std::io;

#[allow(warnings)]
use futures::future;
use futures::Future;
use rusoto_core::region;
use rusoto_core::request::{BufferedHttpResponse, DispatchSignedRequest};
use rusoto_core::{Client, RusotoFuture};

use rusoto_core::credential::{CredentialsError, ProvideAwsCredentials};
use rusoto_core::request::HttpDispatchError;

use rusoto_core::signature::SignedRequest;
use serde_json;
use serde_json::from_slice;
use serde_json::Value as SerdeJsonValue;
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct InvokeEndpointInput {
    ///
The desired MIME type of the inference in the response.

    #[serde(rename = "Accept")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accept: Option<String>,
    ///
Provides input data, in the format specified in the ContentType request header. Amazon SageMaker passes all of the data in the body to the model.

    #[serde(rename = "Body")]
    #[serde(
        deserialize_with = "::rusoto_core::serialization::SerdeBlob::deserialize_blob",
        serialize_with = "::rusoto_core::serialization::SerdeBlob::serialize_blob",
        default
    )]
    pub body: Vec<u8>,
    ///
The MIME type of the input data in the request body.

    #[serde(rename = "ContentType")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_type: Option<String>,
    ///
The name of the endpoint that you specified when you created the endpoint using the CreateEndpoint API.

    #[serde(rename = "EndpointName")]
    pub endpoint_name: String,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct InvokeEndpointOutput {
    ///
Includes the inference provided by the model.

    pub body: Vec<u8>,
    ///
The MIME type of the inference returned in the response body.

    pub content_type: Option<String>,
    ///
Identifies the production variant that was invoked.

    pub invoked_production_variant: Option<String>,
}

/// Errors returned by InvokeEndpoint
#[derive(Debug, PartialEq)]
pub enum InvokeEndpointError {
    ///
 Internal failure occurred.

    InternalFailure(String),
    ///
 Model (owned by the customer in the container) returned an error 500.

    ModelError(String),
    ///
 Service is unavailable. Try your call again.

    ServiceUnavailable(String),
    ///
 Inspect your request and try again.

    ValidationError(String),
    /// An error occurred dispatching the HTTP request
    HttpDispatch(HttpDispatchError),
    /// An error was encountered with AWS credentials.
    Credentials(CredentialsError),
    /// A validation error occurred.  Details from AWS are provided.
    Validation(String),
    /// An error occurred parsing the response payload.
    ParseError(String),
    /// An unknown error occurred.  The raw HTTP response is provided.
    Unknown(BufferedHttpResponse),
}

impl InvokeEndpointError {
    // see boto RestJSONParser impl for parsing errors
    // https://github.com/boto/botocore/blob/4dff78c840403d1d17db9b3f800b20d3bd9fbf9f/botocore/parsers.py#L838-L850
    pub fn from_response(res: BufferedHttpResponse) -> InvokeEndpointError {
        if let Ok(json) = from_slice::<SerdeJsonValue>(&res.body) {
            let error_type = match res.headers.get("x-amzn-errortype") {
                Some(raw_error_type) => raw_error_type
                    .split(':')
                    .next()
                    .unwrap_or_else(|| "Unknown"),
                _ => json
                    .get("code")
                    .or_else(|| json.get("Code"))
                    .and_then(|c| c.as_str())
                    .unwrap_or_else(|| "Unknown"),
            };

            // message can come in either "message" or "Message"
            // see boto BaseJSONParser impl for parsing message
            // https://github.com/boto/botocore/blob/4dff78c840403d1d17db9b3f800b20d3bd9fbf9f/botocore/parsers.py#L595-L598
            let error_message = json
                .get("message")
                .or_else(|| json.get("Message"))
                .and_then(|m| m.as_str())
                .unwrap_or("");

            match error_type {
                "InternalFailure" => {
                    return InvokeEndpointError::InternalFailure(String::from(error_message));
                }
                "ModelError" => return InvokeEndpointError::ModelError(String::from(error_message)),
                "ServiceUnavailable" => {
                    return InvokeEndpointError::ServiceUnavailable(String::from(error_message));
                }
                "ValidationError" => {
                    return InvokeEndpointError::ValidationError(String::from(error_message));
                }
                "ValidationException" => {
                    return InvokeEndpointError::Validation(error_message.to_string());
                }
                _ => {}
            }
        }
        return InvokeEndpointError::Unknown(res);
    }
}

impl From<serde_json::error::Error> for InvokeEndpointError {
    fn from(err: serde_json::error::Error) -> InvokeEndpointError {
        InvokeEndpointError::ParseError(err.description().to_string())
    }
}
impl From<CredentialsError> for InvokeEndpointError {
    fn from(err: CredentialsError) -> InvokeEndpointError {
        InvokeEndpointError::Credentials(err)
    }
}
impl From<HttpDispatchError> for InvokeEndpointError {
    fn from(err: HttpDispatchError) -> InvokeEndpointError {
        InvokeEndpointError::HttpDispatch(err)
    }
}
impl From<io::Error> for InvokeEndpointError {
    fn from(err: io::Error) -> InvokeEndpointError {
        InvokeEndpointError::HttpDispatch(HttpDispatchError::from(err))
    }
}
impl fmt::Display for InvokeEndpointError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.description())
    }
}
impl Error for InvokeEndpointError {
    fn description(&self) -> &str {
        match *self {
            InvokeEndpointError::InternalFailure(ref cause) => cause,
            InvokeEndpointError::ModelError(ref cause) => cause,
            InvokeEndpointError::ServiceUnavailable(ref cause) => cause,
            InvokeEndpointError::ValidationError(ref cause) => cause,
            InvokeEndpointError::Validation(ref cause) => cause,
            InvokeEndpointError::Credentials(ref err) => err.description(),
            InvokeEndpointError::HttpDispatch(ref dispatch_error) => dispatch_error.description(),
            InvokeEndpointError::ParseError(ref cause) => cause,
            InvokeEndpointError::Unknown(_) => "unknown error",
        }
    }
}
/// Trait representing the capabilities of the Amazon SageMaker Runtime API. Amazon SageMaker Runtime clients implement this trait.
pub trait SageMakerRuntime {
    ///
After you deploy a model into production using Amazon SageMaker hosting services, your client applications use this API to get inferences from the model hosted at the specified endpoint.

For an overview of Amazon SageMaker, see How It Works

 Amazon SageMaker strips all POST headers except those supported by the API. Amazon SageMaker might add additional headers. You should not rely on the behavior of headers outside those enumerated in the request syntax.

    fn invoke_endpoint(
        &self,
        input: InvokeEndpointInput,
    ) -> RusotoFuture<InvokeEndpointOutput, InvokeEndpointError>;
}
/// A client for the Amazon SageMaker Runtime API.
pub struct SageMakerRuntimeClient {
    client: Client,
    region: region::Region,
}

impl SageMakerRuntimeClient {
    /// Creates a client backed by the default tokio event loop.
    ///
    /// The client will use the default credentials provider and tls client.
    pub fn new(region: region::Region) -> SageMakerRuntimeClient {
        SageMakerRuntimeClient {
            client: Client::shared(),
            region: region,
        }
    }

    pub fn new_with
(
        request_dispatcher: D,
        credentials_provider: P,
        region: region::Region,
    ) -> SageMakerRuntimeClient
    where
        P: ProvideAwsCredentials + Send + Sync + 'static,
        P::Future: Send,
        D: DispatchSignedRequest + Send + Sync + 'static,
        D::Future: Send,
    {
        SageMakerRuntimeClient {
            client: Client::new_with(credentials_provider, request_dispatcher),
            region: region,
        }
    }
}

impl SageMakerRuntime for SageMakerRuntimeClient {
    ///
After you deploy a model into production using Amazon SageMaker hosting services, your client applications use this API to get inferences from the model hosted at the specified endpoint.

For an overview of Amazon SageMaker, see How It Works

 Amazon SageMaker strips all POST headers except those supported by the API. Amazon SageMaker might add additional headers. You should not rely on the behavior of headers outside those enumerated in the request syntax.

    fn invoke_endpoint(
        &self,
        input: InvokeEndpointInput,
    ) -> RusotoFuture<InvokeEndpointOutput, InvokeEndpointError> {
        let request_uri = format!(
            "/endpoints/{endpoint_name}/invocations",
            endpoint_name = input.endpoint_name
        );

        let mut request = SignedRequest::new("POST", "sagemaker", &self.region, &request_uri);
        if input.content_type.is_none() {
            request.set_content_type("application/x-amz-json-1.1".to_owned());
        }

        request.set_endpoint_prefix("runtime.sagemaker".to_string());
        let encoded = Some(input.body.to_owned());
        request.set_payload(encoded);

        if let Some(ref accept) = input.accept {
            request.add_header("Accept", &accept.to_string());
        }

        if let Some(ref content_type) = input.content_type {
            request.add_header("Content-Type", &content_type.to_string());
        }

        self.client.sign_and_dispatch(request, |response| {
            if response.status.is_success() {
                Box::new(response.buffer().from_err().map(|response| {
                    let mut result = InvokeEndpointOutput::default();
                    result.body = response.body;

                    if let Some(content_type) = response.headers.get("Content-Type") {
                        let value = content_type.to_owned();
                        result.content_type = Some(value)
                    };
                    if let Some(invoked_production_variant) =
                        response.headers.get("x-Amzn-Invoked-Production-Variant")
                    {
                        let value = invoked_production_variant.to_owned();
                        result.invoked_production_variant = Some(value)
                    };

                    result
                }))
            } else {
                Box::new(
                    response
                        .buffer()
                        .from_err()
                        .and_then(|response| Err(InvokeEndpointError::from_response(response))),
                )
            }
        })
    }
}

#[cfg(test)]
mod protocol_tests {}
<commit_msg>Re-generate with stable rust<commit_after>// =================================================================
//
//                           * WARNING *
//
//                    This file is generated!
//
//  Changes made to this file will be overwritten. If changes are
//  required to the generated code, the service_crategen project
//  must be updated to generate the changes.
//
// =================================================================

use std::error::Error;
use std::fmt;
use std::io;

#[allow(warnings)]
use futures::future;
use futures::Future;
use rusoto_core::region;
use rusoto_core::request::{BufferedHttpResponse, DispatchSignedRequest};
use rusoto_core::{Client, RusotoFuture};

use rusoto_core::credential::{CredentialsError, ProvideAwsCredentials};
use rusoto_core::request::HttpDispatchError;

use rusoto_core::signature::SignedRequest;
use serde_json;
use serde_json::from_slice;
use serde_json::Value as SerdeJsonValue;
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct InvokeEndpointInput {
    ///
The desired MIME type of the inference in the response.

    #[serde(rename = "Accept")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accept: Option<String>,
    ///
Provides input data, in the format specified in the ContentType request header. Amazon SageMaker passes all of the data in the body to the model.

    #[serde(rename = "Body")]
    #[serde(
        deserialize_with = "::rusoto_core::serialization::SerdeBlob::deserialize_blob",
        serialize_with = "::rusoto_core::serialization::SerdeBlob::serialize_blob",
        default
    )]
    pub body: Vec<u8>,
    ///
The MIME type of the input data in the request body.

    #[serde(rename = "ContentType")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_type: Option<String>,
    ///
The name of the endpoint that you specified when you created the endpoint using the CreateEndpoint API.

    #[serde(rename = "EndpointName")]
    pub endpoint_name: String,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct InvokeEndpointOutput {
    ///
Includes the inference provided by the model.

    pub body: Vec<u8>,
    ///
The MIME type of the inference returned in the response body.

    pub content_type: Option<String>,
    ///
Identifies the production variant that was invoked.

    pub invoked_production_variant: Option<String>,
}

/// Errors returned by InvokeEndpoint
#[derive(Debug, PartialEq)]
pub enum InvokeEndpointError {
    ///
 Internal failure occurred.

    InternalFailure(String),
    ///
 Model (owned by the customer in the container) returned an error 500.

    ModelError(String),
    ///
 Service is unavailable. Try your call again.

    ServiceUnavailable(String),
    ///
 Inspect your request and try again.

    ValidationError(String),
    /// An error occurred dispatching the HTTP request
    HttpDispatch(HttpDispatchError),
    /// An error was encountered with AWS credentials.
    Credentials(CredentialsError),
    /// A validation error occurred.  Details from AWS are provided.
    Validation(String),
    /// An error occurred parsing the response payload.
    ParseError(String),
    /// An unknown error occurred.  The raw HTTP response is provided.
    Unknown(BufferedHttpResponse),
}

impl InvokeEndpointError {
    // see boto RestJSONParser impl for parsing errors
    // https://github.com/boto/botocore/blob/4dff78c840403d1d17db9b3f800b20d3bd9fbf9f/botocore/parsers.py#L838-L850
    pub fn from_response(res: BufferedHttpResponse) -> InvokeEndpointError {
        if let Ok(json) = from_slice::<SerdeJsonValue>(&res.body) {
            let error_type = match res.headers.get("x-amzn-errortype") {
                Some(raw_error_type) => raw_error_type
                    .split(':')
                    .next()
                    .unwrap_or_else(|| "Unknown"),
                _ => json
                    .get("code")
                    .or_else(|| json.get("Code"))
                    .and_then(|c| c.as_str())
                    .unwrap_or_else(|| "Unknown"),
            };

            // message can come in either "message" or "Message"
            // see boto BaseJSONParser impl for parsing message
            // https://github.com/boto/botocore/blob/4dff78c840403d1d17db9b3f800b20d3bd9fbf9f/botocore/parsers.py#L595-L598
            let error_message = json
                .get("message")
                .or_else(|| json.get("Message"))
                .and_then(|m| m.as_str())
                .unwrap_or("");

            match error_type {
                "InternalFailure" => {
                    return InvokeEndpointError::InternalFailure(String::from(error_message))
                }
                "ModelError" => return InvokeEndpointError::ModelError(String::from(error_message)),
                "ServiceUnavailable" => {
                    return InvokeEndpointError::ServiceUnavailable(String::from(error_message))
                }
                "ValidationError" => {
                    return InvokeEndpointError::ValidationError(String::from(error_message))
                }
                "ValidationException" => {
                    return InvokeEndpointError::Validation(error_message.to_string())
                }
                _ => {}
            }
        }
        return InvokeEndpointError::Unknown(res);
    }
}

impl From<serde_json::error::Error> for InvokeEndpointError {
    fn from(err: serde_json::error::Error) -> InvokeEndpointError {
        InvokeEndpointError::ParseError(err.description().to_string())
    }
}
impl From<CredentialsError> for InvokeEndpointError {
    fn from(err: CredentialsError) -> InvokeEndpointError {
        InvokeEndpointError::Credentials(err)
    }
}
impl From<HttpDispatchError> for InvokeEndpointError {
    fn from(err: HttpDispatchError) -> InvokeEndpointError {
        InvokeEndpointError::HttpDispatch(err)
    }
}
impl From<io::Error> for InvokeEndpointError {
    fn from(err: io::Error) -> InvokeEndpointError {
        InvokeEndpointError::HttpDispatch(HttpDispatchError::from(err))
    }
}
impl fmt::Display for InvokeEndpointError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.description())
    }
}
impl Error for InvokeEndpointError {
    fn description(&self) -> &str {
        match *self {
            InvokeEndpointError::InternalFailure(ref cause) => cause,
            InvokeEndpointError::ModelError(ref cause) => cause,
            InvokeEndpointError::ServiceUnavailable(ref cause) => cause,
            InvokeEndpointError::ValidationError(ref cause) => cause,
            InvokeEndpointError::Validation(ref cause) => cause,
            InvokeEndpointError::Credentials(ref err) => err.description(),
            InvokeEndpointError::HttpDispatch(ref dispatch_error) => dispatch_error.description(),
            InvokeEndpointError::ParseError(ref cause) => cause,
            InvokeEndpointError::Unknown(_) => "unknown error",
        }
    }
}
/// Trait representing the capabilities of the Amazon SageMaker Runtime API. Amazon SageMaker Runtime clients implement this trait.
pub trait SageMakerRuntime {
    ///
After you deploy a model into production using Amazon SageMaker hosting services, your client applications use this API to get inferences from the model hosted at the specified endpoint.

For an overview of Amazon SageMaker, see How It Works

 Amazon SageMaker strips all POST headers except those supported by the API. Amazon SageMaker might add additional headers. You should not rely on the behavior of headers outside those enumerated in the request syntax.

    fn invoke_endpoint(
        &self,
        input: InvokeEndpointInput,
    ) -> RusotoFuture<InvokeEndpointOutput, InvokeEndpointError>;
}
/// A client for the Amazon SageMaker Runtime API.
pub struct SageMakerRuntimeClient {
    client: Client,
    region: region::Region,
}

impl SageMakerRuntimeClient {
    /// Creates a client backed by the default tokio event loop.
    ///
    /// The client will use the default credentials provider and tls client.
    pub fn new(region: region::Region) -> SageMakerRuntimeClient {
        SageMakerRuntimeClient {
            client: Client::shared(),
            region: region,
        }
    }

    pub fn new_with
(
        request_dispatcher: D,
        credentials_provider: P,
        region: region::Region,
    ) -> SageMakerRuntimeClient
    where
        P: ProvideAwsCredentials + Send + Sync + 'static,
        P::Future: Send,
        D: DispatchSignedRequest + Send + Sync + 'static,
        D::Future: Send,
    {
        SageMakerRuntimeClient {
            client: Client::new_with(credentials_provider, request_dispatcher),
            region: region,
        }
    }
}

impl SageMakerRuntime for SageMakerRuntimeClient {
    ///
After you deploy a model into production using Amazon SageMaker hosting services, your client applications use this API to get inferences from the model hosted at the specified endpoint.

For an overview of Amazon SageMaker, see How It Works

 Amazon SageMaker strips all POST headers except those supported by the API. Amazon SageMaker might add additional headers. You should not rely on the behavior of headers outside those enumerated in the request syntax.

    fn invoke_endpoint(
        &self,
        input: InvokeEndpointInput,
    ) -> RusotoFuture<InvokeEndpointOutput, InvokeEndpointError> {
        let request_uri = format!(
            "/endpoints/{endpoint_name}/invocations",
            endpoint_name = input.endpoint_name
        );

        let mut request = SignedRequest::new("POST", "sagemaker", &self.region, &request_uri);
        if input.content_type.is_none() {
            request.set_content_type("application/x-amz-json-1.1".to_owned());
        }

        request.set_endpoint_prefix("runtime.sagemaker".to_string());
        let encoded = Some(input.body.to_owned());
        request.set_payload(encoded);

        if let Some(ref accept) = input.accept {
            request.add_header("Accept", &accept.to_string());
        }

        if let Some(ref content_type) = input.content_type {
            request.add_header("Content-Type", &content_type.to_string());
        }

        self.client.sign_and_dispatch(request, |response| {
            if response.status.is_success() {
                Box::new(response.buffer().from_err().map(|response| {
                    let mut result = InvokeEndpointOutput::default();
                    result.body = response.body;

                    if let Some(content_type) = response.headers.get("Content-Type") {
                        let value = content_type.to_owned();
                        result.content_type = Some(value)
                    };
                    if let Some(invoked_production_variant) =
                        response.headers.get("x-Amzn-Invoked-Production-Variant")
                    {
                        let value = invoked_production_variant.to_owned();
                        result.invoked_production_variant = Some(value)
                    };

                    result
                }))
            } else {
                Box::new(
                    response
                        .buffer()
                        .from_err()
                        .and_then(|response| Err(InvokeEndpointError::from_response(response))),
                )
            }
        })
    }
}

#[cfg(test)]
mod protocol_tests {}
<|endoftext|>


<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#![crate_name="calculator"]
#![crate_type="bin"]

extern crate thrift;

// TODO: move to TProtocol
// Decided to leave the same naming as in the C++ code; to be discussed
#[allow(dead_code)]
#[allow(non_camel_case_types)]
pub enum MessageType {
    T_CALL = 1,
    T_REPLY = 2,
    T_EXCEPTION = 3,
    T_ONEWAY = 4,
}

#[allow(dead_code)]
#[allow(non_camel_case_types)]
pub enum TThriftType {
  T_STOP       = 0,
  T_VOID       = 1,
  T_BOOL       = 2,
  T_BYTE       = 3,
//  T_I08        = 3, TODO
  T_I16        = 6,
  T_I32        = 8,
  T_U64        = 9,
  T_I64        = 10,
  T_DOUBLE     = 4,
  T_STRING     = 11,
//  T_UTF7       = 11, TODO
  T_STRUCT     = 12,
  T_MAP        = 13,
  T_SET        = 14,
  T_LIST       = 15,
  T_UTF8       = 16,
  T_UTF16      = 17,
}

static BINARY_PROTOCOL_VERSION_1: u32 = 0x80010000;

pub trait Transport {

    fn open(& mut self) -> Option<()> {
        // default is nothing
        Some(())
    }

    fn write_i8(& mut self, value: i8) -> i32;

    fn write_be_i32(& mut self, value: i32) -> i32;

    fn write(& mut self, value: &[u8]) -> i32;

    fn write_end(& mut self) -> i32 {
        // default is nothing
        0
    }

    fn flush(& mut self);

    fn close(& mut self) {
        // default is nothing
    }
}

// Matches corresponding C++ class.
// TODO: move to Transport
pub struct ThriftTransport<T: std::io::Writer> {
    writer: T
}

impl <T: std::io::Writer> ThriftTransport<T> {

    fn new(w: T) -> ThriftTransport<T> {
        ThriftTransport { writer: w }
    }
}

impl <T: std::io::Writer> Transport for ThriftTransport<T> {

    fn write_i8(& mut self, value: i8) -> i32 {
        match self.writer.write_i8(value) {
          Ok(_) => 4,
          Err(e) => panic!(e),
        }
    }

    fn write_be_i32(& mut self, value: i32) -> i32 {
        match self.writer.write_be_i32(value) {
          Ok(_) => 4,
          Err(e) => panic!(e),
        }
    }

    fn write(& mut self, value: &[u8]) -> i32 {
        match self.writer.write(value) {
          Ok(_) => 4,
          Err(e) => panic!(e),
        }
    }

    fn flush(& mut self) {
        // Flush the underlying transport.
        self.writer.flush();
    }
}

pub trait Protocol {

//    fn get_transport(&'a self) -> & mut ThriftBufferedTransport {
//        self.transport
//    }

    fn write_message_begin(& mut self, name: &str, message_type: MessageType, seq_id: i32) -> i32 {
        println!("Protocol: message begin");

        // TODO: strict write
        let mut wsize: i32 = 0;
        wsize += self.write_string(name);
        wsize += self.write_i8(message_type as i8);
        wsize += self.write_i32(seq_id);
        wsize
    }

    fn write_string(& mut self, s: &str) -> i32 {
        println!("Protocol: {}", s);
        s.len() as i32
    }

    fn write_i8(& mut self, b: i8) -> i32 {
        println!("Protocol: {}", b);
        1
    }

    fn write_i32(& mut self, i: i32) -> i32 {
        println!("Protocol: {}", i);
        4
    }

    fn write_binary(& mut self, value: &[u8]) -> i32;

    fn write_field_stop(& mut self) -> i32 {
        self.write_i8(TThriftType::T_STOP as i8)
    }

    fn write_message_end(& mut self) -> i32 {
        println!("Protocol: end");
        0
    }

    fn flush(& mut self);
}

// TODO: move to Protocol
struct ThriftBinaryProtocol<T: Transport> {
    transport: T
}

impl <T: Transport> ThriftBinaryProtocol<T> {

    fn new(transport: T) -> ThriftBinaryProtocol<T> {
        ThriftBinaryProtocol { transport: transport }
    }
}

impl <T: Transport> Protocol for ThriftBinaryProtocol<T> {

    // TODO: remove
    fn flush(& mut self) {
        self.transport.flush();
    }

  fn write_message_begin(
    & mut self,
    name: &str,
    message_type: MessageType,
    sequence_id: i32
  ) -> i32 {
    let version = BINARY_PROTOCOL_VERSION_1 as i32 | message_type as i32;
    self.write_i32(version);
    self.write_string(name);
    self.write_i32(sequence_id);
    0
  }

  fn write_i8(& mut self, value: i8) -> i32 {
      self.transport.write_i8(value)
  }

  fn write_i32(& mut self, value: i32) -> i32 {
      self.transport.write_be_i32(value)
  }

  fn write_string(& mut self, value: &str) -> i32 {
    self.write_binary(value.as_bytes());
    0
  }

  fn write_binary(& mut self, value: &[u8]) -> i32 {
    self.write_i32(value.len() as i32);
    self.transport.write(value)
  }
}

// TODO: shall be generated
// TODO: shall we add smth like _dummy: () here?
#[allow(dead_code)]
struct CalculatorPingArgs;

impl CalculatorPingArgs {

    #[allow(unused_variables)]
    fn write(&self, oprot: & mut Protocol) {
        println!("CalculatorPingArgs::write");

  //        oprot->incrementRecursionDepth();
  //xfer += oprot->writeStructBegin("Calculator_ping_args");

  //xfer += oprot->writeFieldStop();
        oprot.write_field_stop();
  //xfer += oprot->writeStructEnd();
  //oprot->decrementRecursionDepth();

    }
}

struct CalculatorClient<T: Protocol> {
    protocol: T
}

impl <T: Protocol> CalculatorClient<T> {

    fn new(protocol: T) -> CalculatorClient<T> {
        CalculatorClient { protocol: protocol }
    }

    fn ping(& mut self) {
        self.send_ping();
        self.receive_ping();
    }

    fn send_ping(& mut self) {
        let cseqid: i32 = 0;
        self.protocol.write_message_begin("ping", MessageType::T_CALL, cseqid);

        let args = CalculatorPingArgs;
        args.write(& mut self.protocol);

        self.protocol.write_message_end();

        //self.oprotocol.get_transport().write_end();
        //self.oprotocol.get_transport().flush();
        self.protocol.flush();
    }

    fn receive_ping(& mut self) {

    }
}

pub fn main() {
    let server_address = "127.0.0.1:9090";
    let addr : std::io::net::ip::SocketAddr = std::str::FromStr::from_str(server_address).expect("bad server address");
    let tcp = std::io::TcpStream::connect(addr).unwrap();
    let transport = ThriftTransport::new(tcp);
    let protocol = ThriftBinaryProtocol::new(transport);
    let mut client = CalculatorClient::new(protocol);

    client.ping();
    client.ping();
    client.ping();
    client.ping();
    client.ping();

    println!("PASS");
}
<commit_msg>Send and receive pings<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#![crate_name="calculator"]
#![crate_type="bin"]

extern crate thrift;

use std::io::net::ip;
use thrift::protocol::{MessageType, Type};
use thrift::transport::Transport;
use thrift::protocol::Protocol;
use thrift::protocol::binary_protocol::BinaryProtocol;

// TODO: shall be generated
//#[allow(dead_code)]
struct CalculatorPingArgs;

impl CalculatorPingArgs {

    #[allow(unused_variables)]
    fn write(&self, oprot: &Protocol, transport: & mut Transport) {
        oprot.write_struct_begin(transport, "Calculator_ping_args");
        oprot.write_field_stop(transport);
        oprot.write_struct_end(transport);
    }
}

struct CalculatorPingResult;

impl CalculatorPingResult {

    #[allow(unused_variables)]
    fn read(&self, iprot: &Protocol, transport: & mut Transport) {
        let fname = iprot.read_struct_begin(transport);

        loop {
            let (fname, ftype, fid) = iprot.read_field_begin(transport);
            if ftype == Type::TStop {
              break;
            }
            iprot.skip(transport, ftype);
            iprot.read_field_end(transport);
        }

        iprot.read_struct_end(transport);
    }
}

struct CalculatorClient<T: Transport, P: Protocol> {
    transport: T,
    protocol: P,
}

impl <T: Transport, P: Protocol> CalculatorClient<T, P> {

    fn ping(& mut self) {
        self.send_ping();
        self.receive_ping();
    }

    fn send_ping(& mut self) {
        let cseqid: i32 = 0;
        self.protocol.write_message_begin(& mut self.transport, "ping", MessageType::MtCall, cseqid);

        let args = CalculatorPingArgs;
        args.write(&self.protocol, & mut self.transport);

        self.protocol.write_message_end(& mut self.transport);

        //self.transport.write_end();
        self.transport.flush();
    }

    fn receive_ping(& mut self) {
      let (fname, mtype, rseqid) = self.protocol.read_message_begin(& mut self.transport);
      match mtype {
        MessageType::MtException => {
            // TODO
            //let x = ApplicationException;
            //x.read(& mut self protocol)
            //self.protocol.read_message_end();
            //transport.read_end();
            //throw x
        }
        MessageType::MtReply => {
            match fname.as_slice() {
                "ping" => {
                    let result = CalculatorPingResult;
                    result.read(&self.protocol, & mut self.transport);
                }
                _ => {
                    self.protocol.skip(& mut self.transport, Type::TStruct);
                }
            }
            self.protocol.read_message_end(& mut self.transport);
            //self.transport.read_end();
        }
        _ => {
            self.protocol.skip(& mut self.transport, Type::TStruct);
            self.protocol.read_message_end(& mut self.transport);
            //self.transport.read_end();
        }
      }
    }
}

pub fn main() {
    let server_address = "127.0.0.1:9090";
    let addr: ip::SocketAddr = std::str::FromStr::from_str(server_address)
        .expect("bad server address");
    let tcp = std::io::TcpStream::connect(addr).unwrap();

    let mut client = CalculatorClient{ protocol: BinaryProtocol, transport: tcp };

    client.ping();

    println!("PASS");
}
<|endoftext|>