Skip to main content

reifydb_type/value/blob/
utf8.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2025 ReifyDB
3
4use super::Blob;
5use crate::{
6	error::{Error, diagnostic::blob},
7	fragment::Fragment,
8};
9
10impl Blob {
11	pub fn from_utf8(fragment: Fragment) -> Self {
12		let fragment = fragment;
13		let utf8_str = fragment.text();
14		Blob::new(utf8_str.as_bytes().to_vec())
15	}
16
17	pub fn to_utf8(&self) -> Result<String, Error> {
18		match std::str::from_utf8(self.as_bytes()) {
19			Ok(s) => Ok(s.to_string()),
20			Err(e) => Err(Error(blob::invalid_utf8_sequence(e))),
21		}
22	}
23
24	pub fn to_utf8_lossy(&self) -> String {
25		String::from_utf8_lossy(self.as_bytes()).to_string()
26	}
27
28	pub fn from_str(fragment: Fragment) -> Self {
29		Self::from_utf8(fragment)
30	}
31}
32
33#[cfg(test)]
34pub mod tests {
35	use super::*;
36	use crate::fragment::Fragment;
37
38	#[test]
39	fn test_from_utf8() {
40		let blob = Blob::from_utf8(Fragment::testing("Hello, World!"));
41		assert_eq!(blob.as_bytes(), b"Hello, World!");
42	}
43
44	#[test]
45	fn test_from_utf8_unicode() {
46		let blob = Blob::from_utf8(Fragment::testing("Hello, δΈ–η•Œ! πŸ¦€"));
47		assert_eq!(blob.as_bytes(), "Hello, δΈ–η•Œ! πŸ¦€".as_bytes());
48	}
49
50	#[test]
51	fn test_from_utf8_empty() {
52		let blob = Blob::from_utf8(Fragment::testing(""));
53		assert_eq!(blob.as_bytes(), b"");
54	}
55
56	#[test]
57	fn test_to_utf8() {
58		let blob = Blob::new("Hello, δΈ–η•Œ!".as_bytes().to_vec());
59		assert_eq!(blob.to_utf8().unwrap(), "Hello, δΈ–η•Œ!");
60	}
61
62	#[test]
63	fn test_to_utf8_invalid() {
64		let blob = Blob::new(vec![0xFF, 0xFE]);
65		assert!(blob.to_utf8().is_err());
66	}
67
68	#[test]
69	fn test_to_utf8_lossy() {
70		let blob = Blob::new("Hello, δΈ–η•Œ!".as_bytes().to_vec());
71		assert_eq!(blob.to_utf8_lossy(), "Hello, δΈ–η•Œ!");
72
73		let invalid_blob = Blob::new(vec![0xFF, 0xFE]);
74		let lossy = invalid_blob.to_utf8_lossy();
75		assert!(lossy.contains('οΏ½')); // replacement character
76	}
77
78	#[test]
79	fn test_from_str() {
80		let blob = Blob::from_str(Fragment::testing("Hello!"));
81		assert_eq!(blob.as_bytes(), b"Hello!");
82	}
83
84	#[test]
85	fn test_utf8_roundtrip() {
86		let original = "Hello, δΈ–η•Œ! πŸ¦€ Test with emojis and unicode";
87		let blob = Blob::from_utf8(Fragment::testing(original));
88		let decoded = blob.to_utf8().unwrap();
89		assert_eq!(decoded, original);
90	}
91}