mz_repr/
url.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Custom Protobuf types for the [`url`] crate.
11
12use mz_proto::{RustType, TryFromProtoError};
13use proptest::prelude::Strategy;
14use url::Url;
15
16include!(concat!(env!("OUT_DIR"), "/mz_repr.url.rs"));
17
18impl RustType<ProtoUrl> for Url {
19    fn into_proto(&self) -> ProtoUrl {
20        ProtoUrl {
21            url: self.to_string(),
22        }
23    }
24
25    fn from_proto(proto: ProtoUrl) -> Result<Self, TryFromProtoError> {
26        Ok(proto.url.parse()?)
27    }
28}
29
30pub fn any_url() -> impl Strategy<Value = Url> {
31    r"(http|https)://[a-z][a-z0-9]{0,10}/?([a-z0-9]{0,5}/?){0,3}".prop_map(|s| s.parse().unwrap())
32}
33
34#[cfg(test)]
35mod tests {
36    use mz_ore::assert_ok;
37    use mz_proto::protobuf_roundtrip;
38    use proptest::prelude::*;
39
40    use super::*;
41
42    proptest! {
43        #![proptest_config(ProptestConfig::with_cases(4096))]
44
45        #[mz_ore::test]
46        #[cfg_attr(miri, ignore)] // too slow
47        fn url_protobuf_roundtrip(expect in any_url() ) {
48            let actual = protobuf_roundtrip::<_, ProtoUrl>(&expect);
49            assert_ok!(actual);
50            assert_eq!(actual.unwrap(), expect);
51        }
52    }
53}