oxcel 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,200 @@
1
+ use magnus::{prelude::*, r_hash::ForEach, Error, RHash, Ruby, Value};
2
+ use rust_xlsxwriter::{Color, Format, FormatAlign, FormatBorder, FormatUnderline};
3
+
4
+ pub fn parse_color_hex(hex: &str) -> Result<Color, String> {
5
+ let clean = hex.trim_start_matches('#');
6
+ let expanded: String;
7
+ let hex6 = match clean.len() {
8
+ 6 => clean,
9
+ 3 => {
10
+ let mut chars = clean.chars();
11
+ let (r, g, b) = (
12
+ chars.next().unwrap(),
13
+ chars.next().unwrap(),
14
+ chars.next().unwrap(),
15
+ );
16
+ expanded = format!("{r}{r}{g}{g}{b}{b}");
17
+ expanded.as_str()
18
+ }
19
+ _ => {
20
+ return Err(format!(
21
+ "invalid hex color: {hex:?} (expected 6-digit '#RRGGBB' or 3-digit '#RGB')"
22
+ ));
23
+ }
24
+ };
25
+ let rgb = u32::from_str_radix(hex6, 16).map_err(|_| {
26
+ format!("invalid hex color: {hex:?} (expected 6-digit '#RRGGBB' or 3-digit '#RGB')")
27
+ })?;
28
+ Ok(Color::RGB(rgb))
29
+ }
30
+
31
+ pub fn parse_color(ruby: &Ruby, hex: &str) -> Result<Color, Error> {
32
+ parse_color_hex(hex).map_err(|msg| Error::new(ruby.exception_arg_error(), msg))
33
+ }
34
+
35
+ pub fn parse_border(ruby: &Ruby, b: &str) -> Result<FormatBorder, Error> {
36
+ match b {
37
+ "thin" => Ok(FormatBorder::Thin),
38
+ "medium" => Ok(FormatBorder::Medium),
39
+ "thick" => Ok(FormatBorder::Thick),
40
+ "dashed" => Ok(FormatBorder::Dashed),
41
+ "dotted" => Ok(FormatBorder::Dotted),
42
+ "double" => Ok(FormatBorder::Double),
43
+ "hair" => Ok(FormatBorder::Hair),
44
+ other => Err(Error::new(
45
+ ruby.exception_arg_error(),
46
+ format!("unknown border: {other:?}"),
47
+ )),
48
+ }
49
+ }
50
+
51
+ pub fn parse_align(ruby: &Ruby, a: &str) -> Result<FormatAlign, Error> {
52
+ match a {
53
+ "left" => Ok(FormatAlign::Left),
54
+ "center" => Ok(FormatAlign::Center),
55
+ "right" => Ok(FormatAlign::Right),
56
+ "justify" => Ok(FormatAlign::Justify),
57
+ "fill" => Ok(FormatAlign::Fill),
58
+ "vcenter" | "middle" => Ok(FormatAlign::VerticalCenter),
59
+ "top" => Ok(FormatAlign::Top),
60
+ "bottom" => Ok(FormatAlign::Bottom),
61
+ other => Err(Error::new(
62
+ ruby.exception_arg_error(),
63
+ format!("unknown align: {other:?}"),
64
+ )),
65
+ }
66
+ }
67
+
68
+ pub fn build_format(ruby: &Ruby, opts: RHash) -> Result<Format, Error> {
69
+ let mut fmt = Some(Format::new());
70
+ opts.foreach(|k: String, v: Value| {
71
+ if v.is_nil() {
72
+ return Ok(ForEach::Continue);
73
+ }
74
+ let current = fmt.take().unwrap();
75
+ let next = match k.as_str() {
76
+ "bold" => {
77
+ if magnus::TryConvert::try_convert(v)? {
78
+ current.set_bold()
79
+ } else {
80
+ current
81
+ }
82
+ }
83
+ "italic" => {
84
+ if magnus::TryConvert::try_convert(v)? {
85
+ current.set_italic()
86
+ } else {
87
+ current
88
+ }
89
+ }
90
+ "strikethrough" => {
91
+ if magnus::TryConvert::try_convert(v)? {
92
+ current.set_font_strikethrough()
93
+ } else {
94
+ current
95
+ }
96
+ }
97
+ "underline" => {
98
+ if v.is_kind_of(ruby.class_true_class()) {
99
+ current.set_underline(FormatUnderline::Single)
100
+ } else if v.is_kind_of(ruby.class_false_class()) {
101
+ current
102
+ } else {
103
+ let u: String = magnus::TryConvert::try_convert(v)?;
104
+ let ul = match u.as_str() {
105
+ "single" => FormatUnderline::Single,
106
+ "double" => FormatUnderline::Double,
107
+ "single_accounting" => FormatUnderline::SingleAccounting,
108
+ "double_accounting" => FormatUnderline::DoubleAccounting,
109
+ other => {
110
+ return Err(Error::new(
111
+ ruby.exception_arg_error(),
112
+ format!("unknown underline: {other:?}"),
113
+ ));
114
+ }
115
+ };
116
+ current.set_underline(ul)
117
+ }
118
+ }
119
+ "font_size" => {
120
+ let sz: f64 = magnus::TryConvert::try_convert(v)?;
121
+ current.set_font_size(sz)
122
+ }
123
+ "font_name" => {
124
+ let name: String = magnus::TryConvert::try_convert(v)?;
125
+ current.set_font_name(&name)
126
+ }
127
+ "font_color" => {
128
+ let c: String = magnus::TryConvert::try_convert(v)?;
129
+ current.set_font_color(parse_color(ruby, &c)?)
130
+ }
131
+ "bg_color" => {
132
+ let c: String = magnus::TryConvert::try_convert(v)?;
133
+ current.set_background_color(parse_color(ruby, &c)?)
134
+ }
135
+ "num_format" | "format" => {
136
+ let nf: String = magnus::TryConvert::try_convert(v)?;
137
+ current.set_num_format(&nf)
138
+ }
139
+ "align" | "valign" => {
140
+ let a: String = magnus::TryConvert::try_convert(v)?;
141
+ current.set_align(parse_align(ruby, &a)?)
142
+ }
143
+ "border" => {
144
+ let b: String = magnus::TryConvert::try_convert(v)?;
145
+ current.set_border(parse_border(ruby, &b)?)
146
+ }
147
+ "border_top" => {
148
+ let b: String = magnus::TryConvert::try_convert(v)?;
149
+ current.set_border_top(parse_border(ruby, &b)?)
150
+ }
151
+ "border_bottom" => {
152
+ let b: String = magnus::TryConvert::try_convert(v)?;
153
+ current.set_border_bottom(parse_border(ruby, &b)?)
154
+ }
155
+ "border_left" => {
156
+ let b: String = magnus::TryConvert::try_convert(v)?;
157
+ current.set_border_left(parse_border(ruby, &b)?)
158
+ }
159
+ "border_right" => {
160
+ let b: String = magnus::TryConvert::try_convert(v)?;
161
+ current.set_border_right(parse_border(ruby, &b)?)
162
+ }
163
+ "border_color" => {
164
+ let c: String = magnus::TryConvert::try_convert(v)?;
165
+ current.set_border_color(parse_color(ruby, &c)?)
166
+ }
167
+ "text_wrap" => {
168
+ if magnus::TryConvert::try_convert(v)? {
169
+ current.set_text_wrap()
170
+ } else {
171
+ current
172
+ }
173
+ }
174
+ "shrink" => {
175
+ if magnus::TryConvert::try_convert(v)? {
176
+ current.set_shrink()
177
+ } else {
178
+ current
179
+ }
180
+ }
181
+ "indent" => {
182
+ let ind: u8 = magnus::TryConvert::try_convert(v)?;
183
+ current.set_indent(ind)
184
+ }
185
+ "rotation" => {
186
+ let rot: i16 = magnus::TryConvert::try_convert(v)?;
187
+ current.set_rotation(rot)
188
+ }
189
+ other => {
190
+ return Err(Error::new(
191
+ ruby.exception_arg_error(),
192
+ format!("unknown format option: {other:?}"),
193
+ ));
194
+ }
195
+ };
196
+ fmt = Some(next);
197
+ Ok(ForEach::Continue)
198
+ })?;
199
+ Ok(fmt.unwrap())
200
+ }
@@ -0,0 +1,65 @@
1
+ mod error;
2
+ mod format;
3
+ mod reader;
4
+ mod writer;
5
+
6
+ use magnus::{function, method, prelude::*, Error, Ruby};
7
+
8
+ use reader::NativeReader;
9
+ use writer::NativeWriter;
10
+
11
+ #[magnus::init]
12
+ fn init(ruby: &Ruby) -> Result<(), Error> {
13
+ let module = ruby.define_module("Oxcel")?;
14
+
15
+ let err_cls = module.define_error("Error", ruby.exception_standard_error())?;
16
+ module.define_error("FileError", err_cls)?;
17
+ module.define_error("ClosedWorkbookError", err_cls)?;
18
+
19
+ let reader_cls = module.define_class("NativeReader", ruby.class_object())?;
20
+ reader_cls.define_singleton_method("open", function!(NativeReader::open, 2))?;
21
+ reader_cls.define_singleton_method("open_bytes", function!(NativeReader::open_bytes, 2))?;
22
+ reader_cls.define_method("close", method!(NativeReader::close, 0))?;
23
+ reader_cls.define_method("closed?", method!(NativeReader::is_closed, 0))?;
24
+ reader_cls.define_method("sheet_names", method!(NativeReader::sheet_names, 0))?;
25
+ reader_cls.define_method("sheets_metadata", method!(NativeReader::sheets_metadata, 0))?;
26
+ reader_cls.define_method("dimensions", method!(NativeReader::dimensions, 1))?;
27
+ reader_cls.define_method("merged_regions", method!(NativeReader::merged_regions, 1))?;
28
+ reader_cls.define_method("formulas", method!(NativeReader::formulas, 1))?;
29
+ reader_cls.define_method("read_sheet", method!(NativeReader::read_sheet, 2))?;
30
+ reader_cls.define_method("each_row", method!(NativeReader::each_row, 3))?;
31
+
32
+ let writer_cls = module.define_class("NativeWriter", ruby.class_object())?;
33
+ writer_cls.define_singleton_method("new", function!(NativeWriter::new, 2))?;
34
+ writer_cls.define_method("closed?", method!(NativeWriter::is_closed, 0))?;
35
+ writer_cls.define_method("add_format", method!(NativeWriter::add_format, 1))?;
36
+ writer_cls.define_method("add_sheet", method!(NativeWriter::add_sheet, 2))?;
37
+ writer_cls.define_method("write_row", method!(NativeWriter::write_row, 5))?;
38
+ writer_cls.define_method("write_rows", method!(NativeWriter::write_rows, 5))?;
39
+ writer_cls.define_method("write_cell", method!(NativeWriter::write_cell, 5))?;
40
+ writer_cls.define_method("merge_range", method!(NativeWriter::merge_range, 7))?;
41
+ writer_cls.define_method("autofilter", method!(NativeWriter::autofilter, 5))?;
42
+ writer_cls.define_method(
43
+ "set_column_width",
44
+ method!(NativeWriter::set_column_width, 3),
45
+ )?;
46
+ writer_cls.define_method("set_row_height", method!(NativeWriter::set_row_height, 3))?;
47
+ writer_cls.define_method(
48
+ "set_freeze_panes",
49
+ method!(NativeWriter::set_freeze_panes, 3),
50
+ )?;
51
+ writer_cls.define_method("autofit", method!(NativeWriter::autofit, 1))?;
52
+ writer_cls.define_method("set_tab_color", method!(NativeWriter::set_tab_color, 2))?;
53
+ writer_cls.define_method(
54
+ "set_screen_gridlines",
55
+ method!(NativeWriter::set_screen_gridlines, 2),
56
+ )?;
57
+ writer_cls.define_method("protect", method!(NativeWriter::protect, 2))?;
58
+ writer_cls.define_method("insert_note", method!(NativeWriter::insert_note, 4))?;
59
+ writer_cls.define_method("add_dropdown", method!(NativeWriter::add_dropdown, 9))?;
60
+ writer_cls.define_method("set_page_setup", method!(NativeWriter::set_page_setup, 2))?;
61
+ writer_cls.define_method("save", method!(NativeWriter::save, 1))?;
62
+ writer_cls.define_method("to_bytes", method!(NativeWriter::to_bytes, 0))?;
63
+
64
+ Ok(())
65
+ }