Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target
index.node
**/node_modules
**/.DS_Store
Cargo.lock
npm-debug.log*
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "db-csv-faker"
version = "0.1.0"
description = "tiny native extendable npm package for mocking db .csvs"
authors = ["zkud"]
license = "MIT"
edition = "2018"
exclude = ["index.node"]

[dependencies]
uuid = { version = "0.8", features = ["v4"] }
rand = "0.8.4"

[lib]
crate-type = ["cdylib"]

[dependencies.neon]
version = "0.8"
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# db-csv-faker

**db-csv-faker:** tiny native extendable npm package for mocking db .csvs

This project was bootstrapped by [create-neon](https://www.npmjs.com/package/create-neon).

## Installing db-csv-faker

Installing db-csv-faker requires a [supported version of Node and Rust](https://github.com/neon-bindings/neon#platform-support).

You can install the project with npm. In the project directory, run:

```sh
$ npm install
```

This fully installs the project, including installing any dependencies and running the build.

## Building db-csv-faker

If you have already installed the project and only want to run the build, run:

```sh
$ npm run build
```

This command uses the [cargo-cp-artifact](https://github.com/neon-bindings/cargo-cp-artifact) utility to run the Rust build and copy the built library into `./index.node`.

## Exploring db-csv-faker

After building db-csv-faker, you can explore its exports at the Node REPL:

```sh
$ npm install
$ node
> require('.').hello()
"hello node"
```

## Available Scripts

In the project directory, you can run:

### `npm install`

Installs the project, including running `npm run build`.

### `npm build`

Builds the Node addon (`index.node`) from source.

### `npm test`

Runs the unit tests by calling `cargo test`. You can learn more about [adding tests to your Rust code](https://doc.rust-lang.org/book/ch11-01-writing-tests.html) from the [Rust book](https://doc.rust-lang.org/book/).

## Project Layout

The directory structure of this project is:

```
db-csv-faker/
├── Cargo.toml
├── README.md
├── index.node
├── package.json
├── src/
| └── lib.rs
└── target/
```

### Cargo.toml

The Cargo [manifest file](https://doc.rust-lang.org/cargo/reference/manifest.html), which informs the `cargo` command.

### README.md

This file.

### index.node

The Node addon—i.e., a binary Node module—generated by building the project. This is the main module for this package, as dictated by the `"main"` key in `package.json`.

Under the hood, a [Node addon](https://nodejs.org/api/addons.html) is a [dynamically-linked shared object](https://en.wikipedia.org/wiki/Library_(computing)#Shared_libraries). The `"build"` script produces this file by copying it from within the `target/` directory, which is where the Rust build produces the shared object.

### package.json

The npm [manifest file](https://docs.npmjs.com/cli/v7/configuring-npm/package-json), which informs the `npm` command.

### src/

The directory tree containing the Rust source code for the project.

### src/lib.rs

The Rust library's main module.

### target/

Binary artifacts generated by the Rust build.

## Learn More

To learn more about Neon, see the [Neon documentation](https://neon-bindings.com).

To learn more about Rust, see the [Rust documentation](https://www.rust-lang.org).

To learn more about Node, see the [Node documentation](https://nodejs.org).
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "db-csv-faker",
"version": "0.1.0",
"description": "tiny native extendable npm package for mocking db .csvs",
"main": "index.node",
"scripts": {
"build": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
"install": "npm run build",
"test": "cargo test"
},
"author": "zkud",
"license": "MIT",
"devDependencies": {
"cargo-cp-artifact": "^0.1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/rapid-d9t/db-csv-faker.git"
},
"keywords": [
"csv"
],
"bugs": {
"url": "https://github.com/rapid-d9t/db-csv-faker/issues"
},
"homepage": "https://github.com/rapid-d9t/db-csv-faker#readme"
}
1 change: 1 addition & 0 deletions src/dumpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

77 changes: 77 additions & 0 deletions src/fields/field_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::fmt;
use std::io;

#[derive(fmt::Debug)]
pub struct FieldError {
error_type: FieldErrorType,
message: String,
}

#[derive(fmt::Debug, Clone, PartialEq)]
pub enum FieldErrorType {
InvalidInputArgs,
}

impl fmt::Display for FieldErrorType {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FieldErrorType::InvalidInputArgs => write!(formatter, "InvalidInputArgs"),
}
}
}

impl FieldError {
pub fn new(message: String, error_type: FieldErrorType) -> FieldError {
FieldError {
message,
error_type,
}
}

pub fn get_message(&self) -> String {
self.message.clone()
}

pub fn get_error_type(&self) -> FieldErrorType {
self.error_type.clone()
}
}

impl fmt::Display for FieldError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"Field Error with type: {}, reason: {}",
self.error_type, self.message
)
}
}

#[cfg(test)]
mod tests {
use super::FieldError;
use super::FieldErrorType;

#[test]
fn it_inits() {
let invalid_args_error =
FieldError::new("field error".to_string(), FieldErrorType::InvalidInputArgs);

assert_eq!(invalid_args_error.get_message(), "field error");
assert_eq!(
invalid_args_error.get_error_type(),
FieldErrorType::InvalidInputArgs
);
}

#[test]
fn it_displayable() {
let invalid_args_error =
FieldError::new("field error".to_string(), FieldErrorType::InvalidInputArgs);

assert_eq!(
format!("{}", invalid_args_error),
"Field Error reason: InvalidInputArgs, reason: file error"
);
}
}
55 changes: 55 additions & 0 deletions src/fields/integer_32_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use neon::declare_types;
use neon::prelude::*;
use rand;
use rand::Rng;

pub struct Integer32Field {
foreign_key_to: Option<String>,
}

declare_types! {
pub class JsInteger32Field for Integer32Field {
init(mut cx) {
let properties = cx.argument::<JsObject>(0)?;

if let Ok(name) = properties.get(&mut cx, "foreignKeyTo") {
if let Ok(name) = name.downcast::<JsString>() {
return Ok(
Integer32Field {
foreign_key_to: Some(name.value())
}
);
}
}

Ok(Integer32Field {
foreign_key_to: None
})
}

method format(mut cx) {
let value = {
let this = cx.this();
let guard = cx.lock();
let fields = this.borrow(&guard);
let mut generator = rand::thread_rng();
let value: i32 = generator.gen();
format!("{}", value)
};
Ok(cx.string(value).upcast())
}

method isForeignKeyTo(mut cx) {
let foreign_key_to = {
let this = cx.this();
let guard = cx.lock();
let fields = this.borrow(&guard);
fields.foreign_key_to.clone()
};
match foreign_key_to {
Some(name) => Ok(cx.string(name).upcast()),
None => Ok(cx.undefined().upcast())
}
}
}
}
4 changes: 4 additions & 0 deletions src/fields/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod field_error;
pub mod integer_32_field;
pub mod string_field;
pub mod uuid_v4_field;
58 changes: 58 additions & 0 deletions src/fields/string_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use neon::declare_types;
use neon::prelude::*;
use rand;
use rand::distributions::Alphanumeric;
use rand::Rng;

pub struct StringField {
foreign_key_to: Option<String>,
}

declare_types! {
pub class JsStringField for StringField {
init(mut cx) {
let properties = cx.argument::<JsObject>(0)?;

if let Ok(name) = properties.get(&mut cx, "foreignKeyTo") {
if let Ok(name) = name.downcast::<JsString>() {
return Ok(
StringField {
foreign_key_to: Some(name.value())
}
);
}
}

Ok(StringField {
foreign_key_to: None
})
}

method format(mut cx) {
let value: String = {
let this = cx.this();
let guard = cx.lock();
let fields = this.borrow(&guard);
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(30)
.map(char::from)
.collect()
};
Ok(cx.string(value).upcast())
}

method isForeignKeyTo(mut cx) {
let foreign_key_to = {
let this = cx.this();
let guard = cx.lock();
let fields = this.borrow(&guard);
fields.foreign_key_to.clone()
};
match foreign_key_to {
Some(name) => Ok(cx.string(name).upcast()),
None => Ok(cx.undefined().upcast())
}
}
}
}
Loading