Skip to content

Commit 7ca92e7

Browse files
committed
feat(code): add options "create-bytes" and "update-bytes"
1 parent 07e32f3 commit 7ca92e7

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

src/bin/main.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
22
use clap_complete::{generate, Shell};
33
use dsync::{error::IOErrorToError, GenerationConfig, TableOptions};
4-
use dsync::{FileChangeStatus, StringType};
4+
use dsync::{BytesType, FileChangeStatus, StringType};
55
use std::collections::HashMap;
66
use std::io::{BufWriter, Write};
77
use std::path::PathBuf;
@@ -98,6 +98,14 @@ pub struct MainOptions {
9898
#[arg(long = "update-str", default_value = "string")]
9999
pub update_str: StringTypeCli,
100100

101+
/// Set which bytes type to use for Create* structs
102+
#[arg(long = "create-bytes", default_value = "vec")]
103+
pub create_bytes: BytesTypeCli,
104+
105+
/// Set which bytes type to use for Update* structs
106+
#[arg(long = "update-bytes", default_value = "vec")]
107+
pub update_bytes: BytesTypeCli,
108+
101109
/// Only Generate a single model file instead of a directory with "mod.rs" and "generated.rs"
102110
#[arg(long = "single-model-file")]
103111
pub single_model_file: bool,
@@ -140,6 +148,27 @@ impl From<StringTypeCli> for StringType {
140148
}
141149
}
142150

151+
#[derive(Debug, ValueEnum, Clone, PartialEq, Default)]
152+
pub enum BytesTypeCli {
153+
/// Use "String"
154+
#[default]
155+
Vec,
156+
/// Use "&str"
157+
Slice,
158+
/// Use "Cow<str>"
159+
Cow,
160+
}
161+
162+
impl From<BytesTypeCli> for BytesType {
163+
fn from(value: BytesTypeCli) -> Self {
164+
match value {
165+
BytesTypeCli::Vec => BytesType::Vec,
166+
BytesTypeCli::Slice => BytesType::Slice,
167+
BytesTypeCli::Cow => BytesType::Cow,
168+
}
169+
}
170+
}
171+
143172
fn main() {
144173
let res = actual_main();
145174

@@ -183,7 +212,9 @@ fn actual_main() -> dsync::Result<()> {
183212
let mut default_table_options = TableOptions::default()
184213
.autogenerated_columns(cols.iter().map(|t| t.as_str()).collect::<Vec<&str>>())
185214
.create_str_type(args.create_str.into())
186-
.update_str_type(args.update_str.into());
215+
.update_str_type(args.update_str.into())
216+
.create_bytes_type(args.create_bytes.into())
217+
.update_bytes_type(args.update_bytes.into());
187218

188219
#[cfg(feature = "tsync")]
189220
if args.tsync {

src/code.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ impl<'a> Struct<'a> {
314314
StructType::Update => self.opts.get_update_str_type().as_str().to_string(),
315315
StructType::Create => self.opts.get_create_str_type().as_str().to_string(),
316316
}
317+
} else if f.base_type == "Vec<u8>" {
318+
f.base_type = match self.ty {
319+
StructType::Read => f.base_type,
320+
StructType::Update => self.opts.get_update_bytes_type().as_str().to_string(),
321+
StructType::Create => self.opts.get_create_bytes_type().as_str().to_string(),
322+
}
317323
}
318324

319325
let mut field_type = f.to_rust_type();

src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ impl StringType {
5757
}
5858
}
5959

60+
/// Available options for bytes types
61+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
62+
pub enum BytesType {
63+
/// Use `Vec<u8>`
64+
#[default]
65+
Vec,
66+
/// Use `&[u8]`
67+
Slice,
68+
/// Use `Cow<[u8]>`
69+
Cow,
70+
}
71+
72+
impl BytesType {
73+
/// Get the current [BytesType] as a rust type string
74+
pub fn as_str(&self) -> &'static str {
75+
match self {
76+
BytesType::Vec => "Vec<u8>",
77+
BytesType::Slice => "&'a [u8]",
78+
BytesType::Cow => "Cow<'a, [u8]>",
79+
}
80+
}
81+
82+
/// Get the lifetime used for the current [BytesType]
83+
pub fn get_lifetime(&self) -> &'static str {
84+
match self {
85+
BytesType::Vec => "",
86+
BytesType::Slice => "'a",
87+
BytesType::Cow => "'a",
88+
}
89+
}
90+
}
91+
6092
/// Options for a individual table
6193
#[derive(Debug, Clone)]
6294
pub struct TableOptions<'a> {
@@ -85,6 +117,12 @@ pub struct TableOptions<'a> {
85117
/// Determines which string type to use for Update* structs
86118
update_str_type: StringType,
87119

120+
/// Determines which bytes type to use for Create* structs
121+
create_bytes_type: BytesType,
122+
123+
/// Determines which bytes type to use for Update* structs
124+
update_bytes_type: BytesType,
125+
88126
/// Only Generate a single model file instead of a directory with "mod.rs" and "generated.rs"
89127
single_model_file: bool,
90128

@@ -123,6 +161,14 @@ impl<'a> TableOptions<'a> {
123161
self.update_str_type
124162
}
125163

164+
pub fn get_create_bytes_type(&self) -> BytesType {
165+
self.create_bytes_type
166+
}
167+
168+
pub fn get_update_bytes_type(&self) -> BytesType {
169+
self.update_bytes_type
170+
}
171+
126172
pub fn get_autogenerated_columns(&self) -> &[&'_ str] {
127173
self.autogenerated_columns.as_deref().unwrap_or_default()
128174
}
@@ -193,6 +239,20 @@ impl<'a> TableOptions<'a> {
193239
}
194240
}
195241

242+
pub fn create_bytes_type(self, type_: BytesType) -> Self {
243+
Self {
244+
create_bytes_type: type_,
245+
..self
246+
}
247+
}
248+
249+
pub fn update_bytes_type(self, type_: BytesType) -> Self {
250+
Self {
251+
update_bytes_type: type_,
252+
..self
253+
}
254+
}
255+
196256
pub fn set_read_only(&mut self, value: bool) {
197257
self.read_only = value;
198258
}
@@ -214,6 +274,8 @@ impl<'a> TableOptions<'a> {
214274
fns: self.fns || other.fns,
215275
create_str_type: other.create_str_type,
216276
update_str_type: other.update_str_type,
277+
create_bytes_type: other.create_bytes_type,
278+
update_bytes_type: other.update_bytes_type,
217279
single_model_file: self.single_model_file || other.single_model_file,
218280
read_only: self.read_only || other.read_only,
219281
}
@@ -233,6 +295,8 @@ impl<'a> Default for TableOptions<'a> {
233295
fns: true,
234296
create_str_type: Default::default(),
235297
update_str_type: Default::default(),
298+
create_bytes_type: Default::default(),
299+
update_bytes_type: Default::default(),
236300
single_model_file: false,
237301
read_only: false,
238302
}

0 commit comments

Comments
 (0)