Skip to content

Commit

Permalink
Merge branch 'docker'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 11, 2024
2 parents ef8dc60 + 36f78cd commit 292a86e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
23 changes: 23 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM python:3.8-slim

# Install system dependencies
RUN apt-get update && apt-get install -y \
wget \
make \
build-essential \
curl \
git \
&& curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
&& . $HOME/.cargo/env \
&& rustup default stable \
&& rustup update \
&& rustc --version \
&& rustup component add rustfmt \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Set PATH environment variable to include the cargo bin directory
ENV PATH="/root/.cargo/bin:${PATH}"

# Run app.py when the container launches
CMD ["python", "app.py"]
8 changes: 8 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Google-ApiS-rs Dev Container",
"build": {
"context": "..",
"dockerfile": "Dockerfile"
},
"remoteUser": "root"
}
53 changes: 39 additions & 14 deletions google-apis-common/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod duration {
ParseIntError(std::num::ParseIntError),
SecondOverflow { seconds: i64, max_seconds: i64 },
SecondUnderflow { seconds: i64, min_seconds: i64 },
DurationSeconds { seconds: i64 },
}

impl From<std::num::ParseIntError> for ParseDurationError {
Expand Down Expand Up @@ -47,6 +48,9 @@ pub mod duration {
"seconds underflow (got {}, minimum seconds possible {})",
seconds, min_seconds
),
ParseDurationError::DurationSeconds { seconds } => {
write!(f, "Could not create a duration from {seconds}")
}
}
}
}
Expand Down Expand Up @@ -93,15 +97,18 @@ pub mod duration {
min_seconds: -MAX_SECONDS,
})
} else {
Ok(Duration::seconds(seconds) + Duration::nanoseconds(nanoseconds.into()))
Ok(Duration::try_seconds(seconds)
.ok_or(ParseDurationError::DurationSeconds { seconds })?
+ Duration::nanoseconds(nanoseconds.into()))
}
}

pub fn to_string(duration: &Duration) -> String {
let seconds = duration.num_seconds();
let nanoseconds = (*duration - Duration::seconds(seconds))
.num_nanoseconds()
.expect("absolute number of nanoseconds is less than 1 billion")
let nanoseconds = (*duration
- Duration::try_seconds(seconds).expect("Seconds in bounds to create Duration from"))
.num_nanoseconds()
.expect("absolute number of nanoseconds is less than 1 billion")
as i32;
if nanoseconds != 0 {
if seconds == 0 && nanoseconds.is_negative() {
Expand Down Expand Up @@ -140,7 +147,7 @@ pub mod standard_base64 {
use serde::{Deserialize, Deserializer, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::borrow::Cow;

pub struct Wrapper;

pub fn to_string(bytes: &Vec<u8>) -> String {
Expand Down Expand Up @@ -204,7 +211,7 @@ pub fn datetime_to_string(datetime: &chrono::DateTime<chrono::offset::Utc>) -> S

#[cfg(test)]
mod test {
use super::{duration, urlsafe_base64, standard_base64};
use super::{duration, standard_base64, urlsafe_base64};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

Expand Down Expand Up @@ -299,15 +306,24 @@ mod test {
#[test]
fn standard_base64_de_success_cases() {
let wrapper: Base64StandardWrapper =
serde_json::from_str(r#"{"bytes": "cVhabzk6U21uOkN+MylFWFRJMVFLdEh2MShmVHp9"}"#).unwrap();
assert_eq!(Some(b"qXZo9:Smn:C~3)EXTI1QKtHv1(fTz}".as_slice()), wrapper.bytes.as_deref());
serde_json::from_str(r#"{"bytes": "cVhabzk6U21uOkN+MylFWFRJMVFLdEh2MShmVHp9"}"#)
.unwrap();
assert_eq!(
Some(b"qXZo9:Smn:C~3)EXTI1QKtHv1(fTz}".as_slice()),
wrapper.bytes.as_deref()
);
}

#[test]
fn standard_base64_de_reader_success_cases() {
let standard: Base64StandardWrapper =
serde_json::from_reader(r#"{"bytes": "cVhabzk6U21uOkN+MylFWFRJMVFLdEh2MShmVHp9"}"#.as_bytes()).unwrap();
assert_eq!(Some(b"qXZo9:Smn:C~3)EXTI1QKtHv1(fTz}".as_slice()), standard.bytes.as_deref());
let standard: Base64StandardWrapper = serde_json::from_reader(
r#"{"bytes": "cVhabzk6U21uOkN+MylFWFRJMVFLdEh2MShmVHp9"}"#.as_bytes(),
)
.unwrap();
assert_eq!(
Some(b"qXZo9:Smn:C~3)EXTI1QKtHv1(fTz}".as_slice()),
standard.bytes.as_deref()
);
}

#[test]
Expand All @@ -326,7 +342,10 @@ mod test {

#[test]
fn urlsafe_base64_de_failure_cases() {
assert!(serde_json::from_str::<Base64URLSafeWrapper>(r#"{"bytes": "aGVsbG8gd29ybG+Q"}"#).is_err());
assert!(
serde_json::from_str::<Base64URLSafeWrapper>(r#"{"bytes": "aGVsbG8gd29ybG+Q"}"#)
.is_err()
);
}

#[test]
Expand All @@ -340,7 +359,10 @@ mod test {
bytes: Some(b"Hello world!".to_vec()),
};
let s = serde_json::to_string(&wrapper).expect("serialization of bytes infallible");
assert_eq!(wrapper, serde_json::from_str::<Base64URLSafeWrapper>(&s).unwrap());
assert_eq!(
wrapper,
serde_json::from_str::<Base64URLSafeWrapper>(&s).unwrap()
);
}

#[test]
Expand All @@ -349,7 +371,10 @@ mod test {
bytes: Some(b"Hello world!".to_vec()),
};
let s = serde_json::to_string(&wrapper).expect("serialization of bytes infallible");
assert_eq!(wrapper, serde_json::from_str::<Base64StandardWrapper>(&s).unwrap());
assert_eq!(
wrapper,
serde_json::from_str::<Base64StandardWrapper>(&s).unwrap()
);
}

#[test]
Expand Down

0 comments on commit 292a86e

Please sign in to comment.