nu_plugin_nw_ulid 0.2.0

Production-grade ULID (Universally Unique Lexicographically Sortable Identifier) utilities plugin for Nushell with cryptographically secure operations, enterprise-grade security, and streaming support
Documentation
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Base32 and hex encoding/decoding commands for ULIDs.

use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
    Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, Type, Value,
};

use crate::{UlidEngine, UlidPlugin};

/// Encodes data using Crockford Base32.
pub struct UlidEncodeBase32Command;

impl PluginCommand for UlidEncodeBase32Command {
    type Plugin = UlidPlugin;

    fn name(&self) -> &str {
        "ulid encode base32"
    }

    fn description(&self) -> &str {
        "Encode data to Base32 (Crockford variant, used by ULIDs)"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name())
            .optional(
                "data",
                SyntaxShape::Any,
                "Data to encode (string or binary)",
            )
            .input_output_types(vec![
                (Type::String, Type::String),
                (Type::Binary, Type::String),
            ])
            .category(Category::Hash)
    }

    fn examples(&self) -> Vec<Example<'_>> {
        vec![
            Example {
                example: "ulid encode base32 'hello world'",
                description: "Encode a string to Base32",
                result: None,
            },
            Example {
                example: "0x48656c6c6f20776f726c64 | ulid encode base32",
                description: "Encode binary data to Base32",
                result: None,
            },
        ]
    }

    fn run(
        &self,
        _plugin: &Self::Plugin,
        _engine: &EngineInterface,
        call: &EvaluatedCall,
        input: PipelineData,
    ) -> Result<PipelineData, LabeledError> {
        let data = if let Some(arg) = call.opt::<Value>(0)? {
            // Using positional argument
            match arg {
                Value::String { val, .. } => val.into_bytes(),
                Value::Binary { val, .. } => val,
                _ => {
                    return Err(LabeledError::new("Invalid input type")
                        .with_label("Expected string or binary data", call.head));
                }
            }
        } else {
            // Using pipeline input
            match input {
                PipelineData::Value(Value::String { val, .. }, _) => val.into_bytes(),
                PipelineData::Value(Value::Binary { val, .. }, _) => val,
                _ => {
                    return Err(LabeledError::new("Invalid input type")
                        .with_label("Expected string or binary data from pipeline", call.head));
                }
            }
        };

        let encoded = base32::encode(base32::Alphabet::Crockford, &data);
        Ok(PipelineData::Value(Value::string(encoded, call.head), None))
    }
}

/// Decodes Crockford Base32 data.
pub struct UlidDecodeBase32Command;

impl PluginCommand for UlidDecodeBase32Command {
    type Plugin = UlidPlugin;

    fn name(&self) -> &str {
        "ulid decode base32"
    }

    fn description(&self) -> &str {
        "Decode Base32 data (Crockford variant, used by ULIDs)"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name())
            .required("data", SyntaxShape::String, "Base32 string to decode")
            .switch("text", "Output as text instead of binary", Some('t'))
            .input_output_types(vec![
                (Type::String, Type::Binary),
                (Type::String, Type::String),
            ])
            .category(Category::Hash)
    }

    fn examples(&self) -> Vec<Example<'_>> {
        vec![
            Example {
                example: "ulid decode base32 'CSQPYRK1E8'",
                description: "Decode Base32 to binary",
                result: None,
            },
            Example {
                example: "ulid decode base32 'CSQPYRK1E8' --text",
                description: "Decode Base32 to text",
                result: Some(Value::string("hello", Span::test_data())),
            },
        ]
    }

    fn run(
        &self,
        _plugin: &Self::Plugin,
        _engine: &EngineInterface,
        call: &EvaluatedCall,
        _input: PipelineData,
    ) -> Result<PipelineData, LabeledError> {
        let data: String = call.req(0)?;
        let as_text = call.has_flag("text")?;

        match base32::decode(base32::Alphabet::Crockford, &data) {
            Some(decoded) => {
                let result = if as_text {
                    match String::from_utf8(decoded) {
                        Ok(text) => Value::string(text, call.head),
                        Err(_) => {
                            return Err(LabeledError::new("Invalid UTF-8")
                                .with_label("Decoded data is not valid UTF-8 text", call.head));
                        }
                    }
                } else {
                    Value::binary(decoded, call.head)
                };

                Ok(PipelineData::Value(result, None))
            }
            None => Err(LabeledError::new("Invalid Base32")
                .with_label("Failed to decode Base32 data", call.head)),
        }
    }
}

/// Encodes data as hexadecimal.
pub struct UlidEncodeHexCommand;

impl PluginCommand for UlidEncodeHexCommand {
    type Plugin = UlidPlugin;

    fn name(&self) -> &str {
        "ulid encode hex"
    }

    fn description(&self) -> &str {
        "Encode data to hexadecimal"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name())
            .optional(
                "data",
                SyntaxShape::Any,
                "Data to encode (string or binary)",
            )
            .switch("uppercase", "Use uppercase hex letters", Some('u'))
            .input_output_types(vec![
                (Type::String, Type::String),
                (Type::Binary, Type::String),
            ])
            .category(Category::Hash)
    }

    fn examples(&self) -> Vec<Example<'_>> {
        vec![
            Example {
                example: "ulid encode hex 'hello'",
                description: "Encode a string to hex",
                result: Some(Value::string("68656c6c6f", Span::test_data())),
            },
            Example {
                example: "ulid encode hex 'hello' --uppercase",
                description: "Encode a string to uppercase hex",
                result: Some(Value::string("68656C6C6F", Span::test_data())),
            },
        ]
    }

    fn run(
        &self,
        _plugin: &Self::Plugin,
        _engine: &EngineInterface,
        call: &EvaluatedCall,
        input: PipelineData,
    ) -> Result<PipelineData, LabeledError> {
        let uppercase = call.has_flag("uppercase")?;

        let data = if let Some(arg) = call.opt::<Value>(0)? {
            // Using positional argument
            match arg {
                Value::String { val, .. } => val.into_bytes(),
                Value::Binary { val, .. } => val,
                _ => {
                    return Err(LabeledError::new("Invalid input type")
                        .with_label("Expected string or binary data", call.head));
                }
            }
        } else {
            // Using pipeline input
            match input {
                PipelineData::Value(Value::String { val, .. }, _) => val.into_bytes(),
                PipelineData::Value(Value::Binary { val, .. }, _) => val,
                _ => {
                    return Err(LabeledError::new("Invalid input type")
                        .with_label("Expected string or binary data from pipeline", call.head));
                }
            }
        };

        let encoded = if uppercase {
            hex::encode_upper(&data)
        } else {
            hex::encode(&data)
        };

        Ok(PipelineData::Value(Value::string(encoded, call.head), None))
    }
}

/// Decodes hexadecimal data.
pub struct UlidDecodeHexCommand;

impl PluginCommand for UlidDecodeHexCommand {
    type Plugin = UlidPlugin;

    fn name(&self) -> &str {
        "ulid decode hex"
    }

    fn description(&self) -> &str {
        "Decode hexadecimal data"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name())
            .required("data", SyntaxShape::String, "Hex string to decode")
            .switch("text", "Output as text instead of binary", Some('t'))
            .input_output_types(vec![
                (Type::String, Type::Binary),
                (Type::String, Type::String),
            ])
            .category(Category::Hash)
    }

    fn examples(&self) -> Vec<Example<'_>> {
        vec![
            Example {
                example: "ulid decode hex '68656c6c6f'",
                description: "Decode hex to binary",
                result: None,
            },
            Example {
                example: "ulid decode hex '68656c6c6f' --text",
                description: "Decode hex to text",
                result: Some(Value::string("hello", Span::test_data())),
            },
        ]
    }

    fn run(
        &self,
        _plugin: &Self::Plugin,
        _engine: &EngineInterface,
        call: &EvaluatedCall,
        _input: PipelineData,
    ) -> Result<PipelineData, LabeledError> {
        let data: String = call.req(0)?;
        let as_text = call.has_flag("text")?;

        match hex::decode(&data) {
            Ok(decoded) => {
                let result = if as_text {
                    match String::from_utf8(decoded) {
                        Ok(text) => Value::string(text, call.head),
                        Err(_) => {
                            return Err(LabeledError::new("Invalid UTF-8")
                                .with_label("Decoded data is not valid UTF-8 text", call.head));
                        }
                    }
                } else {
                    Value::binary(decoded, call.head)
                };

                Ok(PipelineData::Value(result, None))
            }
            Err(e) => Err(LabeledError::new("Invalid hex")
                .with_label(format!("Failed to decode hex data: {}", e), call.head)),
        }
    }
}

/// Converts a ULID string to its native 16-byte binary representation.
pub struct UlidToBytesCommand;

impl PluginCommand for UlidToBytesCommand {
    type Plugin = UlidPlugin;

    fn name(&self) -> &str {
        "ulid to-bytes"
    }

    fn description(&self) -> &str {
        "Convert a ULID to its native 16-byte binary representation"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name())
            .optional("ulid", SyntaxShape::String, "The ULID string to convert")
            .input_output_types(vec![
                (Type::String, Type::Binary),
                (Type::Nothing, Type::Binary),
            ])
            .category(Category::Hash)
    }

    fn examples(&self) -> Vec<Example<'_>> {
        vec![
            Example {
                example: "ulid to-bytes '01AN4Z07BY79KA1307SR9X4MV3'",
                description: "Convert a ULID to its 16-byte binary representation",
                result: None,
            },
            Example {
                example: "ulid generate | ulid to-bytes",
                description: "Generate a ULID and convert it to binary via pipeline",
                result: None,
            },
        ]
    }

    fn run(
        &self,
        _plugin: &Self::Plugin,
        _engine: &EngineInterface,
        call: &EvaluatedCall,
        input: PipelineData,
    ) -> Result<PipelineData, LabeledError> {
        let ulid_str: String = if let Some(arg) = call.opt(0)? {
            arg
        } else {
            match input {
                PipelineData::Value(Value::String { val, .. }, _) => val,
                _ => {
                    return Err(LabeledError::new("Missing ULID").with_label(
                        "Provide a ULID string as an argument or via pipeline",
                        call.head,
                    ));
                }
            }
        };

        if !UlidEngine::validate(&ulid_str) {
            return Err(LabeledError::new("Invalid ULID")
                .with_label(format!("'{}' is not a valid ULID", ulid_str), call.head));
        }

        let ulid = ulid_str
            .parse::<ulid::Ulid>()
            .map_err(|e| LabeledError::new("Parse failed").with_label(e.to_string(), call.head))?;

        let bytes = UlidEngine::to_bytes(&ulid);
        Ok(PipelineData::Value(Value::binary(bytes, call.head), None))
    }
}

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

    mod ulid_to_bytes_command {
        use super::*;

        #[test]
        fn test_command_signature() {
            let cmd = UlidToBytesCommand;
            let sig = cmd.signature();
            assert_eq!(sig.name, "ulid to-bytes");
            assert_eq!(sig.optional_positional.len(), 1);
        }

        #[test]
        fn test_command_name() {
            assert_eq!(UlidToBytesCommand.name(), "ulid to-bytes");
        }

        #[test]
        fn test_command_description() {
            let desc = UlidToBytesCommand.description();
            assert!(desc.contains("binary"));
        }

        #[test]
        fn test_command_examples_not_empty() {
            assert!(!UlidToBytesCommand.examples().is_empty());
        }

        #[test]
        fn test_to_bytes_produces_16_bytes() {
            let ulid = UlidEngine::generate().unwrap();
            let bytes = UlidEngine::to_bytes(&ulid);
            assert_eq!(bytes.len(), 16);
        }

        #[test]
        fn test_to_bytes_roundtrip() {
            let ulid = UlidEngine::generate().unwrap();
            let bytes = UlidEngine::to_bytes(&ulid);
            let restored = ulid::Ulid::from_bytes(bytes.try_into().unwrap());
            assert_eq!(ulid, restored);
        }
    }
}