feat: rm swift-rs/example

This commit is contained in:
2023-12-09 18:59:46 +08:00
parent 5e73466400
commit df36022203
7 changed files with 0 additions and 194 deletions

View File

@@ -1,12 +0,0 @@
[package]
name = "example"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
swift-rs = { path = "../" }
[build-dependencies]
swift-rs = { path = "../", features = ["build"] }

View File

@@ -1,9 +0,0 @@
use swift_rs::SwiftLinker;
fn main() {
// Ensure this matches the versions set in your `Package.swift` file.
SwiftLinker::new("10.15")
.with_ios("11")
.with_package("swift-lib", "./swift-lib/")
.link();
}

View File

@@ -1,39 +0,0 @@
use swift_rs::{swift, Bool, Int, SRObject, SRObjectArray, SRString};
#[repr(C)]
struct Volume {
pub name: SRString,
path: SRString,
total_capacity: Int,
available_capacity: Int,
is_removable: Bool,
is_ejectable: Bool,
is_root_filesystem: Bool,
}
#[repr(C)]
struct Test {
pub null: bool,
}
swift!(fn get_file_thumbnail_base64(path: &SRString) -> SRString);
swift!(fn get_mounts() -> SRObjectArray<Volume>);
swift!(fn return_nullable(null: Bool) -> Option<SRObject<Test>>);
fn main() {
let path = "/Users";
let thumbnail = unsafe { get_file_thumbnail_base64(&path.into()) };
println!(
"length of base64 encoded thumbnail: {}",
thumbnail.as_str().len()
);
let mounts = unsafe { get_mounts() };
println!("First Volume Name: {}", mounts[0].name);
let opt = unsafe { return_nullable(true) };
println!("function returned nil: {}", opt.is_none());
let opt = unsafe { return_nullable(false) };
println!("function returned data: {}", opt.is_some());
}

View File

@@ -1,7 +0,0 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

View File

@@ -1,30 +0,0 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "swift-lib",
platforms: [
.macOS(.v10_15), // macOS Catalina. Earliest version that is officially supported by Apple.
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "swift-lib",
type: .static,
targets: ["swift-lib"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "SwiftRs", path: "../../")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "swift-lib",
dependencies: [.product(name: "SwiftRs", package: "SwiftRs")],
path: "src")
]
)

View File

@@ -1,3 +0,0 @@
# swift
A description of this package.

View File

@@ -1,94 +0,0 @@
import SwiftRs
import AppKit
@_cdecl("get_file_thumbnail_base64")
func getFileThumbnailBase64(path: SRString) -> SRString {
let path = path.toString();
let image = NSWorkspace.shared.icon(forFile: path)
let bitmap = NSBitmapImageRep(data: image.tiffRepresentation!)!.representation(using: .png, properties: [:])!
return SRString(bitmap.base64EncodedString())
}
class Volume: NSObject {
var name: SRString
var path: SRString
var total_capacity: Int
var available_capacity: Int
var is_removable: Bool
var is_ejectable: Bool
var is_root_filesystem: Bool
public init(name: String, path: String, total_capacity: Int, available_capacity: Int, is_removable: Bool, is_ejectable: Bool, is_root_filesystem: Bool) {
self.name = SRString(name);
self.path = SRString(path);
self.total_capacity = total_capacity
self.available_capacity = available_capacity
self.is_removable = is_removable
self.is_ejectable = is_ejectable
self.is_root_filesystem = is_root_filesystem
}
}
@_cdecl("get_mounts")
func getMounts() -> SRObjectArray {
let keys: [URLResourceKey] = [
.volumeNameKey,
.volumeIsRemovableKey,
.volumeIsEjectableKey,
.volumeTotalCapacityKey,
.volumeAvailableCapacityKey,
.volumeIsRootFileSystemKey,
]
let paths = autoreleasepool {
FileManager().mountedVolumeURLs(includingResourceValuesForKeys: keys, options: [])
}
var validMounts: [Volume] = []
if let urls = paths {
autoreleasepool {
for url in urls {
let components = url.pathComponents
if components.count == 1 || components.count > 1
&& components[1] == "Volumes"
{
let metadata = try? url.promisedItemResourceValues(forKeys: Set(keys))
let volume = Volume(
name: metadata?.volumeName ?? "",
path: url.path,
total_capacity: metadata?.volumeTotalCapacity ?? 0,
available_capacity: metadata?.volumeAvailableCapacity ?? 0,
is_removable: metadata?.volumeIsRemovable ?? false,
is_ejectable: metadata?.volumeIsEjectable ?? false,
is_root_filesystem: metadata?.volumeIsRootFileSystem ?? false
)
validMounts.append(volume)
}
}
}
}
return SRObjectArray(validMounts)
}
class Test: NSObject {
var null: Bool
public init(_ null: Bool)
{
self.null = null;
}
}
@_cdecl("return_nullable")
func returnNullable(null: Bool) -> Test? {
if (null == true) { return nil }
return Test(null)
}