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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Custom Protobuf types for the [`url`] crate.

use mz_proto::{RustType, TryFromProtoError};
use proptest::prelude::Strategy;
use url::Url;

include!(concat!(env!("OUT_DIR"), "/mz_repr.url.rs"));

impl RustType<ProtoUrl> for Url {
    fn into_proto(&self) -> ProtoUrl {
        ProtoUrl {
            url: self.to_string(),
        }
    }

    fn from_proto(proto: ProtoUrl) -> Result<Self, TryFromProtoError> {
        Ok(proto.url.parse()?)
    }
}

pub fn any_url() -> impl Strategy<Value = Url> {
    r"(http|https)://[a-z][a-z0-9]{0,10}/?([a-z0-9]{0,5}/?){0,3}".prop_map(|s| s.parse().unwrap())
}

#[cfg(test)]
mod tests {
    use mz_ore::assert_ok;
    use mz_proto::protobuf_roundtrip;
    use proptest::prelude::*;

    use super::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(4096))]

        #[mz_ore::test]
        #[cfg_attr(miri, ignore)] // too slow
        fn url_protobuf_roundtrip(expect in any_url() ) {
            let actual = protobuf_roundtrip::<_, ProtoUrl>(&expect);
            assert_ok!(actual);
            assert_eq!(actual.unwrap(), expect);
        }
    }
}