Compare commits

..

No commits in common. "main" and "v4.0.0-rc1" have entirely different histories.

7 changed files with 15 additions and 205 deletions

View file

@ -13,6 +13,7 @@ jobs:
toolchain: toolchain:
- name: stable - name: stable
- name: nightly - name: nightly
- name: 1.74.1
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4

View file

@ -4,5 +4,3 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
kxio = "5.1"
tokio = { version = "1.43", features = ["full"] }

View file

@ -2,7 +2,15 @@ FROM docker.io/rust:1.84.0-alpine3.21
LABEL org.opencontainers.image.source=https://git.kemitix.net/kemitix/rust LABEL org.opencontainers.image.source=https://git.kemitix.net/kemitix/rust
RUN apk add --no-cache curl=8.11.1-r0 # nodejs - runtime used by forgejo/github actions
# curl - to download cargo-binstall
# clang & mold - faster linkers for rust
# pkgconfig - required to compile some rust `-sys` packages
# openssl-dev - build dependency for git-next
# dbus-dev - linux os interop (e.g. desktop notifications)
# git - git
RUN apk add nodejs curl clang pkgconfig mold openssl-dev dbus-dev git
RUN curl -L https://github.com/cargo-bins/cargo-binstall/releases/download/v1.10.19/cargo-binstall-x86_64-unknown-linux-musl.tgz -o cargo-binstall.tgz && \ RUN curl -L https://github.com/cargo-bins/cargo-binstall/releases/download/v1.10.19/cargo-binstall-x86_64-unknown-linux-musl.tgz -o cargo-binstall.tgz && \
tar -xzf cargo-binstall.tgz && \ tar -xzf cargo-binstall.tgz && \
rm cargo-binstall.tgz && \ rm cargo-binstall.tgz && \
@ -15,39 +23,15 @@ RUN cargo binstall -y \
cargo-mutants@25.0 \ cargo-mutants@25.0 \
release-plz@0.3 release-plz@0.3
# should be a no-op if the FROM line is up-to-date # install v1.74.1
RUN rustup update stable RUN rustup install 1.74.1 && rustup component add --toolchain 1.74.1 rustfmt clippy
RUN rustup component add --toolchain stable-x86_64-unknown-linux-musl rustfmt clippy # should be a no-op if the FROM line is up-to-date
RUN rustup update stable && rustup component add --toolchain stable rustfmt clippy
# install nightly # install nightly
RUN rustup install nightly && rustup component add --toolchain nightly rustfmt clippy RUN rustup install nightly && rustup component add --toolchain nightly rustfmt clippy
# nodejs - runtime used by forgejo/github actions
# curl - to download cargo-binstall
# clang & mold - faster linkers for rust
# pkgconfig - required to compile some rust `-sys` packages
# openssl-dev - build dependency for git-next
# dbus-dev - linux os interop (e.g. desktop notifications)
# perl - native-tls(vendored)
# git - git
RUN apk add --no-cache \
bash \
nodejs \
build-base \
pkgconfig \
libssl3 \
openssl-dev \
perl \
dbus-dev \
git
# clang \
# mold \
# dbus-dev \
RUN git config --global user.email "action@git.kemitix.net" && \ RUN git config --global user.email "action@git.kemitix.net" && \
git config --global user.name "ForgeJo Action. See: https://git.kemitix.net/kemitix/rust" git config --global user.name "ForgeJo Action. See: https://git.kemitix.net/kemitix/rust"

View file

@ -42,8 +42,6 @@ The available toolchain in the image are:
- cargo-chef - cargo-chef
- cargo-hack - cargo-hack
- release-plz - release-plz
- dbus-dev
- perl
### Scripts ### Scripts
@ -59,36 +57,3 @@ steps:
- name: Check for Ignored Files - name: Check for Ignored Files
run: check-for-ignored run: check-for-ignored
``` ```
## Caveats
### openssl
The alpine linux install doesn't build with this dependency. You can either compile `native-tls` with the `vendored` feature, or not use `openssl`.
#### vendoered native-tls
This crate *must* use the `vendored` feature in order to compile in the Alpine Linux image.
```toml
native-tls = { version = "0.2", features = ["vendored"] }
```
#### Don't use `openssl`
Check that none of your dependencies require `openssl`:
```bash
cargo tree --edges normal -i openssl
```
This will list the tree of dependencies that are bringing in `openssl`.
If you do need ssl/tls, try using `rustls`. e.g.
```toml
reqwest = { version = "0.12", default-features = false, features = [
"json",
"rustls-tls",
] }
```

View file

@ -1,19 +0,0 @@
image := "git.kemitix.net/kemitix/rust:test"
build:
docker build . -t {{ image }}
test: build
docker run --rm -v $PWD:/app/ {{ image }} cargo test
clippy: build
docker run --rm -v $PWD:/app/ {{ image }} cargo clippy
run: build
docker run --rm -v $PWD:/app/ {{ image }} cargo run
fmt: build
docker run --rm -v $PWD:/app/ {{ image }} cargo fmt
shell: build
docker run --rm -it -v $PWD:/app/ {{ image }} bash

View file

@ -1,105 +0,0 @@
/// This is an example to show fetching a file from a webiste and saving to a file
///
/// The example consts of:
///
/// - The main program, in `main()` - demonstrates how to setup `kxio` for use in prod
/// - A test module - demonstrates how to use `kxio` in tests
/// - sample functions - showing how to use `kxio` the body of your program, and be testable
///
/// NOTE: running this program with `cargo run --example get` will create and delete the file
/// `example-readme.md` in the current directory.
use std::path::Path;
use kxio::fs::FileHandle;
// #[tokio::main]
pub async fn main() -> kxio::Result<()> {
// Create a `Net` object for making real network requests.
let net: kxio::net::Net = kxio::net::new();
// Create a `FileSystem` object for accessing files within the current directory.
// The object created will return a `PathTraveral` error result if there is an attempt to\
// access a file outside of this directory.
let current_dir = std::env::current_dir().map_err(kxio::fs::Error::Io)?;
let fs: kxio::fs::FileSystem = kxio::fs::new(current_dir);
// The URL we will fetch - the readme for this library.
let url = "https://git.kemitix.net/kemitix/kxio/raw/branch/main/README.md";
// Create a PathBuf to a file within the directory that the `fs` object has access to.
let file_path = fs.base().join("example-readme.md");
// Create a generic handle for the file. This doesn't open the file, and always succeeds.
let path = fs.path(&file_path);
// Other options are;
// `fs.file(&file_path)` - for a file
// `fs.dir(&dir_path)` - for a directory
// Checks if the path exists (whether a file, directory, etc)
if path.exists()? {
eprintln!("The file {path} already exists. Aborting!");
return Ok(());
}
// Passes a reference to the `fs` and `net` objects for use by your program.
// Your programs should not know whether they are handling a mock or the real thing.
// Any file or network access should be made using these handlers to be properly testable.
let file = download_and_save_to_file(url, &file_path, &fs, &net).await?;
read_file(&file)?;
delete_file(file)?;
Ok(())
}
/// An function that uses a `FileSystem` and a `Net` object to interact with the outside world.
async fn download_and_save_to_file(
url: &str,
file_path: &Path,
// The filesystem abstraction
fs: &kxio::fs::FileSystem,
// The network abstraction
net: &kxio::net::Net,
) -> kxio::Result<FileHandle> {
println!("fetching: {url}");
// Makes a GET request that can be mocked in a test
let response = net.get(url).header("key", "value").send().await?;
// As you can see, we use [reqwest] under the hood.
//
// If you need to create a more complex request than the [kxio] fluent API allows, you
// can create a request using [reqwest] and pass it to [net.send(request)].
let body = response.text().await?;
println!("fetched {} bytes", body.bytes().len());
// Uses the file system abstraction to create a handle for a file.
let file: kxio::fs::PathReal<kxio::fs::FileMarker> = fs.file(file_path);
println!("writing file: {file}");
// Writes the body to the file.
file.write(body)?;
Ok(file)
}
/// A function that reads the file contents
fn read_file(file: &FileHandle) -> kxio::Result<()> {
println!("reading file: {file}");
// Creates a `Reader` which loaded the file into memory.
let reader: kxio::fs::Reader = file.reader()?;
let contents: &str = reader.as_str()?;
println!("{contents}");
Ok(())
}
/// A function that deletes the file
fn delete_file(file: FileHandle) -> kxio::Result<()> {
println!("deleting file: {file}");
file.remove()?;
Ok(())
}

View file

@ -1,17 +1,3 @@
// fn main() {
mod kxio;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Hello, world!"); println!("Hello, world!");
let rt = tokio::runtime::Runtime::new()?;
Ok(rt.block_on(crate::kxio::main())?)
}
#[cfg(test)]
mod tests {
#[test]
fn passes() {
println!("passes okay");
}
} }