reifydb_type/value/blob/
utf8.rs

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