mysqldump_mutator/lib.rs
1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! MySQL mysqldump stream processor / mutator in Rust
14//!
15//! This crate allows to parse and the insert values of a mysqldump file
16//! and change them on the fly. Its intended usage is to anonymize a
17//! mysqldump backup in order to safely allow to be shared between
18//! developers.
19//!
20//! ```rust,no_run
21//! use mysqldump_mutator::{InsertContext, Parser, SQLContextType, Token};
22//! # use std::fs::File;
23//! # use std::io::{self, BufReader};
24//! # fn main() -> io::Result<()> {
25//! # let file: String = std::env::args().collect::<Vec<String>>()[1].clone();
26//!
27//! let _ = Parser::parse_mysqldump(
28//! BufReader::new(File::open(&file)?),
29//! |context, token| match context {
30//! // You can get the origial token content with token.to_string()
31//! // Check the Token struct methods
32//! SQLContextType::Insert(InsertContext::Value((table_name, column_index)))
33//! if table_name == "users" && column_index == &3 =>
34//! {
35//! // You can change the value of the column in an inser.
36//! // Columns are identifies by index.
37//! Token::new(&"[REDACTED]".to_string(), Some('\''))
38//! }
39//! SQLContextType::ColumnDefinition((_table_name, _column_name, _column_index)) => {
40//! // Here you can take note of what index is each column in each table.
41//! # token
42//! }
43//! _ => token, // Or just return the original token
44//! },
45//! |tokens| {
46//! for token in tokens {
47//! // Token has implemented the Display trait.
48//! // Given that no token was replaced, this would generate an output.
49//! // byte-by-byte equal to the input.
50//! print!("{}", token)
51//! }
52//! },
53//! );
54//! # Ok(())
55//! # }
56//! ```
57//!
58
59#![warn(clippy::all)]
60#![forbid(unsafe_code)]
61
62mod ast;
63mod dialect;
64mod parser;
65mod tokenizer;
66
67pub use parser::Parser;
68pub use parser::{InsertContext, SQLContextType};
69pub use tokenizer::Token;