Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/repo prep #1

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Rust

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
ci:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: fmt
run: cargo fmt --check
- name: Run tests
run: cargo test --verbose
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
[package]
name = "surt-rs"
version = "0.1.0"
authors = ["Mark Johnson <mark@mijho.co>"]
description = "A Rust implementation of the Sort-friendly URI Reordering Transform (SURT)"
license = "MIT"
readme = "README.md"
homepage = "https://github.com/mijho/surt-rs"
documentation = "https://github.com/mijho/surt-rs/blob/main/README.md"
keywords = ["surt", "web", "url", "normalisation", "normalization", "web-archiving", "archive"]
categories = ["text-processing"]
edition = "2021"

[lib]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rust SURT Library
# Rust SURT

This library provides a Rust implementation for generating a Sort-friendly URI Reordering Transform (SURT) from a given URL. These are predominantly used in the Web Archiving world.
This library provides a Rust implementation for generating a Sort-friendly URI Reordering Transform (SURT) from a given URL. These are predominantly used in the Web Archiving world to provide a normalised and sortable variant of a URL for use at replay time.

## Usage

7 changes: 3 additions & 4 deletions src/bin/surt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use url::{ParseError};
use std::env;
use surt_rs::{generate_surt};

use surt_rs::generate_surt;
use url::ParseError;

// This will generate the SURT for the given URL
fn main() -> Result<(), ParseError> {
let args: Vec<String> = env::args().collect();
let url = &args[1];
let url = &args[1];
let surt = generate_surt(url).unwrap_or_else(|_| url.to_string());

println!("{}", surt);
187 changes: 93 additions & 94 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use url::{Url, ParseError};

use url::{ParseError, Url};

fn normalize_surt(surt: &str) -> String {
let mut surt = surt.to_string();
@@ -53,7 +52,7 @@ pub fn generate_surt(url: &str) -> Result<String, ParseError> {
let scheme = parsed.scheme();
match scheme == "https" || scheme == "http" {
true => scheme,
_ => return Err(ParseError::RelativeUrlWithoutBase)
_ => return Err(ParseError::RelativeUrlWithoutBase),
};

if parsed.host_str().is_none() {
@@ -67,7 +66,7 @@ pub fn generate_surt(url: &str) -> Result<String, ParseError> {
if parsed.port().is_some() {
let port = parsed.port().unwrap();
surt += &format!(":{}", port);
}
}

if parsed.path() != "" {
let path = parsed.path().to_lowercase();
@@ -92,93 +91,93 @@ pub fn generate_surt(url: &str) -> Result<String, ParseError> {

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_generate_surt_with_valid_url() {
let url = "http://example.com/path?query=value#fragment";
let expected = "com,example)/path?query=value#fragment";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_without_scheme() {
let url = "example.com";
assert!(generate_surt(url).is_err());
}

#[test]
fn test_generate_surt_with_relative_url() {
let url = "/path";
assert!(generate_surt(url).is_err());
}

#[test]
fn test_generate_surt_with_url_without_host() {
let url = "http://";
assert!(generate_surt(url).is_err());
}

#[test]
fn test_generate_surt_with_url_with_port() {
let url = "http://example.com:8080";
let expected = "com,example:8080)/";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_query() {
let url = "http://example.com?query=value";
let expected = "com,example)/?query=value";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_query_and_trailing_slash_after_path() {
let url = "http://example.com/foo/bar/?query=value";
let expected = "com,example)/foo/bar?query=value";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_fragment() {
let url = "http://example.com#fragment";
let expected = "com,example)/#fragment";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_uppercase() {
let url = "http://EXAMPLE.COM/PATH?QUERY=VALUE#FRAGMENT";
let expected = "com,example)/path?query=value#fragment";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_space() {
let url = "http://example.com/path with space";
let expected = "com,example)/path%20with%20space";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_trailing_slash() {
let url = "http://example.com/";
let expected = "com,example)/";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_trailing_slash_after_path() {
let url = "http://example.com/foo/bar/";
let expected = "com,example)/foo/bar";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_www_subdomain() {
let url = "http://www.example.com";
let expected = "com,example)/";
assert_eq!(generate_surt(url).unwrap(), expected);
}
}
use super::*;

#[test]
fn test_generate_surt_with_valid_url() {
let url = "http://example.com/path?query=value#fragment";
let expected = "com,example)/path?query=value#fragment";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_without_scheme() {
let url = "example.com";
assert!(generate_surt(url).is_err());
}

#[test]
fn test_generate_surt_with_relative_url() {
let url = "/path";
assert!(generate_surt(url).is_err());
}

#[test]
fn test_generate_surt_with_url_without_host() {
let url = "http://";
assert!(generate_surt(url).is_err());
}

#[test]
fn test_generate_surt_with_url_with_port() {
let url = "http://example.com:8080";
let expected = "com,example:8080)/";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_query() {
let url = "http://example.com?query=value";
let expected = "com,example)/?query=value";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_query_and_trailing_slash_after_path() {
let url = "http://example.com/foo/bar/?query=value";
let expected = "com,example)/foo/bar?query=value";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_fragment() {
let url = "http://example.com#fragment";
let expected = "com,example)/#fragment";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_uppercase() {
let url = "http://EXAMPLE.COM/PATH?QUERY=VALUE#FRAGMENT";
let expected = "com,example)/path?query=value#fragment";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_space() {
let url = "http://example.com/path with space";
let expected = "com,example)/path%20with%20space";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_trailing_slash() {
let url = "http://example.com/";
let expected = "com,example)/";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_trailing_slash_after_path() {
let url = "http://example.com/foo/bar/";
let expected = "com,example)/foo/bar";
assert_eq!(generate_surt(url).unwrap(), expected);
}

#[test]
fn test_generate_surt_with_url_with_www_subdomain() {
let url = "http://www.example.com";
let expected = "com,example)/";
assert_eq!(generate_surt(url).unwrap(), expected);
}
}
Loading
Oops, something went wrong.