Skip to content

Commit

Permalink
Remove the various Name traits like DatabaseName (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev authored Dec 1, 2020
1 parent 70aef03 commit e42a076
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 151 deletions.
2 changes: 1 addition & 1 deletion sdk/cosmos/examples/database_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
println!("collection == {:?}", collection);
let collection_client = database.clone().into_collection_client(collection.id);

if collection_client.collection_name().name() == "democ" {
if collection_client.collection_name() == "democ" {
println!("democ!");

let data = r#"
Expand Down
4 changes: 0 additions & 4 deletions sdk/cosmos/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,5 @@ pub use crate::resources::*;

// Traits
pub use crate::traits::*;
#[doc(inline)]
pub use database::DatabaseName;
#[doc(inline)]
pub use user::UserName;

pub use permission::AuthorizationToken;
14 changes: 6 additions & 8 deletions sdk/cosmos/src/requests/create_collection_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use crate::resources::collection::{Collection, CollectionName, IndexingPolicy, PartitionKey};
use crate::resources::collection::{Collection, IndexingPolicy, PartitionKey};
use crate::resources::ResourceType;
use crate::responses::CreateCollectionResponse;
use azure_core::prelude::*;
Expand Down Expand Up @@ -27,7 +27,7 @@ pub struct CreateCollectionBuilder<
p_indexing_policy: PhantomData<IndexingPolicySet>,
p_partition_key: PhantomData<PartitionKeySet>,
offer: Option<Offer>,
collection_name: Option<&'a dyn CollectionName>,
collection_name: Option<&'a str>,
indexing_policy: Option<&'a IndexingPolicy>,
partition_key: Option<&'a PartitionKey>,
user_agent: Option<&'a str>,
Expand Down Expand Up @@ -86,7 +86,7 @@ where
IndexingPolicySet: ToAssign,
PartitionKeySet: ToAssign,
{
fn collection_name(&self) -> &'a dyn CollectionName {
fn collection_name(&self) -> &'a str {
self.collection_name.unwrap()
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ where
{
type O = CreateCollectionBuilder<'a, OfferSet, Yes, IndexingPolicySet, PartitionKeySet>;

fn with_collection_name(self, collection_name: &'a dyn CollectionName) -> Self::O {
fn with_collection_name(self, collection_name: &'a str) -> Self::O {
CreateCollectionBuilder {
database_client: self.database_client,
p_offer: PhantomData {},
Expand Down Expand Up @@ -366,10 +366,8 @@ impl<'a> CreateCollectionBuilder<'a, Yes, Yes, Yes, Yes> {
let req = ActivityIdOption::add_header(self, req);
let req = ConsistencyLevelOption::add_header(self, req);

let mut collection = Collection::new(
self.collection_name().name(),
self.indexing_policy().to_owned(),
);
let mut collection =
Collection::new(self.collection_name(), self.indexing_policy().to_owned());
collection.parition_key = self.partition_key().to_owned();

let body = serde_json::to_string(&collection)?;
Expand Down
8 changes: 4 additions & 4 deletions sdk/cosmos/src/requests/create_database_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
{
cosmos_client: &'a CosmosClient,
p_database_name: PhantomData<DatabaseNameSet>,
database_name: Option<&'a dyn DatabaseName>,
database_name: Option<&'a str>,
user_agent: Option<&'a str>,
activity_id: Option<&'a str>,
consistency_level: Option<ConsistencyLevel>,
Expand Down Expand Up @@ -43,7 +43,7 @@ where
}

impl<'a> DatabaseNameRequired<'a> for CreateDatabaseBuilder<'a, Yes> {
fn database_name(&self) -> &'a dyn DatabaseName {
fn database_name(&self) -> &'a str {
self.database_name.unwrap()
}
}
Expand Down Expand Up @@ -78,7 +78,7 @@ where
impl<'a> DatabaseNameSupport<'a> for CreateDatabaseBuilder<'a, No> {
type O = CreateDatabaseBuilder<'a, Yes>;

fn with_database_name(self, database_name: &'a dyn DatabaseName) -> Self::O {
fn with_database_name(self, database_name: &'a str) -> Self::O {
CreateDatabaseBuilder {
cosmos_client: self.cosmos_client,
p_database_name: PhantomData {},
Expand Down Expand Up @@ -155,7 +155,7 @@ impl<'a> CreateDatabaseBuilder<'a, Yes> {
}

let req = serde_json::to_string(&CreateDatabaseRequest {
id: self.database_name().name(),
id: self.database_name(),
})?;

let request = self.cosmos_client().prepare_request(
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/src/requests/create_permission_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'a, 'b> CreatePermissionBuilder<'a, 'b> {
&format!(
"dbs/{}/users/{}/permissions",
self.permission_client.database_client().database_name(),
self.permission_client.user_client().user_name().id(),
self.permission_client.user_client().user_name(),
),
http::Method::POST,
ResourceType::Permissions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl<'a, 'b> CreateReferenceAttachmentBuilder<'a, 'b, Yes, Yes> {
}

let request = serde_json::to_string(&_Request {
id: self.attachment_client.attachment_name().name(),
id: self.attachment_client.attachment_name(),
content_type: ContentTypeRequired::content_type(self),
media: self.media(),
})?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/src/requests/create_slug_attachment_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<'a, 'b> CreateSlugAttachmentBuilder<'a, 'b, Yes, Yes> {

req = ContentTypeRequired::add_header(self, req);

req = req.header("Slug", self.attachment_client.attachment_name().name());
req = req.header("Slug", self.attachment_client.attachment_name());
req = req.header(http::header::CONTENT_LENGTH, self.body().len());

let req = req.body(self.body())?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/src/requests/list_attachments_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<'a, 'b> ListAttachmentsBuilder<'a, 'b> {
"dbs/{}/colls/{}/docs/{}/attachments",
self.document_client.database_client().database_name(),
self.document_client.collection_client().collection_name(),
self.document_client.document_name().name()
self.document_client.document_name()
),
http::Method::GET,
ResourceType::Attachments,
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/src/requests/list_collections_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<'a> ListCollectionsBuilder<'a> {
pub async fn execute(&self) -> Result<ListCollectionsResponse, CosmosError> {
trace!("ListCollectionsBuilder::execute called");
let request = self.database_client.cosmos_client().prepare_request(
&format!("dbs/{}/colls", self.database_client.database_name().name()),
&format!("dbs/{}/colls", self.database_client.database_name()),
http::Method::GET,
ResourceType::Collections,
);
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/src/requests/list_permissions_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<'a, 'b> ListPermissionsBuilder<'a, 'b> {
&format!(
"dbs/{}/users/{}/permissions",
self.user_client.database_client().database_name(),
self.user_client.user_name().id(),
self.user_client.user_name()
),
http::Method::GET,
ResourceType::Permissions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl<'a, 'b> ReplaceReferenceAttachmentBuilder<'a, 'b, Yes, Yes> {
}

let request = serde_json::to_string(&_Request {
id: self.attachment_client.attachment_name().name(),
id: self.attachment_client.attachment_name(),
content_type: ContentTypeRequired::content_type(self),
media: self.media(),
})?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/src/requests/replace_slug_attachment_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<'a, 'b> ReplaceSlugAttachmentBuilder<'a, 'b, Yes, Yes> {

req = ContentTypeRequired::add_header(self, req);

req = req.header("Slug", self.attachment_client.attachment_name().name());
req = req.header("Slug", self.attachment_client.attachment_name());
req = req.header(http::header::CONTENT_LENGTH, self.body().len());

let req = req.body(self.body())?;
Expand Down
9 changes: 4 additions & 5 deletions sdk/cosmos/src/requests/replace_user_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::prelude::*;
use crate::resources::user::UserName;
use crate::responses::CreateUserResponse;
use azure_core::prelude::*;
use azure_core::{No, ToAssign, Yes};
Expand All @@ -14,7 +13,7 @@ where
{
user_client: &'a UserClient,
p_user_name: PhantomData<UserNameSet>,
user_name: Option<&'a dyn UserName>,
user_name: Option<&'a str>,
user_agent: Option<&'b str>,
activity_id: Option<&'b str>,
consistency_level: Option<ConsistencyLevel>,
Expand Down Expand Up @@ -43,7 +42,7 @@ where
}

impl<'a, 'b> UserNameRequired<'a> for ReplaceUserBuilder<'a, 'b, Yes> {
fn user_name(&self) -> &'a dyn UserName {
fn user_name(&self) -> &'a str {
self.user_name.unwrap()
}
}
Expand Down Expand Up @@ -78,7 +77,7 @@ where
impl<'a, 'b> UserNameSupport<'a> for ReplaceUserBuilder<'a, 'b, No> {
type O = ReplaceUserBuilder<'a, 'b, Yes>;

fn with_user_name(self, user_name: &'a dyn UserName) -> Self::O {
fn with_user_name(self, user_name: &'a str) -> Self::O {
ReplaceUserBuilder {
user_client: self.user_client,
p_user_name: PhantomData {},
Expand Down Expand Up @@ -150,7 +149,7 @@ impl<'a, 'b> ReplaceUserBuilder<'a, 'b, Yes> {
id: &'x str,
}
let request_body = RequestBody {
id: self.user_name().id(),
id: self.user_name(),
};
let request_body = serde_json::to_string(&request_body)?;

Expand Down
45 changes: 9 additions & 36 deletions sdk/cosmos/src/resources/collection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,19 @@ impl Collection {
id: id.to_owned(),
indexing_policy,
parition_key: PartitionKey::default(),
rid: "".to_owned(),
rid: String::new(),
ts: 0,
_self: "".to_owned(),
etag: "".to_owned(),
docs: "".to_owned(),
sprocs: "".to_owned(),
triggers: "".to_owned(),
udfs: "".to_owned(),
conflicts: "".to_owned(),
_self: String::new(),
etag: String::new(),
docs: String::new(),
sprocs: String::new(),
triggers: String::new(),
udfs: String::new(),
conflicts: String::new(),
}
}
}

pub trait CollectionName: std::fmt::Debug {
fn name(&self) -> &str;
}

impl CollectionName for Collection {
fn name(&self) -> &str {
&self.id
}
}

impl CollectionName for &str {
fn name(&self) -> &str {
self
}
}

impl CollectionName for String {
fn name(&self) -> &str {
self.as_ref()
}
}

impl Resource for Collection {
fn uri(&self) -> &str {
&self._self
Expand Down Expand Up @@ -106,16 +84,14 @@ pub enum DataType {
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialOrd, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum IndexingMode {
#[serde(rename = "consistent")]
Consistent,
#[serde(rename = "lazy")]
Lazy,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialOrd, PartialEq)]
pub struct IncludedPath {
#[serde(rename = "path")]
pub path: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "indexes")]
Expand All @@ -126,16 +102,13 @@ pub struct IncludedPath {
pub struct IncludedPathIndex {
#[serde(rename = "dataType")]
pub data_type: DataType,
#[serde(rename = "precision")]
#[serde(skip_serializing_if = "Option::is_none")]
pub precision: Option<i8>,
#[serde(rename = "kind")]
pub kind: KeyKind,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialOrd, PartialEq)]
pub struct ExcludedPath {
#[serde(rename = "path")]
pub path: String,
}

Expand Down
18 changes: 3 additions & 15 deletions sdk/cosmos/src/resources/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,13 @@ pub struct Database {
pub users: String,
}

pub trait DatabaseName: std::fmt::Debug {
fn name(&self) -> &str;
}

impl DatabaseName for Database {
fn name(&self) -> &str {
impl Database {
/// The name of the database
pub fn name(&self) -> &str {
&self.id
}
}

impl<R> DatabaseName for R
where
R: AsRef<str> + std::fmt::Debug,
{
fn name(&self) -> &str {
self.as_ref()
}
}

impl Resource for Database {
fn uri(&self) -> &str {
&self._self
Expand Down
22 changes: 0 additions & 22 deletions sdk/cosmos/src/resources/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,3 @@ impl<T> Resource for &Document<T> {
self.document_attributes._self()
}
}

pub trait DocumentName: std::fmt::Debug {
fn name(&self) -> &str;
}

impl DocumentName for &str {
fn name(&self) -> &str {
self
}
}

impl DocumentName for String {
fn name(&self) -> &str {
self.as_ref()
}
}

impl DocumentName for std::borrow::Cow<'_, str> {
fn name(&self) -> &str {
self.as_ref()
}
}
7 changes: 4 additions & 3 deletions sdk/cosmos/src/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
//!
//! You can learn about the Cosmos DB resource model [here](https://docs.microsoft.com/en-us/azure/cosmos-db/account-databases-containers-items).
mod attachment;
pub mod collection;
pub mod database;
pub mod document;
pub mod permission;
pub mod stored_procedure;
pub mod trigger;
pub mod user;

mod attachment;
mod database;
mod user;
mod user_defined_function;

#[doc(inline)]
Expand Down
Loading

0 comments on commit e42a076

Please sign in to comment.