1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::ffi::{CStr,CString};
use std::os::raw::c_char;

mod loremipsum;

static CHICKEN: &'static str = "chicken";
static BUFFALO: &'static str = "buffalo";

pub trait Chickenize {
  fn chicken(&self) -> String {
    String::from(CHICKEN)
  }

  fn buffalo(&self) -> String {
    String::from(BUFFALO)
  }

  fn anonymize(&self, replacement: &str) -> String {
    String::from(replacement)
  }

  fn lorem_ipsum(&self) -> String {
    String::from("Lorem Ipsum")
  }
}

impl Chickenize for bool {}
impl Chickenize for char {}
impl Chickenize for i8 {}
impl Chickenize for i16 {}
impl Chickenize for i32 {}
impl Chickenize for i64 {}
impl Chickenize for u8 {}
impl Chickenize for u16 {}
impl Chickenize for u32 {}
impl Chickenize for u64 {}
impl Chickenize for isize {}
impl Chickenize for usize {}
impl Chickenize for f32 {}
impl Chickenize for f64 {}

// We are using an Anonymize macro here to avoid argument passing in runtime and keep
//    the concrete (standard) chickenizations as performant as possible.
#[macro_export]
macro_rules! Anonymize(
  ($text:expr, $replacement:expr) => (
  {
    let text_iterator = $text.chars().peekable();
    let mut chickenized = String::new();
    let mut seen_letter = false;
    for c in text_iterator {
      if !c.is_alphanumeric() {
        if seen_letter {
          chickenized.push_str($replacement);
          seen_letter = false;
        }
        chickenized.push(c);
      } else {
        seen_letter = true;
      }
    }
    if seen_letter {
      chickenized.push_str($replacement);
    }
    chickenized
  }
  )
);

impl<'a> Chickenize for &'a str {
  fn chicken(&self) -> String {
    Anonymize!(self, CHICKEN)
  }
  fn buffalo(&self) -> String {
    Anonymize!(self, BUFFALO)
  }
  fn anonymize(&self, replacement: &str) -> String {
    Anonymize!(self, replacement)
  }
  fn lorem_ipsum(&self) -> String {
    let mut li = loremipsum::Generator::default();
    Anonymize!(self, li.next_word())
  }
}

impl Chickenize for String {
  fn chicken(&self) -> String {
    self.as_str().chicken()
  }
  fn buffalo(&self) -> String {
    self.as_str().buffalo()
  }
  fn anonymize(&self, replacement: &str) -> String {
    self.as_str().anonymize(replacement)
  }
  fn lorem_ipsum(&self) -> String {
    self.as_str().lorem_ipsum()
  }
}

impl Chickenize for Vec<String> {
  fn chicken(&self) -> String {
    let chickenized_vec: Vec<String> = self.iter().map(|x| x.chicken()).collect();
    chickenized_vec.join(" ")
  }
}

impl Chickenize for Vec<i32> {
  fn chicken(&self) -> String {
    let chickenized_vec: Vec<String> = self.iter().map(|x| x.chicken()).collect();
    chickenized_vec.join(" ")
  }
}

#[no_mangle]
pub extern "C" fn chickenize(value: *const c_char) -> *mut c_char {
  let c_value = unsafe { CStr::from_ptr(value) };
  let chickenized = match c_value.to_str() {
    Ok(value) => value.chicken(),
    _ => String::new(),
  };
  CString::new(chickenized).unwrap().into_raw()
}

#[no_mangle]
pub extern "C" fn buffalo(value: *const c_char) -> *mut c_char {
  let c_value = unsafe { CStr::from_ptr(value) };
  let buffalo = match c_value.to_str() {
    Ok(value) => value.buffalo(),
    _ => String::new(),
  };
  CString::new(buffalo).unwrap().into_raw()
}

#[no_mangle]
pub extern "C" fn lorem_ipsum(value: *const c_char) -> *mut c_char {
  let c_value = unsafe { CStr::from_ptr(value) };
  let lorem_ipsum = match c_value.to_str() {
    Ok(value) => value.lorem_ipsum(),
    _ => String::new(),
  };
  CString::new(lorem_ipsum).unwrap().into_raw()
}

#[no_mangle]
pub extern "C" fn anonymize(value: *const c_char, replacement: *const c_char) -> *mut c_char {
  let c_value = unsafe { CStr::from_ptr(value) };
  let c_replacement = unsafe { CStr::from_ptr(replacement) };
  let anonymized = match c_value.to_str() {
    Ok(value) => match c_replacement.to_str() {
      Ok(replacement) => value.anonymize(replacement),
      _ => String::new()
    },
    _ => String::new(),
  };
  CString::new(anonymized).unwrap().into_raw()
}