Debian Codegraph @

src/string.rs

raw symbols SCIP
1 // Copyright (c) 2023 Harry [Majored] [[email protected]]
2 // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4 use craterust-analyzer cargo async_zip 0.0.18 crate/::errorrust-analyzer cargo async_zip 0.0.18 error/::{Resultrust-analyzer cargo async_zip 0.0.18 error/Result#, ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#};
5
6 /// A string encoding supported by this crate.
7 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
8 pub enum StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding# {
9 Utf8rust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Utf8#,
10 Rawrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Raw#,
11 }
12
13 /// A string wrapper for handling different encodings.
14 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
15 pub struct ZipStringrust-analyzer cargo async_zip 0.0.18 string/ZipString# {
16 encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding.: StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#,
17 rawrust-analyzer cargo async_zip 0.0.18 string/ZipString#raw.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
18 alternativerust-analyzer cargo async_zip 0.0.18 string/ZipString#alternative.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>,
19 }
20
21 impl ZipStringrust-analyzer cargo async_zip 0.0.18 string/ZipString# {
22 /// Constructs a new encoded string from its raw bytes and its encoding type.
23 ///
24 /// # Note
25 /// If the provided encoding is [`StringEncoding::Utf8`] but the raw bytes are not valid UTF-8 (ie. a call to
26 /// `std::str::from_utf8()` fails), the encoding is defaulted back to [`StringEncoding::Raw`].
27 pub fn newrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]new().(raw: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, mut encoding: StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#) -> Selfrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString] {
28 if let StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#::Utf8rust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Utf8# = encoding {
29 if stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&raw).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
30 encoding = StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#::Rawrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Raw#;
31 }
32 }
33
34 Selfrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString] { encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding., rawrust-analyzer cargo async_zip 0.0.18 string/ZipString#raw., alternativerust-analyzer cargo async_zip 0.0.18 string/ZipString#alternative.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# }
35 }
36
37 /// Constructs a new encoded string from utf-8 data, with an alternative in native MBCS encoding.
38 pub fn new_with_alternativerust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]new_with_alternative().(utf8: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, alternative: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Selfrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString] {
39 Selfrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString] { encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding.: StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#::Utf8rust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Utf8#, rawrust-analyzer cargo async_zip 0.0.18 string/ZipString#raw.: utf8.into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().(), alternativerust-analyzer cargo async_zip 0.0.18 string/ZipString#alternative.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(alternative) }
40 }
41
42 /// Returns the raw bytes for this string.
43 pub fn as_bytesrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]as_bytes().(&self) -> &[u8] {
44 &self.rawrust-analyzer cargo async_zip 0.0.18 string/ZipString#raw.
45 }
46
47 /// Returns the encoding type for this string.
48 pub fn encodingrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]encoding().(&self) -> StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding# {
49 self.encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding.
50 }
51
52 /// Returns the alternative bytes (in native MBCS encoding) for this string.
53 pub fn alternativerust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]alternative().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&[u8]> {
54 self.alternativerust-analyzer cargo async_zip 0.0.18 string/ZipString#alternative..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
55 }
56
57 /// Returns the raw bytes converted into a string slice.
58 ///
59 /// # Note
60 /// A call to this method will only succeed if the encoding type is [`StringEncoding::Utf8`].
61 pub fn as_strrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]as_str().(&self) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<&str> {
62 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self.encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding., StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#::Utf8rust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Utf8#) {
63 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#::StringNotUtf8rust-analyzer cargo async_zip 0.0.18 error/ZipError#StringNotUtf8#);
64 }
65
66 // SAFETY:
67 // "The bytes passed in must be valid UTF-8.'
68 //
69 // This function will error if self.encoding is not StringEncoding::Utf8.
70 //
71 // self.encoding is only ever StringEncoding::Utf8 if this variant was provided to the constructor AND the
72 // call to `std::str::from_utf8()` within the constructor succeeded. Mutable access to the inner vector is
73 // never given and no method implemented on this type mutates the inner vector.
74
75 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked().(&self.rawrust-analyzer cargo async_zip 0.0.18 string/ZipString#raw.) })
76 }
77
78 /// Returns the raw bytes converted to an owned string.
79 ///
80 /// # Note
81 /// A call to this method will only succeed if the encoding type is [`StringEncoding::Utf8`].
82 pub fn into_stringrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]into_string().(self) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
83 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self.encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding., StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#::Utf8rust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Utf8#) {
84 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#::StringNotUtf8rust-analyzer cargo async_zip 0.0.18 error/ZipError#StringNotUtf8#);
85 }
86
87 // SAFETY: See above.
88 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8_uncheckedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8_unchecked().(self.rawrust-analyzer cargo async_zip 0.0.18 string/ZipString#raw.) })
89 }
90
91 /// Returns the alternative bytes (in native MBCS encoding) converted to the owned.
92 pub fn into_alternativerust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]into_alternative().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> {
93 self.alternativerust-analyzer cargo async_zip 0.0.18 string/ZipString#alternative.
94 }
95
96 /// Returns whether this string is encoded as utf-8 without an alternative.
97 pub fn is_utf8_without_alternativerust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]is_utf8_without_alternative().(&self) -> bool {
98 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self.encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding., StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#::Utf8rust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Utf8#) && self.alternativerust-analyzer cargo async_zip 0.0.18 string/ZipString#alternative..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()
99 }
100 }
101
102 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> for ZipStringrust-analyzer cargo async_zip 0.0.18 string/ZipString# {
103 fn fromrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString][`From<String>`]from().(value: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString][`From<String>`] {
104 Selfrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString][`From<String>`] { encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding.: StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#::Utf8rust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Utf8#, rawrust-analyzer cargo async_zip 0.0.18 string/ZipString#raw.: value.into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().(), alternativerust-analyzer cargo async_zip 0.0.18 string/ZipString#alternative.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# }
105 }
106 }
107
108 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&str> for ZipStringrust-analyzer cargo async_zip 0.0.18 string/ZipString# {
109 fn fromrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString][`From<&str>`]from().(value: &str) -> Selfrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString][`From<&str>`] {
110 Selfrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString][`From<&str>`] { encodingrust-analyzer cargo async_zip 0.0.18 string/ZipString#encoding.: StringEncodingrust-analyzer cargo async_zip 0.0.18 string/StringEncoding#::Utf8rust-analyzer cargo async_zip 0.0.18 string/StringEncoding#Utf8#, rawrust-analyzer cargo async_zip 0.0.18 string/ZipString#raw.: value.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().().to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(), alternativerust-analyzer cargo async_zip 0.0.18 string/ZipString#alternative.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# }
111 }
112 }