Vendor dependencies

This commit is contained in:
2026-08-01 16:11:49 +03:00
parent 7f139a0241
commit 6b5e7f0f8b
29706 changed files with 9575646 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
#![allow(dead_code)]
use std::convert::From;
use derive_builder::Builder;
#[derive(PartialEq, Default, Debug, Clone)]
struct Uuid(i32);
#[derive(PartialEq, Default, Debug, Clone)]
struct Authentication(i32);
impl From<i32> for Uuid {
fn from(x: i32) -> Uuid {
Uuid(x)
}
}
impl From<i32> for Authentication {
fn from(x: i32) -> Authentication {
Authentication(x)
}
}
#[derive(Debug, Default, Builder)]
#[builder(setter(into))]
struct Channel {
id: Uuid,
token: Authentication,
special_info: i32,
}
fn main() {
let ch = ChannelBuilder::default()
.special_info(42)
.id(0)
.token(5_494_192)
.build()
.unwrap();
println!("{:?}", ch);
}
@@ -0,0 +1,73 @@
#![allow(dead_code)]
use derive_builder::Builder;
#[derive(Debug, Clone)]
pub enum ContentType {
Json,
Xml,
}
impl Default for ContentType {
fn default() -> Self {
Self::Json
}
}
#[derive(Debug, Builder)]
#[builder(
custom_constructor,
create_empty = "empty",
build_fn(private, name = "fallible_build")
)]
pub struct ApiClient {
// To make sure `host` and `key` are not changed after creation, tell derive_builder
// to create the fields but not to generate setters.
#[builder(setter(custom))]
host: String,
#[builder(setter(custom))]
key: String,
// uncommenting this field will cause the unit-test below to fail
// baz: String,
#[builder(default)]
content_type: ContentType,
}
impl ApiClient {
pub fn new(host: impl Into<String>, key: impl Into<String>) -> ApiClientBuilder {
ApiClientBuilder {
host: Some(host.into()),
key: Some(key.into()),
..ApiClientBuilder::empty()
}
}
}
impl ApiClientBuilder {
pub fn build(&self) -> ApiClient {
self.fallible_build()
.expect("All required fields were initialized")
}
}
fn main() {
dbg!(ApiClient::new("hello", "world")
.content_type(ContentType::Xml)
.build());
}
#[cfg(test)]
mod tests {
use super::ApiClient;
/// If any required fields are added to ApiClient and ApiClient::new was not updated,
/// the field will be `None` when this "infallible" build method is called, resulting
/// in a panic. Panicking is appropriate here, since the author of the builder promised
/// the caller that it was safe to call new(...) followed immediately by build(), and
/// the builder author is also the person who can correct the coding error by setting
/// a default for the new property or changing the signature of `new`
#[test]
fn api_client_new_collects_all_required_fields() {
ApiClient::new("hello", "world").build();
}
}
@@ -0,0 +1,52 @@
use derive_builder::Builder;
#[derive(Builder, PartialEq, Debug)]
struct Lorem {
ipsum: String,
#[builder(default = "self.default_dolor()?")]
dolor: String,
#[builder(default = "self.default_sit()?")]
sit: String,
#[builder(setter(skip), default = "self.default_amet()")]
amet: String,
}
impl LoremBuilder {
fn default_dolor(&self) -> Result<String, String> {
self.ipsum
.clone()
.ok_or_else(|| "ipsum must be initialized to build dolor".to_string())
}
fn default_sit(&self) -> Result<String, String> {
match self.ipsum {
Some(ref x) if x.chars().count() > 3 => Ok(format!("sit {}", x)),
_ => Err("ipsum must at least 3 chars to build sit".to_string()),
}
}
fn default_amet(&self) -> String {
if let Some(ref x) = self.ipsum {
format!("amet {}", x)
} else {
"..nothing there".to_string()
}
}
}
fn main() {
let x = LoremBuilder::default()
.ipsum("ipsum".to_string())
.build()
.unwrap();
assert_eq!(
x,
Lorem {
ipsum: "ipsum".to_string(),
dolor: "ipsum".to_string(),
sit: "sit ipsum".to_string(),
amet: "amet ipsum".to_string(),
}
);
}
@@ -0,0 +1,53 @@
//! This example shows using custom validation with a non-string error type.
//!
//! This relies on how the generated build function is constructed; the validator
//! is invoked in conjunction with the `?` operator, so anything that converts to
//! the generated `FooBuilderError` type is valid.
#![allow(dead_code)]
use std::fmt;
use derive_builder::{Builder, UninitializedFieldError};
fn validate_age(builder: &ExampleBuilder) -> Result<(), Error> {
match builder.age {
Some(age) if age > 150 => Err(Error::UnrealisticAge(age)),
_ => Ok(()),
}
}
#[derive(Debug, Builder)]
#[builder(setter(into), build_fn(validate = "validate_age", error = "Error"))]
struct Example {
name: String,
age: usize,
}
enum Error {
UninitializedField(&'static str),
UnrealisticAge(usize),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::UnrealisticAge(age) => write!(f, "Nobody is {} years old", age),
Self::UninitializedField(field) => write!(f, "Required field '{}' not set", field),
}
}
}
impl From<UninitializedFieldError> for Error {
fn from(e: UninitializedFieldError) -> Self {
Self::UninitializedField(e.field_name())
}
}
fn main() {
let person_err = ExampleBuilder::default()
.name("Jane Doe")
.age(200usize)
.build()
.unwrap_err();
println!("{}", person_err);
}
@@ -0,0 +1,53 @@
//! This example shows combining generics with custom errors and validation.
//!
//! Note the use of the type parameter in the `#[builder(...)]` attribute.
#![allow(dead_code)]
use derive_builder::{Builder, UninitializedFieldError};
trait Popular {
fn is_popular(&self) -> bool;
}
impl<'a> Popular for &'a str {
fn is_popular(&self) -> bool {
!self.starts_with('b')
}
}
#[derive(Debug, Builder)]
#[builder(build_fn(validate = "check_person", error = "Error<N>"))]
struct Person<N: Popular + Clone> {
name: N,
age: u16,
}
#[derive(Debug)]
enum Error<N> {
UninitializedField(&'static str),
UnpopularName(N),
}
impl<N> From<UninitializedFieldError> for Error<N> {
fn from(error: UninitializedFieldError) -> Self {
Self::UninitializedField(error.field_name())
}
}
fn check_person<N: Popular + Clone>(builder: &PersonBuilder<N>) -> Result<(), Error<N>> {
if let Some(name) = &builder.name {
if !name.is_popular() {
return Err(Error::UnpopularName(name.clone()));
}
}
Ok(())
}
fn main() {
dbg!(PersonBuilder::default()
.name("bill")
.age(71)
.build()
.unwrap_err());
}
@@ -0,0 +1,22 @@
//! Some people may have `#![deny(missing_docs)]` in their crate.
//!
//! NOTE: This can only be tested in examples, but not integration tests.
#![deny(missing_docs)]
use derive_builder::Builder;
/// Traditional form of communication.
#[derive(Debug, Builder)]
#[builder(setter(into))]
pub struct Letter {
/// Be creative.
pub message: String,
}
fn main() {
let x = LetterBuilder::default()
.message("Hello World!")
.build()
.unwrap();
println!("{}", x.message);
}
@@ -0,0 +1,13 @@
// NOTE: generate fully expanded version with `cargo expand`.
//
// cargo expand --example doc_example
use derive_builder::Builder;
#[allow(dead_code)]
#[derive(Builder)]
struct Lorem {
ipsum: u32,
}
fn main() {}
@@ -0,0 +1,23 @@
// NOTE: generate fully expanded version with `cargo expand`.
//
// cargo expand --example readme_example
#![allow(dead_code)]
use derive_builder::Builder;
#[derive(Default, Builder, Debug)]
#[builder(setter(into))]
struct Channel {
token: i32,
special_info: i32,
}
fn main() {
// builder pattern, go, go, go!...
let ch = ChannelBuilder::default()
.special_info(42u8)
.token(19_124)
.build()
.unwrap();
println!("{:?}", ch);
}
@@ -0,0 +1,33 @@
//! This example illustrates the use of `validate` to add a pre-build validation
//! step.
use derive_builder::Builder;
#[derive(Builder, Debug, PartialEq)]
#[builder(build_fn(validate = Self::validate))]
struct Lorem {
pub ipsum: u8,
}
impl LoremBuilder {
/// Check that `Lorem` is putting in the right amount of effort.
fn validate(&self) -> Result<(), String> {
if let Some(ref ipsum) = self.ipsum {
match *ipsum {
i if i < 20 => Err("Try harder".to_string()),
i if i > 100 => Err("You'll tire yourself out".to_string()),
_ => Ok(()),
}
} else {
Ok(())
}
}
}
fn main() {
// If we're trying too hard...
let x = LoremBuilder::default().ipsum(120).build().unwrap_err();
// .. the build will fail:
assert_eq!(&x.to_string(), "You'll tire yourself out");
}