summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 62d3fa4c02eac779fca42963a06e6aab18b8fc14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const z = @import("std");
const urlencode = @import("percent_encoding.zig");

var gpa = z.heap.GeneralPurposeAllocator( .{ .thread_safe = true } ){};
const alloc = gpa.allocator();

const JishoData = struct {
  pub const DataEntry = struct {
    slug: []const u8,
    japanese: []struct {
      word: []const u8,
      reading: []const u8,
    },
    senses: []struct {
      english_definitions: [][]const u8
    },
  };

  meta: struct { status: u32 },
  data: []DataEntry,
};

/// freed by caller
fn requestWord( word: []const u8 ) ![]const u8 {
  var client = z.http.Client{ .allocator = alloc };
  defer client.deinit();

  const encoded = urlencode.encode_alloc( alloc, word, .{} ) catch |e| {
    z.debug.print( "error encoding word {s}\n", .{ word } ); return e;
  };
  defer alloc.free( encoded );

  const url = try z.fmt.allocPrint( alloc, "https://jisho.org/api/v1/search/words?keyword={s}", .{ encoded } );
  defer alloc.free( url );
  const uri = try z.Uri.parse( url );

  var buf: [4096]u8 = undefined;
  var req = try client.open( .GET, uri, .{ .server_header_buffer = &buf } );
  defer req.deinit();

  req.send() catch |e| { z.debug.print( "error sending request to {s}\n", .{ url } ); return e; };
  req.finish() catch |e| { z.debug.print( "error sending request to {s}\n", .{ url } ); return e; };
  req.wait() catch |e| { z.debug.print( "error sending request to {s}\n", .{ url } ); return e; };

  if( req.response.status != .ok ) {
    z.debug.print( "invalid response from {s}: {d}\n", .{ url, @intFromEnum( req.response.status ) } );
    return error.InvalidResponse;
  }

  var reader = req.reader();
  const body = try reader.readAllAlloc( alloc, 999999 );

  return body;
}

fn formatDef( buf: []u8, data: *JishoData.DataEntry, definition_count: u32, sense_count: u32 ) ![]const u8 {
  if( data.japanese.len == 0 ) {
    return error.NotJapanese;
  }

  const wordb = try z.fmt.bufPrint( buf, "{s}({s}) - ", .{ data.japanese[0].word, data.japanese[0].reading } );
  var len = wordb.len;
  var engb: []const u8 = buf[len..];
  for( data.senses, 0.. ) |sense, i| {
    if( i > sense_count ) {
      engb = try z.fmt.bufPrint( buf[len..], ", etc...", .{} );
      len += engb.len;
      break;
    }

    for( sense.english_definitions, 0.. ) |definition, j| {
      if( j > definition_count )
        break
      else if( j < definition_count and j < sense.english_definitions.len - 1 )
        engb = try z.fmt.bufPrint( buf[len..], "{s}/", .{ definition } )
      else
        engb = try z.fmt.bufPrint( buf[len..], "{s}", .{ definition } );
      len += engb.len;
    }

    if( i < sense_count and i < data.senses.len - 1 ) {
      engb = try z.fmt.bufPrint( buf[len..], ", ", .{} );
      len += engb.len;
    }
  }

  return buf[0..len];
}

fn parseArgs( definitions_count: *u32, senses_count: *u32 ) ![]const u8 {
  const args = try z.process.argsAlloc( alloc );
  defer z.process.argsFree( alloc, args );
  if( args.len < 2 ) {
    z.debug.print( "usage: {s} [-d <definitions> -s <senses>] <word>\n", .{args[0]} );
    return error.InvalidArgs;
  }

  var usedargs: u32 = 0;
  for( args, 0.. ) |arg, i| {
    if( z.mem.eql( u8, arg, "-d" ) ) {
      if( i == args.len - 1 )
        return error.InvalidDefinitionCount;

      definitions_count.* = z.fmt.parseInt( u32, args[i + 1], 10 ) catch return error.InvalidSenseCount;
      usedargs += 1;
    }
    if( z.mem.eql( u8, arg, "-s" ) ) {
      if( i == args.len - 1 )
        return error.InvalidSenseCount;

      senses_count.* = z.fmt.parseInt( u32, args[i + 1], 10 ) catch return error.InvalidSenseCount;
      usedargs += 1;
    }
  }

  if( args.len - usedargs < 2 ) {
    z.debug.print( "usage: {s} [-d <definitions> -s <senses>] <word>\n", .{args[0]} );
    z.process.argsFree( alloc, args );
    return error.InvalidArgCount;
  }

  return alloc.dupe( u8, args[args.len - 1] );
}

pub fn main() !void {
  var definitions_count: u32 = 3;
  var senses_count: u32 = 4;

  const word = parseArgs( &definitions_count, &senses_count ) catch |e| {
    z.debug.print( "failed to parse arguments: {any}\n", .{e} );
    return;
  };
  const res = requestWord( word ) catch |e| {
    z.debug.print( "failed to request word: {any}\n", .{e} );
    return;
  };
  const parsed = z.json.parseFromSlice( JishoData, alloc, res, .{ .ignore_unknown_fields = true } ) catch |e| {
    z.debug.print( "failed to parse response body ({any}): {s}\n", .{e, res} );
    return;
  };
  if( parsed.value.data.len == 0 ) {
    z.debug.print( "response empty: not japanese?\n", .{} );
    return;
  }

  const data = &parsed.value.data[0];

  // Good Enough:tm:
  var buf: [64000]u8 = undefined;
  const str = formatDef( &buf, data, definitions_count - 1, senses_count - 1 ) catch |e| {
    if( e == error.NotJapanese ) {
      z.debug.print( "response empty: not japanese?\n", .{} );
      return;
    }
    return e;
  };
  try z.io.getStdOut().writer().print( "{s}\n", .{str} );

  parsed.deinit();
  alloc.free( word );
  alloc.free( res );
  _ = gpa.deinit();
}